Load balancer does not have available server for client已解決

目錄

普遍的解決辦法

異常解析

SpringCloud服務消費搭建

服務端

消費端

註冊中心

開啓多個

可能遇到報錯


普遍的解決辦法

Load balancer does not have available server for client

首先,這個錯誤網上搜了很多文章,提供辦法最多的是在配置文件里加入一下代碼

ribbon:
  eureka:
    enabled: false

以及

## 這個BaseRemote是加了@FeignClient註解的類
BaseRemote:
  ribbon:
    ## 服務提供者的地址,不是服務註冊中心的地址
    listOfServers: http://localhost:8086

  但對於剛接觸springcloud的孩子來說,出現錯誤根本不知道爲什麼,不知道具體怎麼操作,相關文章普遍針對作者自己遇到的問題解決,說的很籠統,我跟着走了很多彎路,一直也沒搞懂到底是哪個地方寫哪個名字。

異常解析

直說出現Load balancer does not have available server for client這個錯誤的情況比較多,意思是負載均衡找不到那個服務,後面跟着服務名,配置錯了,某一個名字寫錯了都可能觸發,只要讓他找不到就報這個錯。上述解決辦法適合一部分情況,手動配置多個服務以及負載均衡策略等,我現在用的是Feign做的消費端,網上還有針對其他方式搭建的解決辦法,這就不說了,下面走一遍流程。

SpringCloud服務消費搭建

springboot版本2.0.6.RELEASE   springCloud版本Finchley.SR2 可以去搜對應表,版本對不上會衝突,有找不到某個jar包的錯報出來

其他無關的省略了,就跟普通springboot一樣

服務端

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

yml配置

server:
  port: 8088
  servlet:
    context-path: /baseService

spring:
  application:
    name: base
eureka:
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://localhost:8081/eureka/

其他的不用管,普通的mvc三層,重點看controller,普通的就好

@ResponseBody
    @RequestMapping("getOne")
    public String sayhello(@RequestBody String param){
。。。
}

消費端

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


<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
server:
  port: 9000
  servlet:
    context-path: /infoService
spring:
  application:
    name: information
eureka:
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://localhost:8081/eureka/

消費者其實可以不註冊服務,他是消費的,去找服務提供者。

@FeignClient(name= "base")//服務端的名字name,也就是訪問Eureka頁面顯示的那個名字
public interface BaseRemote {

    @RequestMapping("baseService/user/getOne")//訪問的全路徑
    String sayhello1(String param);

}

服務端寫了@RequestBody,這參數名就不重要了。這樣接口就對接上了

@Autowired
BaseRemote baseRemote;

然後調用他請求服務

註冊中心

ribbon那個開啓了負載均衡

server:
  port: 8081

spring:
  application:
    name: z-eureka
ribbon:
  eureka:
    enabled: true
eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://localhost:${server.port}/eureka/

開啓多個

修改服務端模塊的端口,返回給消費端不同的標識,打成不同端口的jar包,java -jar 包名運行,Ctrl+C結束運行。用消費者請求服務者,發現默認情況三個服務被輪流調用。

可能遇到報錯

java.nio.charset.MalformedInputException: Input length = 1 mybatis綁定失敗
其中一個最容易找的原因是interface 和 xml的 namespace 對應不上
還有一種情況是application.yml的編碼問題,統一所有文件的編碼,或者根本找不到yml,jar包在離開項目目錄運行不了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章