源码及系列文章目录
Git 源码 :https://github.com/tangsong1995/TS.Microservices
CSDN 资源 :https://download.csdn.net/download/qq_33649351/34675095
系列文章目录 :https://blog.csdn.net/qq_33649351/article/details/120998558
ConfigMap
ConfigMap 是 k8s 提供的内置的配置管理。
从文件创建
使用 from-file ,读取 configs 文件夹下的所有文件进行创建:
kubectl create configmap ts-microservices-webapi-config --from-file=ts-microservices-webapi/configs -o yaml --dry-run | kubectl apply -f -
从键值对创建
使用 from-env-file ,读取 env.txt 的内容已键值对的方式创建 ConfigMap :
kubectl create configmap ts-microservices-config --from-env-file=env.txt -o yaml --dry-run | kubectl apply -f -
映射为环境变量
env:
- name: ASPNETCORE_HOSTINGSTARTUPASSEMBLIES
valueFrom:
configMapKeyRef:
name: ts-microservices-config
key: skywalking_hostingStartup
映射为文件
volumeMounts:
- mountPath: "/app/appsettings.json"
name: appsettings
subPath: appsettings-{{.Chart.AppVersion}}.json
注意点
建议版本化管理配置文件,跟随服务版本,以支持快速回滚
Apollo
Apollo 配置中心系统的能力
- 权限与审计
- 版本管理
- 热更新
- 原生支持 Java、.NET 客户端
安装
docker-compose up
docker-compose.yml 文件:
version: '2'
services:
apollo-quick-start:
image: nobodyiam/apollo-quick-start
container_name: apollo-quick-start
depends_on:
- apollo-db
ports:
- "8080:8080"
- "8070:8070"
links:
- apollo-db
apollo-db:
image: mysql:5.7
container_name: apollo-db
environment:
TZ: Asia/Shanghai
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
depends_on:
- apollo-dbdata
ports:
- "13306:3306"
volumes:
- ./sql:/docker-entrypoint-initdb.d
volumes_from:
- apollo-dbdata
apollo-dbdata:
image: alpine:latest
container_name: apollo-dbdata
volumes:
- /var/lib/mysql
代码集成
引用包:Com.Ctrip.Framework.Apollo.Configuration
Program 添加配置:
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>();
});
配置:
"Apollo": {
"AppId": "ts-microservices-mobile-gateway",
"Env": "DEV",
"MetaServer": "http://192.168.2.146:8080",
"ConfigServer": [ "http://192.168.2.146:8080" ]
},