.NET Core Apollo 配置中心

Apollo Quick Start

Apollo(阿波罗)是携程框架部门研发的配置管理平台,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性。
服务端基于Spring Boot和Spring Cloud开发,打包后可以直接运行,不需要额外安装Tomcat等应用容器。

配置中心的应用场景:
● 公司内存在多个系统,比如我们的web站点外加dubbo服务总超过20个,且系统之间的技术架构基本相同并且有一定的联系性
● 一套系统需要配置多个环境,我们有开发环境,测试环境,预上线环境,线上环境

Quick Start
本地部署:https://github.com/ctripcorp/apollo/wiki/Quick-Start
Docker部署:https://github.com/ctripcorp/apollo/wiki/Apollo-Quick-Start-Docker部署
分布式部署:https://github.com/ctripcorp/apollo/wiki/分布式部署指南

1、管理 NuGet 包(N)...

Com.Ctrip.Framework.Apollo.Configuration

2、appsettings.json
AppId:标识应用身份的唯一id;
MetaServer:客户端获取配置的服务器配置;

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "Apollo": {
    "AppId": "apollo.net",
    "Cluster": "test",
    "MetaServer": "http://localhost:8080/",
    "Secret": "f55faf3729e14866ac5fc2bed2293643",
    "Namespaces": ["application.xml","application.json","application.yml","application.yaml","application"],
    "Env": "Dev",
    "Meta": {
      "DEV": "http://106.54.227.205:8080/",
      "FAT": "http://106.54.227.205:8080/",
      "UAT": "http://106.54.227.205:8080/",
      "PRO": "http://106.54.227.205:8080/"
    }
  }
}

3、Program.cs

using Com.Ctrip.Framework.Apollo;
using Com.Ctrip.Framework.Apollo.Logging;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;

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

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((context, config) =>
                {
                    //注入配置
                    //把阿波罗的日志级别调整为最低
                    LogManager.UseConsoleLogging(Com.Ctrip.Framework.Apollo.Logging.LogLevel.Trace);

                    config
                    .AddApollo(config.Build().GetSection("Apollo"))
                    .AddDefault()
                    //.AddDefault(Com.Ctrip.Framework.Apollo.Enums.ConfigFileFormat.Properties); //添加默认application Namespace
                    .AddNamespace("TEST3.Shared")
                    .AddNamespace("ClientService");
                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup();
                });
    }
}

4、Startup.cs
*
*
5、ValuesController.cs

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System.Collections.Generic;

namespace ApolloProject.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        private readonly IConfiguration _configuration;

        public ValuesController(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        [HttpGet]
        public ActionResult> Get()
        {
            var appName = _configuration["AppName"];
            return new string[] { "value1", "value2", appName };
        }
    }
}

*
*
*
*
*

你可能感兴趣的:(.NET,Core,.netcore,java,spring)