spring cloud+dotnet core搭建微服务架构

来源:互联网 发布:福建师范大学软件学院 编辑:程序博客网 时间:2024/06/05 19:11

  现在开始介绍我们今天的主角spring cloud config,我觉得它最大的优点就是可以和git做集成,使用起来非常方便。spring cloud config包含服务端和客户端,服务端提供配置的读取和配置仓库,客户端来获取配置。

  也可以使用svn或者文件来存储配置文件,我们这里只讲Git的方式

  业务场景

  我们模拟一个业务场景,有一个远程配置文件我们通过应用程序获取它。

  代码实现

  我们需要创建2个应用程序:配置服务服务端(Java),配置服务客户端(.Net Core)和一个Github仓库。

  使用IntelliJ IDEA创建一个spring boot项目,创建配置中心服务端,端口设置为5100

  pom.xml

  org.springframework.cloud

  spring-cloud-config-server

  ConfigServerApplication.java

  @SpringBootApplication

  @EnableConfigServer

  public class ConfigServerApplication {

  public static void main(String[] args) {

  SpringApplication.run(ConfigServerApplication.class, args);

  }

  }

  application.properties

  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格式)

  demo-dev.yml

  name: mickey

  age: 3

  env: test

  demo-test.yml

  name: fiona

  age: 28

  env: test

  demo-prod.yml

  name: leo

  age: 30

  env: prod

  配置文件命名规则{application}-{profile}.yml

  支持yml和properties格式

  运行配置中心服务端

  在浏览器输入http://localhost:5001/demo/dev

  再访问http://localhost:5001/demo/test

  再访问http://localhost:5001/demo/prod

  通过上面3个URL我们发现配置中心通过REST的方式将配置信息返回。

  配置服务REST规则如下:成都男性早泄的症?

  /{application}/{profile}[/{label}]

  /{application}-{profile}.yml

  /{label}/{application}-{profile}.yml

  /{application}-{profile}.properties

  /{label}/{application}-{profile}.properties

  下面我们再看看.NET程序如何读取配置信息呢?

  创建一个 .net core web api程序,端口5101

  nuget引用

  appsettings.json

  {

  "spring": {

  "application": {

  "name": "demo"//与配置文件的名称对应

  },

  "cloud": {

  "config": {

  "uri": "http://localhost:5100",

  "env": "dev" //与环境名称对应龟头流白色的脓是怎么回事?

  }

  }

  },

  "Logging": {

  "IncludeScopes": false,

  "LogLevel": {

  "Default": "Warning"

  }

  }

  }

  Startup.cs

  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(Configuration);

  }

  Demo.cs

  public class Demo

  {

  public string Name { get; set; }

  public int Age { get; set; }

  public string Env { get; set; }

  }

  ValuesController.cs

  [Route("/")]

  public class ValuesController : Controller

  {

  private readonly IConfigurationRoot _config;

  private readonly IOptionsSnapshot _configDemo;

  public ValuesController(IConfigurationRoot config, IOptionsSnapshot 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,配置生效了。

  后记

  通过上面的例子,我们能够通过应用程序获取到配置信息,但是这有明显的问题,总不能一有配置更新就去重启配置服务和客户端吧?如何做到自动通知到客户端呢?留下这些问题,敬请期待下一章。

阅读全文
0 0
原创粉丝点击