spring-cloud / spring-cloud-consul

Spring Cloud Consul
http://cloud.spring.io/spring-cloud-consul/
Apache License 2.0
813 stars 541 forks source link

Consul Admin Partition Support #813

Open mmunar opened 1 year ago

mmunar commented 1 year ago

Describe the bug Using spring-cloud-starter-consul-discovery version 3.1.2. Using a combination of Spring Boot microservice, Spring Cloud Consul plugins, Consul agents on Kubernetes, and Consul Enterprise with Admin Partitions. The microservices are able to register into the correct admin partition (as seen on Consul UI). However, when using DiscoveryClient, no microservices are able to be found. It appears it is only querying the default partition. Unable to find environment variable or Java API call to specify admin partition to query in.

pom.xml:

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-consul-discovery</artifactId>
                <version>3.1.2</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-consul-config</artifactId>
                <version>3.1.2</version>
            </dependency>

Sample code:

import org.springframework.cloud.client.discovery.DiscoveryClient;
...

@Component
public class AppDiscoveryClient {

    @Autowired
    private DiscoveryClient discoveryClient;

...

    private URI serviceUri(String serviceName) {
        List<ServiceInstance> instances = discoveryClient.getInstances(serviceName);
        if (instances == null || instances.isEmpty()) {
            throw new RuntimeException("Cannot find microservice " + serviceName);
        }
        ServiceInstance serviceInstance = instances.get(0);
        return serviceInstance.getUri();
    }

...

from logs:

Registering service with consul: NewService{id='api-10-0-0-147', name='api', tags=[contextPath=/api], address='10.0.0.147', meta={secure=false}, port=8080, enableTagOverride=null, check=Check{script='null', dockerContainerID='null', shell='null', interval='10s', ttl='null', http='http://10.0.0.147:8080/api/actuator/health', method='null', header={}, tcp='null', timeout='null', deregisterCriticalServiceAfter='null', tlsSkipVerify=null, status='null', grpc='null', grpcUseTLS=null}, checks=null}

When trying to query from another Spring boot microservice, there is the exception thrown of "Cannot find microservice api". When using the Consul REST API, the entries appear when adding partition as a query parameter to the GET services.