This commit is contained in:
2026-05-10 13:05:23 +08:00
@@ -24,31 +24,41 @@ namespace IM.InitCommon
builder.Host.ConfigureAppConfiguration((hostCtx, configbuilder) => builder.Host.ConfigureAppConfiguration((hostCtx, configbuilder) =>
{ {
var env = hostCtx.HostingEnvironment; var env = hostCtx.HostingEnvironment;
string serviceName = env.ApplicationName.Replace(".WebApi", ""); string serviceName = env.ApplicationName.Replace(".WebApi", "");
ConsulOption consulOption = hostCtx.Configuration.GetSection("ConsulOptions").Get<ConsulOption>() ?? new ConsulOption(); // 优先从环境变量读取
string consulUrl =
Environment.GetEnvironmentVariable("CONSUL_URL")
?? "http://localhost:8500";
string consulKey = $"IM/{env.EnvironmentName}/appsettings.json"; string consulKey = $"IM/{env.EnvironmentName}/appsettings.json";
string serviceConsulKey = $"IM/{env.EnvironmentName}/{serviceName}/appsettings.json"; string serviceConsulKey = $"IM/{env.EnvironmentName}/{serviceName}/appsettings.json";
configbuilder.AddConsul( configbuilder.AddConsul(
consulKey, consulKey,
options => options =>
{ {
options.ConsulConfigurationOptions = cco => options.ConsulConfigurationOptions = cco =>
{ {
cco.Address = new Uri(consulOption.Url); cco.Address = new Uri(consulUrl);
}; };
//options.Optional = true; // 如果本地开发没开 Consul,不报错,继续运行
options.ReloadOnChange = true; // 开启热更新!Consul 里改了,程序立马生效
//options.OnLoadException = exceptionContext => { exceptionContext.Ignore = true; };
}
);
configbuilder.AddConsul(serviceConsulKey, options => options.ReloadOnChange = true;
{ });
options.ConsulConfigurationOptions = cco => { cco.Address = new Uri(consulOption.Url); };
options.Optional = true; configbuilder.AddConsul(
options.ReloadOnChange = true; serviceConsulKey,
}); options =>
{
options.ConsulConfigurationOptions = cco =>
{
cco.Address = new Uri(consulUrl);
};
options.Optional = true;
options.ReloadOnChange = true;
});
}); });
} }