一,問題java
採起eureka集羣、客戶端經過Ribbon調用服務,Ribbon端報下列異常web
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版本比較亂,版本關聯引用更是亂,最終我切換到 <spring-cloud.version>Greenwich.SR1</spring-cloud.version> 異常爲:No instances available for SERVICE-HIspring
2、尋找答案 瀏覽器
網上答案千奇百怪tomcat
1,Spring Cloud 官網,RestTemplate bean配置中添加負載均衡註解@LoadBalanced,我添加了服務器
@Bean @LoadBalanced public RestTemplate getRestTemplate(){ return new RestTemplate(); }
結果無效仍然同樣報錯app
2,訪問的服務名名稱不能有下劃線: 負載均衡
個人名稱是「SERVICE-HI」自己就不存在下劃線,因此不考慮這條。
3,主機名稱沒在系統文件hosts中配置,ping不通你服務名:spring-boot
很扯的答案,爲何要配host,負載多臺機器讓主機名指向誰?不考慮此答案
三,分析問題測試
百度不到,本身分析緣由,發現ribbon服務器沒有註冊到 eureka server中
分析原理:個人客戶端服務「SERVICE-HI」已經成功註冊到eureka server中了,若是ribbon服務器不在eureka server中註冊,是不會知道客戶端服務「SERVICE-HI」的存在以及它存在的位置,那麼結論就是,由於ribbon服務器沒有在eureka server中註冊成功,因此不能識別主機名稱。
四,解決問題
配置文件
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8764
spring:
application:
name: service-ribbon
依賴導入
<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>
主程序註釋
@SpringBootApplication @EnableDiscoveryClient public class ServiceRibbonApplication { public static void main(String[] args) { SpringApplication.run( ServiceRibbonApplication.class, args ); } }
有問題,最終發現@EnableDiscoveryClient標籤沒法註冊到註冊中心,百度@EnableDiscoveryClient,獲得的結論是
@EnableDiscoveryClient和@EnableEurekaClient同樣,可以讓註冊中心可以發現,掃描到改服務,不一樣點:@EnableEurekaClient只適用於Eureka做爲註冊中心,@EnableDiscoveryClient 能夠是Eureka或其餘(consul、zookeeper等)註冊中心。
具體緣由不去分析,這裏先直接切換爲@EnableEurekaClient註釋
@EnableEurekaClient在哪一個包裏簡直是迷同樣的存在,不一樣版本的spring cloud 中位置不一樣,我使用Greenwich.SR1,須要引入下面的包
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
修改主程序註釋
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 ); } }
這裏提一句在 Greenwich.SR1中段容器在下面包中
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
從新啓動ribbon,發現控制檯輸入
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)
查看Eureka
瀏覽器測試訪問成功!!!
五,附件:Greenwich.SR1 版中經常使用依賴
有好多問題都是由於 不一樣版本中引入不正確的依賴致使,這裏列出 Greenwich.SR1 版中經常使用依賴,這裏都不須要指定版本號
<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>