微服务Demo

第4章 4.2 dotnet启用分布式配置中心(spring c

2018-10-29  本文已影响43人  elef

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的本地磁盘,也支持放在远程Git仓库中。下面演示的配置信息存储在本地磁盘中。

配置存储目录

/home/daijinming/configbase/spring-cloud-config-server

开发配置文件

demo-dev.yml

文件内容

appname: demoapplication
message: this is a demo app

启用服务

docker run -it -p 8888:8888 \
      -v /home/daijinming/demo/configbase/spring-cloud-config-server:/config \
      -e SPRING_PROFILES_ACTIVE=native \
      hyness/spring-cloud-config-server

证明配置服务中心可以从远程程序获取配置信息,http请求地址和资源文件映射如下:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

测试地址:

http://127.0.0.1:8888/demo-dev.yml
或者
http://127.0.0.1:8888/demo-dev.properties

如果能正常打开上面的网址,说明本机config服务开启成功。

创建dotnet webapi 项目

采用root账号打开code(vscode)
sudo code --user-data-dir

#创建API项目
$ dotnet new webapi -n webapi-config

#添加引用
$ dotnet add package Steeltoe.Extensions.Configuration.ConfigServerCore

Program.cs


using Steeltoe.Extensions.Configuration.ConfigServer;

namespace chapter4_webapi
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
            .AddConfigServer()       //添加对ConfigServer的支持
            .UseStartup<Startup>();
    }
}

Startup.cs


using Steeltoe.Extensions.Configuration.ConfigServer;

namespace chapter4_webapi
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            services.ConfigureConfigServerClientOptions(Configuration);
            //新增加
            services.AddConfiguration(Configuration);
            //新增加
            services.Configure<demo>(this.Configuration);

        }
      
    }
}

ValuesController.cs

using Microsoft.Extensions.Options;
using Steeltoe.Extensions.Configuration.ConfigServer;


namespace chapter4_webapi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {

         private IOptionsSnapshot<demo> IConfigServerData { get; set; }     

        public ValuesController(IOptionsSnapshot<demo> configServerData)
        {

            if (configServerData != null)
                IConfigServerData = configServerData;
                
        }

        [HttpGet]
        public demo Get()
        {

            var demo = IConfigServerData.Value;
            return demo;
        }
    
}

运行程序,浏览器输入:http://127.0.0.1:5000/api/values

上一篇 下一篇

猜你喜欢

热点阅读