
.net core 配置中心(Apollo)
Apollo简介
Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性,适用于微服务配置管理场景。
服务端基于Spring Boot和Spring Cloud开发,打包后可以直接运行,不需要额外安装Tomcat等应用容器。
Java客户端不依赖任何框架,能够运行于所有Java运行时环境,同时对Spring/Spring Boot环境也有较好的支持。
.Net客户端不依赖任何框架,能够运行于所有.Net运行时环境。
更多产品介绍参见Apollo配置中心介绍
本地快速部署请参见Quick Start
演示环境(Demo):
106.54.227.205
账号/密码: apollo/admin
如访问github速度缓慢,可以访问gitee镜像,不定期同步
通过 Docker 来进行安装 Apollo
注意
- 请确保 Docker 已经安装好
- 请确保 Docker-Compose 已经安装好
- 尽量使用国内的加速器
- 如果是阿里云服务器请在安全组规则中添加 8080与8070的端口
相关地址
Apollo安装地址:GitHub
项目地址:GitHub
安装
下载好Apollo后,打开终端到当前目录下执行命令
docker-compose up
其他命令
# 也可以后台运行
[root@xxxx]# docker-compose up -d
# 更新到最新镜像
[root@xxxx]# docker-compose pull
# 停止镜像并删除
[root@xxxx]# docker-compose down
安装与启动
访问你部署服务器的地址,登录到 dashboard:
http://Your_Server_IP:8070
用户名: apollo
密码: admin
configServer:
http://Your_Server_IP:8080
创建项目
创建TestApolloProject项目
创建环境变量
其他
查看历史记录
更多功能
创建.net core示例项目
安装依赖包
项目结构
修改内容
Program.cs
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostBuilderContext,configurationBuilder)=>{
//注入配置
//把阿波罗的日志级别调整为最低
LogManager.UseConsoleLogging(Com.Ctrip.Framework.Apollo.Logging.LogLevel.Trace);
//从已加载的配置文件中读取,阿波罗的基础配置,并注入
configurationBuilder.AddApollo(configurationBuilder.Build().GetSection("Apollo"))
//注入默认配置
.AddDefault(Com.Ctrip.Framework.Apollo.Enums.ConfigFileFormat.Properties);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
TestController.cs
[Route("api/[controller]")]
[ApiController]
public class TestController : ControllerBase
{
private readonly IConfiguration _configuration;
public TestController(IConfiguration configuration)
{
_configuration = configuration;
}
[HttpGet]
public string GetENV_ABV2()
{
return _configuration["ENV_ABV2"];
}
}
appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Apollo": {
"AppId": "TestApolloProject",
"Env": "DEV",
"MetaServer": "http://Your_Server_IP:8080/",
"ConfigServer": [ "http://Your_Server_IP:8080/" ]
},
"AllowedHosts": "*"
}
配置热更新展示
当前配置
获取当前配置成功
修改当前配置并发布
再次访问接口,达成热更新
补充
添加部门
输入organizations
然后点击查询,在末尾添加上一行然后点击保存就可以了
添加json数据
普通的文本命名空间是不支持json数据的,所以我们需要添加新的命名空间,并且是json格式的。
找到项目的左下角,点击添加Namespace
然后根据下图所示添加json类型的命名空间,我们还可以看到它还可以支持xml
、yml
、yaml
、json
、txt
.
最后点击保存。
最后我这里简单的写了个配置。
然后在我们的代码中需要做如下修改:添加好我们的命名空间,并规定好其中的格式。
configurationBuilder.AddApollo(configurationBuilder.Build().GetSection("Apollo"))
.AddNamespace("SWPrinterNS", ConfigFileFormat.Json);
在我们测试时使用热更新的方式
public class IdentityModelConfig
{
public string Authority { get; set; }
public string Audience { get; set; }
public bool IsHttps { get; set; }
public bool ValidateIssuer { get; set; }
public bool ValidateAudience { get; set; }
}
services.Configure<IdentityModelConfig>(configuration.GetSection("Identity_Config"));
[ApiVersion("1.0")]
[Route("api/[controller]")]
[ApiController]
public class TestAPIController : ControllerBase
{
public readonly IOptionsMonitor<IdentityModelConfig> _IomIMC;
public TestAPIController(
IOptionsMonitor<IdentityModelConfig> IomIMC
)
{
_IomIMC = IomIMC;
}
[HttpGet("ID4")]
public string TestID4env()
{
return JsonConvert.SerializeObject(_IomIMC);
}
}
Apollo迁移
首先通过如下命令在旧的宿主机上进入容器,生成数据文件,并将数据文件放到旧的宿主机上。
# 进入容器
docker exec -it apollo-db /bin/bash
# 生成apollo相关数据文件
mysqldump -t -uroot --databases ApolloPortalDB --tables App AppNamespace Authorities Consumer ConsumerRole ConsumerToken Favorite Permission Role RolePermission UserRole Users --complete-insert > portal_dump.sql
mysqldump -t -uroot --databases ApolloConfigDB --tables App AppNamespace AccessKey Cluster Commit Item Namespace Release ReleaseHistory --complete-insert > config_Pro_dump.sql
exit
# 复制数据文件到宿主机
docker cp apollo-db:/config_Pro_dump.sql .
docker cp apollo-db:/portal_dump.sql .
将这两个数据文件复制到新的宿主机上后,创建清理数据的cleardatafile.sql
文件,命令如下:
# 编辑清理数据文件
cat << EOF > cleardatafile.sql
Use ApolloPortalDB;
truncate table App;
truncate table AppNamespace;
truncate table Authorities;
truncate table Consumer;
truncate table ConsumerRole;
truncate table ConsumerToken;
truncate table Favorite;
truncate table Permission;
truncate table Role;
truncate table RolePermission;
truncate table UserRole;
truncate table Users;
Use ApolloConfigDB;
truncate table App;
truncate table AppNamespace;
truncate table AccessKey;
truncate table Cluster;
truncate table Commit;
truncate table Item;
truncate table Namespace;
truncate table ReleaseHistory;
EOF
然后在新的宿主机上,将三个文件复制到容器名为apollo-db的mysql数据库中,然后关闭apollo应用的运行,执行数据脚本,随后再启动apollo应用。
docker cp cleardatafile.sql apollo-db:/
docker cp config_Pro_dump.sql apollo-db:/
docker cp portal_dump.sql apollo-db:/
docker stop apollo-quick-start
docker exec -it apollo-db /bin/bash
mysql -uroot -A
source /cleardatafile.sql
use ApolloPortalDB;
source /portal_dump.sql
use ApolloConfigDB;
source /config_Pro_dump.sql
docker start apollo-quick-start
在完成迁移的最后需要到Apollo 仪表板中发布相关的Config。
更多连接请参考
Apollo 项目地址:https://github.com/ctripcorp/apollo
Apollo.net 项目地址:https://github.com/ctripcorp/apollo.net/tree/dotnet-core/
.Net客户端使用指南:https://github.com/ctripcorp/apollo/wiki/.Net%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739


13655202412
热更新是怎么配置的