现象
Ocelot + Concul 进行访问
Ocelot Concul 服务都部署在同一台机器上时,访问正常
服务部署在另外一台机器上时,无法访问 显示 '102 (unknown) status code of request URI:
发现Ocelot 日志显示是 hostname/api
所有 换了一台电脑就无法访问了
Ocelot 是 C# .net6 23.3.3
解决方法
在官网上查到
https://ocelot.readthedocs.io/en/latest/features/servicediscovery.html
这里就会直接访问服务的ip
public class MyConsulServiceBuilder : DefaultConsulServiceBuilder
{
public MyConsulServiceBuilder(Func<ConsulRegistryConfiguration> configurationFactory, IConsulClientFactory clientFactory, IOcelotLoggerFactory loggerFactory)
: base(configurationFactory, clientFactory, loggerFactory) { }
// I want to use the agent service IP address as the downstream hostname
protected override string GetDownstreamHost(ServiceEntry entry, Node node) => entry.Service.Address;
}
services.AddOcelot()
.AddConsul<MyConsulServiceBuilder>();