微服务之间的配置共享
在搭建微服务时,很多配置都是一样的。这个时候可以把这些配置拿出来做成公共配置,服务单独使用的做成私有配置
目前的项目结构
AspNetCoreSharing
里面创建一个appsetting.public.json
文件作为公共配置,并且将Product.Host
的appsettings.json
改为appsettings.private.json
在Program
添加这两个文件的配置
#if DEBUG
string currentDirectory = Directory.GetCurrentDirectory();
string appsettingPublicPath = Path.GetFullPath(Path.Combine(currentDirectory, @"..\..\", @"sharing\AspNetCoreSharing", "appsettings.public.json"));
builder.Configuration.AddJsonFile(appsettingPublicPath, optional: true, reloadOnChange: true);
builder.Configuration.AddJsonFile("appsettings.private.json", optional: true, reloadOnChange: true);
#else
builder.Configuration.AddJsonFile("appsettings.public.json", optional: true, reloadOnChange: true);
builder.Configuration.AddJsonFile("appsettings.private.json", optional: true, reloadOnChange: true);
#endif
私有配置
必须在公共配置
之后添加。当两个配置都有同一个值存在时,取私有配置
值
这里的if DEBUG
是配置debug
时走项目文件路径。当项目发布后配置文件
和dll
在一个文件夹后便不需要具体的路径地址了
注:我这里的AspNetCoreSharing
是放置一些公共的nuget
包Product.Application
已经引用了它。若没有引用,在发布时并不会生成
发布的文件内容
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律