我有一个映射多个服务的网关。当我在没有 docker 的情况下运行它时,这非常有用。但是,当我更改为 docker 版本时,它就不起作用了。
我收到以下消息:
:发布' 2024-10-21 06:21:49 [10:21:49 WRN] 请求 ID:0HN7H3M32VGVE:00000001, previousRequestId:无 PreviousRequestId,消息:'错误代码: ConnectionToDownstreamServiceError 消息:连接时出错 下游服务,异常:System.Net.Http.HttpRequestException: 连接被拒绝 (crm:8001) 2024-10-21 06:21:49 ---> System.Net.Sockets.SocketException(111):连接被拒绝 2024-10-21 06:21:49 于 System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError 错误,CancellationToken取消令牌)2024-10-21 06:21:49 at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 令牌)2024-10-21 06:21:49 在 System.Net.Sockets.Socket.g__WaitForConnectWithCancellation|285_0(AwaitableSocketAsyncEventArgs saea,ValueTask connectTask,CancellationToken CancellationToken) 2024-10-21 06:21:49 于 System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(字符串主机, Int32 端口、HttpRequestMessage 初始请求、布尔异步、 CancellationToken CancellationToken) 2024-10-21 06:21:49 --- 结束 内部异常堆栈跟踪 --- 2024-10-21 06:21:49 at System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(字符串主机, Int32 端口、HttpRequestMessage 初始请求、布尔异步、 CancellationToken(取消令牌)2024-10-21 06:21:49 at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage 请求、布尔异步、CancellationToken(取消令牌) 2024-10-21 06:21:49 于 System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage 请求、布尔异步、CancellationToken(取消令牌) 2024-10-21 06:21:49 于 System.Net.Http.HttpConnectionPool.AddHttp11ConnectionAsync(QueueItem 队列项目) 2024-10-21 06:21:49 at System.Threading.Tasks.TaskCompletionSourceWithCancellation`1.WaitWithCancellationAsync(CancellationToken 取消令牌)2024-10-21 06:21:49 at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage 请求、布尔异步、布尔 doRequestAuth、CancellationToken 取消令牌)2024-10-21 06:21:49 at System.Net.Http.DiagnosticsHandler.SendAsyncCore(HttpRequestMessage 请求、布尔异步、CancellationToken(取消令牌) 2024-10-21 06:21:49 于 Ocelot.Requester.TimeoutDelegatingHandler.SendAsync(HttpRequestMessage 请求,CancellationToken取消令牌)2024-10-21 06:21:49
在 Ocelot.Requester.MessageInvokerHttpRequester.GetResponse(HttpContext httpContext) 在 ResponderMiddleware 中发现错误。设定错误 响应请求路径:/crm/customers/get-page,请求方法: 发布'
我已经更改了 ocelot.json:
{
"Routes": [
// CRM routes
{
"DownstreamPathTemplate": "/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 8001
}
],
"UpstreamPathTemplate": "/crm/{everything}",
"UpstreamHttpMethod": [ "GET", "POST" ]
}
//other mappings
],
"GlobalConfiguration": {
"BaseUrl": "http://localhost:5000;https://localhost:5001"
}
}
至:
{
"Routes": [
// CRM routes
{
"DownstreamPathTemplate": "/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "crm",
"Port": 8001
}
],
"UpstreamPathTemplate": "/crm/{everything}",
"UpstreamHttpMethod": [ "GET", "POST" ]
}
// other mappings.
"GlobalConfiguration": {
"BaseUrl": "http://localhost:5000;https://localhost:5001"
}
}
为什么会无法连接到docker服务。它们在邮递员那里都可以正常工作。
尝试在 Docker Compose 文件中添加
network
并确保所有服务都使用同一网络,或更新 DownstreamHostAndPorts
以使用容器 IP 而不是主机名。