我正在尝试构建我自己的微服务体系结构,并且被困在了API.Gateway部分。我试图让Ocelot V14在.sln文件中找到所有配置、、ocelot.json或configuration.json文件。

在Program.cs文件中,我试图将配置文件与以下链接https://ocelot.readthedocs.io/en/latest/features/configuration.html#react-to-configuration-changes中的代码合并
builder.ConfigureServices(s => s.AddSingleton(builder))
.ConfigureAppConfiguration((hostingContext, config) =>
{
config
.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
.AddJsonFile("appsettings.json", true, true)
.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
.AddOcelot(hostingContext.HostingEnvironment)
.AddEnvironmentVariables();
})
.UseStartup<Startup>();当我运行这个程序时,应用程序在我的ocelot.json项目中创建以下的OcelotApiGw 文件
{
"ReRoutes": [
]
}问题是它是空的,重路由不起作用。当我将所需的重路由粘贴到这个ocelot.json文件中时,重路由工作,这不是我想要的功能。
我想要的是从不同的.json文件中自动合并配置文件。
任何帮助都将不胜感激。
eShopOnContainers是如何用Ocelot V12这样实现的
IWebHostBuilder builder = WebHost.CreateDefaultBuilder(args);
builder.ConfigureServices(s => s.AddSingleton(builder))
.ConfigureAppConfiguration(ic => ic.AddJsonFile(Path.Combine("configuration", "configuration.json")))
.UseStartup<Startup>();如果您需要更多的代码,文件结构或其他任何东西,只需评论和询问。
发布于 2020-03-15 20:11:11
不知道这是不是你要找的?这就是Ocelot将多个路由文件合并在一起的方式。
https://ocelot.readthedocs.io/en/latest/features/configuration.html#merging-configuration-files
我们不使用这个,但是我们是这样定义我们的创业的:
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddJsonFile(this.env.IsDevelopment() ? "ocelot.json" : "ocelot.octopus.json")
.AddEnvironmentVariables();因此,我们有我们的标准appSettings加上我们使用的Ocelot,当我们的Ocelot实例被部署到我们的测试/生产envs (或者只是我们的测试/本地实例)中时,Octopus将转换我们想要的各种变量。
这似乎定义了如何处理多个文件:
在此场景中,
将查找与Ocelot模式(?i)ocelot匹配的任何文件。(a-Za-Z0-9*).json,然后将它们合并在一起。如果要设置GlobalConfiguration属性,则必须有一个名为ocelot.global.json的文件。
不确定是否需要显式定义每个文件(除非可以通过{env.EnvironmentName}这样的变量来定义它们),但这应该很容易测试。
对不起,如果我弄错了,但希望这能帮上忙。
https://stackoverflow.com/questions/60636040
复制相似问题