咱们项目中有不少须要配置的地方,最多见的就是各类服务URL地址,这些地址针对不一样的运行环境还不同,无论和打包仍是部署都麻烦,须要很是的当心。通常配置都是存储到配置文件里面,无论多小的配置变更,都须要对应用程序进行重启,对于分布式系统来讲,这是很是不可取的。因此配置中心就在这种场景孕育出来,可以适配不一样的环境,正在运行的程序不用重启直接生效。html
如今开始介绍咱们今天的主角spring cloud config,我以为它最大的优势就是能够和git作集成,使用起来很是方便。spring cloud config包含服务端和客户端,服务端提供配置的读取和配置仓库,客户端来获取配置。java
也可使用svn或者文件来存储配置文件,咱们这里只讲Git的方式git
咱们模拟一个业务场景,有一个远程配置文件咱们经过应用程序获取它。github
咱们须要建立2个应用程序:配置服务服务端(Java),配置服务客户端(.Net Core)和一个Github仓库。
使用IntelliJ IDEA建立一个spring boot项目,建立配置中心服务端,端口设置为5100web
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
@SpringBootApplication @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
server.port=5100 spring.application.name=config-server #git仓库地址 spring.cloud.config.server.git.uri=https://github.com/longxianghui/configs.git #git用户名和密码 #spring.cloud.config.server.git.username=xxx #spring.cloud.config.server.git.password=xxx #git目录下的文件夹,多个用逗号分割 #spring.cloud.config.server.git.search-paths=xxx,xxx,xxx
使用Github建立一个仓库,并提交3个文件,文件内容以下(注意yml格式)spring
name: mickey age: 3 env: test
name: fiona age: 28 env: test
name: leo age: 30 env: prod
配置文件命名规则{application}-{profile}.yml
支持yml和properties格式json
运行配置中心服务端
在浏览器输入http://localhost:5001/demo/dev
再访问http://localhost:5001/demo/test
再访问http://localhost:5001/demo/prod
经过上面3个URL咱们发现配置中心经过REST的方式将配置信息返回。
配置服务REST规则以下:api
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties浏览器
下面咱们再看看.NET程序如何读取配置信息呢?
建立一个 .net core web api程序,端口5101架构
<PackageReference Include="Steeltoe.Extensions.Configuration.ConfigServer" Version="1.1.0" />
{ "spring": { "application": { "name": "demo"//与配置文件的名称对应 }, "cloud": { "config": { "uri": "http://localhost:5100", "env": "dev" //与环境名称对应 } } }, "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Warning" } } }
public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddEnvironmentVariables() .AddConfigServer(env); Configuration = builder.Build(); } public void ConfigureServices(IServiceCollection services) { services.AddConfigServer(Configuration); // Add framework services. services.AddMvc(); services.Configure<Demo>(Configuration); }
public class Demo { public string Name { get; set; } public int Age { get; set; } public string Env { get; set; } }
[Route("/")] public class ValuesController : Controller { private readonly IConfigurationRoot _config; private readonly IOptionsSnapshot<Demo> _configDemo; public ValuesController(IConfigurationRoot config, IOptionsSnapshot<Demo> configDemo) { _config = config; _configDemo = configDemo; } [HttpGet] public Demo Get() { //两种方式获取配置文件的数据 //var demo = new Demo //{ // Name = _config["name"], // Age = int.Parse(_config["age"]), // Env = _config["env"] //}; var demo = _configDemo.Value; return demo; } }
运行程序 浏览器访问http://localhost:5101/,
更改配置文件appsettings.json,"env": "test"
,从新启动程序,刷新页面
再更改配置文件appsettings.json,"env": "prod"
,程序启动程序,刷新页面
这样经过修改本地的配置文件,就能获取远程上的各类配置了。
咱们再试试修改远程的配置文件,修改demo-prod.yml的配置name: leo1,提交到github。
再访问http://localhost:5011/,发现配置并无变化,这是由于配置服务并不知道git有更新,咱们重启配置服务,再次访问,问题依旧,那么再重启客户端,发现咱们获得了刚才更新的配置name= leo1,配置生效了。
经过上面的例子,咱们可以经过应用程序获取到配置信息,可是这有明显的问题,总不能一有配置更新就去重启配置服务和客户端吧?如何作到自动通知到客户端呢?留下这些问题,敬请期待下一章。
全部代码均上传github。代码按照章节的顺序上传,例如第一章demo1,第二章demo2以此类推。
求推荐,大家的支持是我写做最大的动力,个人QQ群:328438252,交流微服务。
java部分
.net部分