eureka displayed with the service but the problem No instances available for service-hello ribbon display by calling

First, the problem

Take eureka cluster, the client calls the service through the Ribbon, Ribbon end reported the following exception

?
1
2
3
4
5
6
7
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

Spring Cloud chaotic version, the version associated quote is chaos, eventually I switched to <spring-cloud.version> Greenwich.SR1 </spring-cloud.version> abnormalities: No instances available for SERVICE-HI

Second, look for answers 

Strange answer online

1, Spring Cloud's official website, RestTemplate bean configuration add load balancing notes @LoadBalanced, I added

?
1
2
3
4
5
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
   return new RestTemplate();
}

The results are still the same error invalid

2, service name, the name can not be accessed underlined:

My name is "SERVICE-HI" does not exist in itself underscores, so I do not consider this.

3, the host name is not in the system hosts file configured, ping nowhere you service name:

Pull it answers why with host, load multiple machines so that the host name pointing to who? Does not consider this answer

Third, the analysis of the problem

Baidu less than he analyze the causes and find ribbon server is not registered in eureka server

Analysis principle: My client service "SERVICE-HI" has been successfully registered in the eureka server, if the ribbon eureka server server is not registered, it will not know the existence of "SERVICE-HI" client service and where it exists , then the conclusion is that, because the ribbon server is not registered success in eureka server, the host can not recognize the name.

Fourth, to solve the problem

Profiles

?
1
2
3
4
5
6
7
8
9
eureka:
  client:
   serviceUrl:
    defaultZone: http://localhost:8761/eureka/
server:
  port: 8764
spring:
  application:
   name: service-ribbon

Import dependence

?
1
2
3
4
5
6
7
8
9
10
< 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 >

The main program Notes

?
1
2
3
4
5
6
7
8
9
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceRibbonApplication {
   
   public static void main(String[] args) {   
     SpringApplication.run( ServiceRibbonApplication. class , args );
   }
 
}

There are problems, eventually found @EnableDiscoveryClient label can not be registered to the registry, Baidu @EnableDiscoveryClient, to the conclusion that

@EnableDiscoveryClient and @EnableEurekaClient as to allow the registry to discover, scan-to-change service, different points: @EnableEurekaClient only applies to Eureka as a registration center, @ EnableDiscoveryClient Eureka or may be other (consul, zookeeper, etc.) registry.

Do not analyze the specific reasons, where the first switch directly to @EnableEurekaClient comments

@EnableEurekaClient in which the bag is simply the presence of the same fans, different versions of the spring cloud in position, I use Greenwich.SR1, we need to introduce the following package

?
1
2
3
4
< dependency >
   < groupId >org.springframework.cloud</ groupId >
   < artifactId >spring-cloud-starter-netflix-eureka-client</ artifactId >
</ dependency >

Modify the main program Notes

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
 
 
@SpringBootApplication
@EnableEurekaClient
@EnableHystrix //我开启了段容器
public class ServiceRibbonApplication {
 
   public static void main(String[] args) {
     SpringApplication.run( ServiceRibbonApplication. class , args );
   }
 
}

Here mention that in the middle of containers next Greenwich.SR1 bread

?
1
2
3
4
< dependency >
   < groupId >org.springframework.cloud</ groupId >
   < artifactId >spring-cloud-starter-netflix-hystrix</ artifactId >
</ dependency >

Restart ribbon, find console input

?
1
2
3
4
5
6
7
8
9
10
11
12
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 to access test successfully! ! !

V. Accessories: Greenwich.SR1 version dependent common

There are a lot of problems because of the introduction of different versions lead to incorrect rely listed here Greenwich.SR1 version commonly rely on, here do not need to specify the version number

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
< 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 >
   <!-- 段容器 -->
   < 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 >

转自https://www.jb51.net/article/163154.htm

Guess you like

Origin www.cnblogs.com/cangqinglang/p/11704475.html