.net - Ocelot - Error connecting to downstream service - Connection refused - Stack Overflow

i am trying to connect .Net web API using ocelot gateway. Routing works fine when i run ocelot and API

i am trying to connect .Net web API using ocelot gateway. Routing works fine when i run ocelot and API respectively without docker.

With docker desktop (using 4.38.0) it shows 502 in browser with Error connecting to downstream service, exception: System.Net.Http.HttpRequestException: Connection refused (localhost:5001)

My docker compose yaml and Ocelot.json is as below. Both have included port 5001 and same has been exposed from product service's docker file.

I have tried changing ports, exposing them as shown below. Added inbound rule for 5001 with no luck.

TIA, if you can assist on what am i missing / needs change.

docker-compose.yml

services:
  ocelotgateway:
    image: ${DOCKER_REGISTRY-}ocelotgateway
    build:
      context: .
      dockerfile: OcelotGateway/Dockerfile

  productservice:
    image: ${DOCKER_REGISTRY-}productservice
    build:
      context: ProductService
      dockerfile: Dockerfile
    ports:
      - "5001:5001"

Ocelot.json

{
    "Routes": [
        {
            "DownstreamPathTemplate": "/api/products",
            "DownstreamScheme": "http",
            "DownstreamHostAndPorts": [
                {
                    "Host": "localhost",
                    "Port": 5001
                }
            ],
            "UpstreamPathTemplate": "/products",
            "UpstreamHttpMethod": [ "Get" ]
        },
    ]
}

i am trying to connect .Net web API using ocelot gateway. Routing works fine when i run ocelot and API respectively without docker.

With docker desktop (using 4.38.0) it shows 502 in browser with Error connecting to downstream service, exception: System.Net.Http.HttpRequestException: Connection refused (localhost:5001)

My docker compose yaml and Ocelot.json is as below. Both have included port 5001 and same has been exposed from product service's docker file.

I have tried changing ports, exposing them as shown below. Added inbound rule for 5001 with no luck.

TIA, if you can assist on what am i missing / needs change.

docker-compose.yml

services:
  ocelotgateway:
    image: ${DOCKER_REGISTRY-}ocelotgateway
    build:
      context: .
      dockerfile: OcelotGateway/Dockerfile

  productservice:
    image: ${DOCKER_REGISTRY-}productservice
    build:
      context: ProductService
      dockerfile: Dockerfile
    ports:
      - "5001:5001"

Ocelot.json

{
    "Routes": [
        {
            "DownstreamPathTemplate": "/api/products",
            "DownstreamScheme": "http",
            "DownstreamHostAndPorts": [
                {
                    "Host": "localhost",
                    "Port": 5001
                }
            ],
            "UpstreamPathTemplate": "/products",
            "UpstreamHttpMethod": [ "Get" ]
        },
    ]
}
Share Improve this question edited Mar 9 at 22:54 chint asked Mar 9 at 5:43 chintchint 451 silver badge8 bronze badges 1
  • updated Host with service name within "DownstreamHostAndPorts": [ { "Host": "productservice", "Port": 5001 }" still same issue. – chint Commented Mar 9 at 10:08
Add a comment  | 

1 Answer 1

Reset to default 0

After below changes, I am able to access individual API and also via ocelot gateway using docker.

- Removed ports definition from docker-compose.yml
- In ocleot.json included service name as host and port as 8080
- Exposed only 8080 as port from API's docker file

Final ocelot.json

{
    "Routes": [
        {
            "DownstreamPathTemplate": "/api/products",
            "DownstreamScheme": "http",
            "DownstreamHostAndPorts": [
                {
                    "Host": "productservice",
                    "Port": 8080
                }
            ],
            "UpstreamPathTemplate": "/products",
            "UpstreamHttpMethod": [ "Get" ]
        },
    ]
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744877077a4598611.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信