.NET Core Apollo 配置中心

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<Startup>();
                });
    }
}

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<IEnumerable<string>> Get()
        {
            var appName = _configuration["AppName"];
            return new string[] { "value1", "value2", appName };
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值