DeveloperAkash
DeveloperAkash

Reputation: 11

Connection refused in ocelot api gateway requests via consul for service discovery in docker containerize applications

I am developing a project with microservices architecture, Ocelot is being used as API gateway, Consul is for service discovery and application will be containerize with docker. All the services are running on docker containers on common network and registered on consul with below details. Consul Details Id: UserSettingsService-9100 ServiceName: UserSettingsService Address: api.usersettings.httpapi.host Port: 8080

API gateway is also registered on consul with below details- Id: apiGateway-9100 ServiceName: apiGatewayService Address: api.gateway Port: 8080

For both discovery address is same as DiscoveryAddress=http://consul:8500

Docker container Details UserSettingsService Host Port : 8001 Container port : 8080

apiGatewayService Host Port : 8002 Container port : 8080

Ocelot.json

    {
     "Routes": [
        {
          "DownstreamPathTemplate": "/api/Home/testgateway",
          "DownstreamScheme": "http",
          "ServiceName": "UserSettingsService",
          "UpstreamPathTemplate": "/UserService/testgateway",
          "UpstreamHttpMethod": [ "GET", "POST" ],
          "LoadBalancerOptions": {
            "Type": "RoundRobin"
          },
          "UseServiceDiscovery": true
        }
       ],
      "GlobalConfiguration": {
        "ServiceDiscoveryProvider": {
          "Host": "consul",
          "Port": 8500,
          "Type": "Consul"
        }
      }
    }

Docker-compose.yml

    services:
      consul:
        image: hashicorp/consul:latest
        command: consul agent -dev -log-level=warn -ui -client=0.0.0.0
        hostname: consul
        container_name: consul
        networks:
          - common_network
        ports:
          - "8500:8500"

      api.usersettings.httpapi.host:
        image: ${DOCKER_REGISTRY-}apiusersettingshttpapihost
        build:
          context: .
          dockerfile: services/UserSettings/API.UserSettings.HttpApi.Host/Dockerfile
        networks:
          - common_network
        depends_on:
          - consul

      api.gateway:
        image: ${DOCKER_REGISTRY-}apigateway
        build:
          context: .
          dockerfile: Gateways/ApiGateway/API.Gateway/Dockerfile
        networks:
          - common_network
        depends_on:
          - consul

    networks:
      common_network:
        driver: bridge

Docker-compose.override.yml

api.usersettings.httpapi.host:
        environment:
          - ASPNETCORE_ENVIRONMENT=Development
          - ConsulConfig__Id=UserSettingsService-9100
          - ConsulConfig__Name=UserSettingsService
          - ConsulConfig__DiscoveryAddress=http://consul:8500
          - ConsulConfig__Address=api.usersettings.httpapi.host
          - ConsulConfig__Port=8080
          - ConsulConfig__HealthCheckEndPoint=healthcheck
        ports:
          - "8001:8080"
        volumes:
          - ${APPDATA}/Microsoft/UserSecrets:/home/app/.microsoft/usersecrets:ro
          - ${APPDATA}/ASP.NET/Https:/home/app/.aspnet/https:ro
        depends_on:
          - consul

      api.gateway:
        environment:
          - ASPNETCORE_ENVIRONMENT=Development
          - ConsulConfig__Id=apiGateway-9100
          - ConsulConfig__Name=apiGatewayService
          - ConsulConfig__DiscoveryAddress=http://consul:8500
          - ConsulConfig__Address=api.gateway
          - ConsulConfig__Port=8080
          - ConsulConfig__HealthCheckEndPoint=healthcheck
        ports:
          - "8002:8080"
        volumes:
          - ${APPDATA}/Microsoft/UserSecrets:/home/app/.microsoft/usersecrets:ro
          - ${APPDATA}/ASP.NET/Https:/home/app/.aspnet/https:ro
        depends_on:
          - consul

With these configurations I am trying to access http://localhost:8002/UserService/testgateway it is giving error "connection refused(consul:8080)" while i can directly access the service on http://localhost:8001/api/Home/testgateway Even if try with DownstreamHostAndPorts instead of service name It works fine.

    "DownstreamHostAndPorts": [
            {
              "Host": "api.usersettings.httpapi.host",
              "Port": 8080
            }
          ],

Let me know if you need any other specific information.

I have tried changing localhost in place of consul under ServiceDiscoveryProvider. Nothing worked for me.

Upvotes: 1

Views: 126

Answers (0)

Related Questions