Skip to content

BUG in Ocelot(.Consul) > 23.0.0 is ignoring ServiceAddress and instead building the downstream URL from the Consul node name #2311

@cda963

Description

@cda963

The following error only happens in version > 23.0.0; Before upgrading from 23.0.0 everything was working fine. Nothing else changed in the code or configuration.

I've got Consul running in a Docker container:

  Consul:
    image: hashicorp/consul
    container_name: consul
    ports:
      - "8500:8500"
      - "8600:8600/udp"
    command: >
      consul agent -server -bootstrap-expect=1 -ui -node=consul-server 
      -client=0.0.0.0 -bind=0.0.0.0 -data-dir=/consul/data
    volumes:
      - ./data/consul:/consul/data
    networks:
      - myNetwork

And in the same Docker network I've got my API gateway using Ocelot and Ocelot.Provider.Consul.

This is my ocelot.json excerpt:

{
  "Routes":
  [
		{
			"UpstreamHeaderTransform":
			{
				"SkipTenantTokenMiddleware": "true"
			},
			"UpstreamPathTemplate": "/api/accounts/health",
			"DownstreamPathTemplate": "/health",
			"DownstreamScheme": "http",
			"ServiceName": "accounts-api",
			"LoadBalancerOptions":
			{
				"Type": "RoundRobin"
			},
			"UseServiceDiscovery": true
		}
  ],
  "GlobalConfiguration":
  {
    "RequestIdKey": "OcelotRequestId",
		"ServiceDiscoveryProvider":
		{
			"Type": "Consul",
			"Host": "consul",
			"Port": 8500,
			"Scheme": "http",
			"ConfigurationKey": "InternalConfiguration"
		}
  }
}

The problem is in versions > 23.0.0, when I make any HTTP request to my accounts-api service, Ocelot seems to resolve the wrong address; the logs look like this:

Upstream URL path: /api/accounts/health

[05:43:33 DBG] RequestId: 0HNFBENTU48LS:00000001, PreviousRequestId: -

Downstream templates: /health

[05:43:33 INF] RequestId: 0HNFBENTU48LS:00000001, PreviousRequestId: -

EnableEndpointEndpointRateLimiting is not enabled for downstream path: /health

[05:43:33 INF] RequestId: 0HNFBENTU48LS:00000001, PreviousRequestId: -

No authentication needed for path: /api/accounts/health

[05:43:33 INF] RequestId: 0HNFBENTU48LS:00000001, PreviousRequestId: -

No authorization needed for upstream path: /api/accounts/health

[05:43:33 INF] RequestId: 0HNFBENTU48LS:00000001, PreviousRequestId: -

The UseServiceDiscovery mode of the route ':accounts-api:^(?i)/api/accounts/health$' is enabled.

[05:43:33 INF] RequestId: 0HNFBENTU48LS:00000001, PreviousRequestId: -

Getting service discovery provider of Type 'Consul'...

[05:43:33 DBG] RequestId: 0HNFBENTU48LS:00000001, PreviousRequestId: -

Consul Provider: Found total 6 service entries for 'accounts-api' service.

[05:43:33 DBG] RequestId: 0HNFBENTU48LS:00000001, PreviousRequestId: -

Consul Provider: Found total 1 catalog nodes.

[05:43:33 DBG] RequestId: 0HNFBENTU48LS:00000001, PreviousRequestId: -

Downstream URL: [http://consul-server/health⁠](http://consul-server/health%E2%81%A0)

[05:43:45 INF] RequestId: 0HNFBENTU48LS:00000001, PreviousRequestId: -

102 unknown status code of request URI:

[05:43:45 DBG] RequestId: 0HNFBENTU48LS:00000001, PreviousRequestId: -

IHttpRequester returned an error, setting pipeline error

[05:43:45 WRN] RequestId: 0HNFBENTU48LS:00000001, PreviousRequestId: -

ResponderMiddleware found 1 error ->

ConnectionToDownstreamServiceError: Error connecting to downstream service, exception: System.Net.Http.HttpRequestException: Connection refused (consul-server:80)

 ---> System.Net.Sockets.SocketException (111): Connection refused

Basically the downstream url is pointing to the Consul node ("consul-server") instead of the actual service url ("accounts-api").


For reference these are the logs before upgrading from 23.0.0:

[06:20:28 DBG] requestId: 0HNFBFCHUFTLI:00000001, previousRequestId: No PreviousRequestId, message: 'ocelot pipeline started'

[06:20:28 DBG] requestId: 0HNFBFCHUFTLI:00000001, previousRequestId: No PreviousRequestId, message: 'Upstream url path is /api/accounts/health'

[06:20:28 DBG] requestId: 0HNFBFCHUFTLI:00000001, previousRequestId: No PreviousRequestId, message: 'downstream templates are /health'

[06:20:28 INF] requestId: 0HNFBFCHUFTLI:00000001, previousRequestId: No PreviousRequestId, message: 'EndpointRateLimiting is not enabled for /health'

[06:20:28 INF] requestId: 0HNFBFCHUFTLI:00000001, previousRequestId: No PreviousRequestId, message: 'No authentication needed for path '/api/accounts/health'.'

[06:20:28 INF] requestId: 0HNFBFCHUFTLI:00000001, previousRequestId: No PreviousRequestId, message: '/health route does not require user to be authorized'

[06:20:28 INF] requestId: 0HNFBFCHUFTLI:00000001, previousRequestId: No PreviousRequestId, message: 'The UseServiceDiscovery mode of the route '^(?i)/api/accounts/health$' is enabled.'

[06:20:28 INF] requestId: 0HNFBFCHUFTLI:00000001, previousRequestId: No PreviousRequestId, message: 'Getting service discovery provider of Type 'Consul'...'

[06:20:28 DBG] requestId: 0HNFBFCHUFTLI:00000001, previousRequestId: No PreviousRequestId, message: 'Downstream url is [http://accounts-api/health'⁠](http://accounts-api/health')

[06:20:48 INF] requestId: 0HNFBFCHUFTLI:00000001, previousRequestId: No PreviousRequestId, message: '200 (OK) status code, request uri: [http://accounts-api/health'⁠](http://accounts-api/health')

Metadata

Metadata

Assignees

No one assigned

    Labels

    ConsulService discovery by ConsulService DiscoveryOcelot feature: Service DiscoveryquestionInitially seen a question could become a new feature or bug or closed ;)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions