微服务之间的配置共享
在搭建微服务时,很多配置都是一样的。这个时候可以把这些配置拿出来做成公共配置,服务单独使用的做成私有配置
目前的项目结构
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
已经引用了它。若没有引用,在发布时并不会生成
发布的文件内容