你可能不知道的.Net Core Configuration

目录

执行原理

环境变量

Spring Cloud Config Server

挂卷Volume

Config Server vs Volume

 

执行原理

  1. 配置读取顺序:与代码先后顺序一致。

    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", false, true)
            .AddJsonFile("cussettings.json", false, true);
        Configuration = builder.Build();
    }

   以上代码会先读取appsettings.json,再读取cussettings.json,cussettings.json的内容会覆盖appsettings.json的内容。

 

  2. 覆盖逻辑:原有的配置继承,相同的配置覆写,新增的配置添加。

  appsettings.json:

    {
      "settings": {
        "name": "AppSetting",
        "age": 20 
      } 
    }

  cussettings.json

    {
      "settings": {
        "name": "CusSetting",
        "gender": "Male"
      } 
    }

  结果:

  

   

  3. 可以设置配置文件不存在或失效时,程序不会被中止,该配置会被忽略。

  如cussettings.json不存在或失效时:

  

 

环境变量

  1. appsettings.{env.EnvironmentName}.json

  可以根据当前的环境变量设置读取对应的配置,来覆盖之前的配置,有点像Asp.Net的Web Transform,其中环境变量的key为:ASPNETCORE_ENVIRONMENT。

  可以在四个地方设置该环境变量:

  a) Visual Studio 2017中的launchSettings.json

  

  b) 操作系统的环境变量

  Windows:

  

 

  Linux:

  

 

  c) Dockerfile

  FROM microsoft/aspnetcore:1.1
  COPY . /app
  WORKDIR /app
  EXPOSE 5000/tcp
  ENV ASPNETCORE_URLS http://*:5000/
  ENV ASPNETCORE_ENVIRONMENT Production
  ENTRYPOINT ["dotnet", "EnvironmentVariable.dll"]

 

  d) Docker启动指令

  docker run --name {name} -e ASPNETCORE_ENVIRONMENT=Production ...

  如:

  

 

  2. AddEnvironmentVariables()的作用

    var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", false, true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", true)
                .AddEnvironmentVariables();//作用?
    Configuration = builder.Build();

  a) 读取系统的环境变量信息,这个应该大部分人都知道。

  b) 覆盖之前的配置信息。

  appsettings.json:

{
  "settings": {
    "name": "AppSetting"
  }
}

  通过环境变量来覆盖settings:name:

  

  

  说明:这里是用控制台运行,因为操作系统的环境变量要对IIS Express生效,要重启vs2017,我懒!

  另外环境变量使用两下划线(__)作为层次的分隔符,具体可参考EnvironmentVariablesConfigurationProvider的源码。

  

   

Spring Cloud Config Server

  Config Server是Spring Cloud 配置管理的重要中间件,接下来,我们看一下.Net Core如何跟Config Server进行交互。

  1. 准备配置文件,这里使用Git作为配置文件的Source,地址:https://github.com/ErikXu/.NetCore-Configuration

  2. 获取安装包,这里使用Docker启动,因此,是拉取官方镜像:docker pull hyness/spring-cloud-config-server

  

  3.  准备启动资源文件application.yml

info:
  component: config service
 
server:
  port: 8888
 
spring:
  application:
    name: git-config
  profiles:
    active: dev
  cloud:
    config:
      server:
        git:
          uri: https://github.com/ErikXu/.NetCore-Configuration
          searchPaths: Configs

  4. 执行指令启动Config Server

  docker run --name configsvr -it -d -p 8888:8888 -v /root/config-server/application.yml:/config/application.yml hyness/spring-cloud-config-server

 

   5. 引入Nuget包

  Install-Package Steeltoe.Extensions.Configuration.ConfigServer -Version 1.1.1

 

  6. 准备appsetting.json

{
  "spring": {
    "application": {
      "name": "foo"
    },
    "cloud": {
      "config": {
        "uri": "http://192.168.52.142:8888",
        "validate_certificates": false,
        "env": "dev" 
      }
    }
  },
  "settings": {
    "name": "AppSetting"//会被Config Server的内容覆盖
  }
}

 

  7. 引入Config Server

  

  结果:

  

 

挂卷Volume

  挂卷是docker的一种机制,可以把特定的目录或者文件挂载到docker容器的指定位置。配合.Net Core Configuration的机制,可以在appsetting.json中记录开发环境的配置,然后再指定一个待挂载的文件用于记录不同环境的配置,从而覆盖开发环境的配置,达到多环境适配的能力。

  1. appsetting.json

{
  "settings": {
    "name": "AppSetting"
  }
}

  2. 指定一个待挂载的目录,但是在开发环境不存在此文件。

    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", false, true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", true)
            .AddEnvironmentVariables()
            .AddJsonFile("/configs/volume.json", true, true); //待挂载

        Configuration = builder.Build();
    }

  3. 开发环境

  

 

  4. docker启动指令

  docker run --name {name} -v {source}:{target} ...

  

  注:如果有权限问题,需要先执行setenforce 0

 

  5. 效果

  

 

   挂卷感觉跟appsettings.{env.EnvironmentName}.json很像,既然如此,为什么还要有挂卷呢?那是因为appsettings.{env.EnvironmentName}.json是.net core的机制,其它语言不一定会有,或者是另外的机制,docker的挂卷是服务各种语言的。.Net Core是很优秀的语言,它接收了很多业界的需求,也响应了docker挂卷的机制,在这一点上,.net的能力就相对较弱了。

  

Config Server vs Volume

  Config Server是Spring Cloud体系的配置管理中心,而Kubernetes更多是使用Volume的方式(HostPath,Config Map,Secret)。

  Config Server是运行时的配置管理,它会定期检测(心跳,2s)remote config的内容,如果内容更新,就刷新容器的配置信息。Volume是发布(更新)时的配置管理,在发布(更新)期间,就把配置信息挂载到容器中。

  Config Server要自己保证其高可用,否则,Config Server挂了,会读取容器内的原始配置,Volume是把文件挂载到容器中,因此无中间件高可用要求,但要利用Kubernetes等保证发布更新时,配置挂载到了各个容器中。

  Config Server具有一定的语言侵入性(必须要有驱动jdk或者sdk),Volume无语言侵入性。

  Config Server只支持Yaml等少量配置文件类型,Volume支持各种配置文件类型。

posted @ 2018-02-03 23:49  编程玩家  阅读(2430)  评论(1编辑  收藏  举报