Skip to content

Look up services in their configured datacenter - #1057

Open
officialasishkumar wants to merge 1 commit into
spring-cloud:mainfrom
officialasishkumar:fix/712-discovery-datacenters
Open

officialasishkumar wants to merge 1 commit into
spring-cloud:mainfrom
officialasishkumar:fix/712-discovery-datacenters

Conversation

@officialasishkumar

Copy link
Copy Markdown
Contributor

Problem

The discovery docs say that spring.cloud.consul.discovery.datacenters sets the datacenter a service is looked up in:

spring.cloud.consul.discovery.datacenters.STORES=dc-west

The Ribbon ConsulServerList used to read this map. Since the move to Spring Cloud LoadBalancer, nothing reads it. Services in another datacenter can't be found through the DiscoveryClient or the load balancer, and the property does nothing.

Solution

ConsulDiscoveryClient now sets the configured datacenter (dc) on the QueryParams it sends for each service, next to the configured consistency mode. This matches what the Ribbon server list did with new QueryParams(datacenter, consistencyMode).

getInstances, getAllInstances, ConsulReactiveDiscoveryClient and Spring Cloud LoadBalancer all go through this code, so all of them now find services in their configured datacenter. Services without an entry are still looked up in the agent's datacenter.

Testing

  • ConsulDiscoveryClientDatacentersTests is new. It uses MockRestServiceServer behind the real HTTP service proxy, like ConsulDiscoveryClientAllInstancesTests, and covers:

    • getInstances sending dc for a mapped service
    • no dc for a service without an entry
    • dc combined with the stale consistency mode
    • getAllInstances using each service's own datacenter
    • the reactive discovery client

    On main, the four tests that expect dc fail.

  • I also ran an end-to-end check against two Consul 1.22 agents, dc-east and dc-west, joined over the WAN, using a temporary @SpringBootTest that is not part of this PR. I registered stores only in dc-west. The application connected to the dc-east agent with spring.cloud.consul.discovery.datacenters.stores=dc-west:

    • With this change, DiscoveryClient.getInstances("stores") returned 10.1.2.3:9090 and the Spring Cloud LoadBalancer picked that instance.
    • On main, the discovery client returned no instances and the load balancer had nothing to pick.
  • ConsulDiscoveryClientAllInstancesTests, Checkstyle and spring-javaformat pass.

Fixes #712

The discovery docs say that spring.cloud.consul.discovery.datacenters
maps a service to the datacenter it is looked up in, for example
spring.cloud.consul.discovery.datacenters.STORES=dc-west. The Ribbon
server list used to read this map, but nothing has read it since the
move to Spring Cloud LoadBalancer. Services in other datacenters could
therefore not be found, and the property did nothing.

ConsulDiscoveryClient now sets the configured datacenter on the
QueryParams it sends for a service, next to the configured consistency
mode, the same way the Ribbon server list did. getInstances,
getAllInstances, the reactive discovery client and Spring Cloud
LoadBalancer all go through this code, so all of them now find services
in their configured datacenter. Services without an entry are still
looked up in the agent's datacenter.

Fixes spring-cloudgh-712

Signed-off-by: Asish Kumar <officialasishkumar@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for spring cloud loadbalancer and datacenter

2 participants