Detailed explanation of spring cloud ribbon load balancing Shenkeng that cannot be connected through the server name

Time:2019-10-21

First, the problem.

Take the Eureka cluster and the client calls the service through the ribbon. The ribbon side reports the following exceptions


java.net.UnknownHostException: SERVICE-HI

java.lang.IllegalStateException: No instances available for SERVICE-HI

java.lang.IllegalStateException: Request URI does not contain a valid hostname: http://SERVICE-HI

com.netfix.discovery.shared.taransport.TransportException: Cannot execute request on any known server

The spring cloud version is messy, and the version association references are even messy. Finally, I switch to < spring cloud. Version >Greenwich.SR1 The exception of < / spring cloud. Version > is:No instances available for SERVICE-HI

2. Find the answer

There are so many strange answers on the Internet

1. On the spring cloud official website, add the load balancing annotation @ loadbalanced to the resttemplate bean configuration. I added


@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
  return new RestTemplate();
}

If the result is invalid, the error will still be reported.

2. The name of the accessed service name cannot be underlined:

My name is “service-hi” and there is no underline in itself, so I don’t consider this one.

3. The host name is not configured in the system file hosts. The Ping fails to work. Your service name:

It’s a lame answer. Why do you need to configure host, load multiple machines and let the host name point to whom? Do not consider this answer

III. analysis of problems

Baidu did not arrive. After analyzing the reasons, we found that the ribbon server was not registered in Eureka server.

Analysis principle: my client service “service-hi” has been successfully registered in the Eureka server. If the ribbon server is not registered in the Eureka server, it will not know the existence and location of the client service “service-hi”. The conclusion is that because the ribbon server is not successfully registered in the Eureka server, it cannot recognize the host name.

IV. problem solving

configuration file


eureka:
 client:
  serviceUrl:
   defaultZone: http://localhost:8761/eureka/
server:
 port: 8764
spring:
 application:
  name: service-ribbon

Dependency import


<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
  </dependency>
</dependencies>

Main program notes


@SpringBootApplication
@EnableDiscoveryClient
public class ServiceRibbonApplication {
  
  public static void main(String[] args) {    
    SpringApplication.run( ServiceRibbonApplication.class, args );
  }

}

There was a problem, and finally found that the @ enablediscoveryclient tag could not be registered in the registration center. Baidu @ enablediscoveryclient got the conclusion that

@Enablediscoveryclient is the same as @ enableeurekaclient, which enables the registry to discover and scan to change services. The difference is: @ enableeurekaclient is only applicable to Eureka as the registry, @ enablediscoveryclient can be Eureka or other (consumer, zookeeper, etc.) registries.

The specific reason is not analyzed. First, switch to the @ enableeurekaclient annotation.

@In which package does enableeurekaclient exist exactly like a fan? Different versions of spring cloud have different locations. I use greenwich.sr1 and need to introduce the following package


<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

Modify main program notes

import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;


@SpringBootApplication
@EnableEurekaClient
@Enablehystrix // I opened the segment container
public class ServiceRibbonApplication {

  public static void main(String[] args) {
    SpringApplication.run( ServiceRibbonApplication.class, args );
  }

}

The container in the middle section of greenwich.sr1 is in the following package.


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

Restart ribbon and discover console input


2019-06-15 13:08:06.668 INFO 14796 --- [      main] com.netflix.discovery.DiscoveryClient  : Getting all instance registry info from the eureka server
2019-06-15 13:08:06.878 INFO 14796 --- [      main] com.netflix.discovery.DiscoveryClient  : The response status is 200
2019-06-15 13:08:06.882 INFO 14796 --- [      main] com.netflix.discovery.DiscoveryClient  : Starting heartbeat executor: renew interval is: 30
2019-06-15 13:08:06.886 INFO 14796 --- [      main] c.n.discovery.InstanceInfoReplicator   : InstanceInfoReplicator onDemand update allowed rate per min is 4
2019-06-15 13:08:06.891 INFO 14796 --- [      main] com.netflix.discovery.DiscoveryClient  : Discovery Client initialized at timestamp 1560575286889 with initial instances count: 2
2019-06-15 13:08:06.894 INFO 14796 --- [      main] o.s.c.n.e.s.EurekaServiceRegistry    : Registering application SERVICE-RIBBON with eureka with status UP
2019-06-15 13:08:06.896 INFO 14796 --- [      main] com.netflix.discovery.DiscoveryClient  : Saw local status change event StatusChangeEvent [timestamp=1560575286896, current=UP, previous=STARTING]
2019-06-15 13:08:06.900 INFO 14796 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient  : DiscoveryClient_SERVICE-RIBBON/DESKTOP-FJQITE3:service-ribbon:8764: registering service...
2019-06-15 13:08:06.958 INFO 14796 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient  : DiscoveryClient_SERVICE-RIBBON/DESKTOP-FJQITE3:service-ribbon:8764 - registration status: 204
2019-06-15 13:08:06.961 INFO 14796 --- [      main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8764 (http) with context path ''
2019-06-15 13:08:06.963 INFO 14796 --- [      main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8764
2019-06-15 13:08:06.967 INFO 14796 --- [      main] cn.meylink.ServiceRibbonApplication   : Started ServiceRibbonApplication in 5.868 seconds (JVM running for 7.204)

View Eureka

Browser Test Access succeeded!!!

V. attachment: common dependencies in greenwich.sr1

Many problems are caused by incorrect dependencies introduced in different versions. The common dependencies in greenwich.sr1 are listed here. There is no need to specify the version number.

<dependencies>
  <!-- eureka client -->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  </dependency>
  <!-- eureka server -->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  </dependency>
  <! -- segment container -- >
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  </dependency>
  <!-- ribbon -->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
  </dependency>
  <!-- feign -->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
  </dependency>
  <!-- config server -->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
  </dependency>
  <!-- config client -->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
  </dependency>
  <!-- zuul -->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
  </dependency>
</dependencies>

The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.

Recommended Today

How to check the package according to the command in CentOS

In our work, we often encounter that we want to use a command machine that is not installed, but we don’t know which package the command is in (source compilation is not covered in this article). Here is a rather stupid method to help us solve this problem. Explain:Blue = command name Light green = […]