微服务之间的配置共享

在搭建微服务时,很多配置都是一样的。这个时候可以把这些配置拿出来做成公共配置,服务单独使用的做成私有配置

目前的项目结构

image

AspNetCoreSharing里面创建一个appsetting.public.json文件作为公共配置,并且将Product.Hostappsettings.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是放置一些公共的nugetProduct.Application已经引用了它。若没有引用,在发布时并不会生成

发布的文件内容

image

posted @   kele-cc  阅读(118)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示