你可能不知道的.Net Core Configuration

目录

执行原理

环境变量

Spring Cloud Config Server

挂卷Volume

Config Server vs Volume

 

执行原理

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

    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的内容。html

 

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

  appsettings.json:git

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

  cussettings.jsongithub

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

  结果:spring

  

   

  3. 能够设置配置文件不存在或失效时,程序不会被停止,该配置会被忽略。docker

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

  

 

环境变量

  1. appsettings.{env.EnvironmentName}.jsonapp

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

  能够在四个地方设置该环境变量:

  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支持各类配置文件类型。

相关文章
相关标签/搜索