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

o.s.c.l.core.RoundRobinLoadBalancer : No servers available for service: data-service-1 #753

Closed jiehuaou closed 3 years ago

jiehuaou commented 3 years ago

Describe the bug ... o.s.c.l.core.RoundRobinLoadBalancer : No servers available for service: data-service-1 ... spring cloud load-balancer does not work in spring boot 2.5.x with Spring cloud version 2020.0.X spring cloud load-balancer can not fetch service instance list

Sample 1) I have a service "data-service-1" running and registered in consul which I can verify it by checking http://localhost:8500/ui, 2) now I launch my client service which internally invoke "data-service-1" , with sample code here:

   @Autowired
    RestTemplate restTemplate;

    @GetMapping("/load-id")
    public String discoveryId() throws ServiceUnavailableException {
        String data = "init";
        try {
            ResponseEntity<String> resp = restTemplate.getForEntity("http://data-service-1/id", String.class);
            data = resp.getBody();
        }catch (Exception ex){
            ///
        }
        return data;
    }
//////
@LoadBalancerClient(value = "data-service-1", configuration = LoadBalancerConfiguration.class)
@Configuration
public class RestConfig {
    @LoadBalanced
    @Bean
    public RestTemplate loadbalancedRestTemplate() {
        return new RestTemplate();
    }
}
//////
public class LoadBalancerConfiguration {
    @Bean
    public ServiceInstanceListSupplier discoveryClientServiceInstanceListSupplier(
            ConfigurableApplicationContext context) {
        return ServiceInstanceListSupplier.builder()
                .withDiscoveryClient()
                .withHealthChecks()
                .withCaching()
                .build(context);
    }
}

the load-balancer can work well in spring boot 2.3.3 with Spring cloud version Hoxton.SR12, even can handle the case when some instance are Up or down, such as re-fetch latest UP instance then route call to it,

but when I change the version to spring boot 2.5.4 with Spring cloud version 2020.0.3 , it just simply gives warning :

2021-09-16 11:16:15.385 WARN 21040 --- [ctor-http-nio-1] o.s.c.l.core.RoundRobinLoadBalancer : No servers available for service: data-service-1

here is sample code : https://github.com/jiehuaou/discovery-client-lb

thanks, Albert

jiehuaou commented 3 years ago

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version> 
 <!--       <version>2.3.3.RELEASE</version>--> 
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>client-lb</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>client-lb</name>
    <description>Demo LB Client for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    <spring-cloud.version>2020.0.3</spring-cloud.version>
<!--        <spring-cloud.version>Hoxton.SR12</spring-cloud.version>-->
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-netflix-hystrix</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

please note the version:

< version >2.5.4< / version > < ! -- < version>2.3.3.RELEASE< / version> -- >

and

< spring-cloud.version>2020.0.3</spring-cloud.version > < ! -- Hoxton.SR12</spring-cloud.version> -- >

jiehuaou commented 3 years ago

application.yml

spring:
  cloud:
...
    loadbalancer:
      health-check:
        refetch-instances: true
        refetch-instances-interval: 2s
#        repeat-health-check: true
#        interval: 10s
      ribbon:
        enabled: false
      cache:
        ttl: 2s
        enabled: true
wabixia commented 2 years ago

I just meet this issue and search solution, could you tell me how did you solve this issue? thanks