簡體   English   中英

通過啟用功能區的客戶端調用微服務時失敗(沒有eureka服務發現)

[英]Failure while calling a microservice via ribbon enabled client(Without eureka service discovery)

我試圖通過功能區啟用客戶端(功能區客戶端)來調用“微服務”(微服務生產者),但這給我一個錯誤。

java.lang.IllegalStateException:沒有適用於員工微服務的實例

我正在關注用於客戶端負載平衡的官方spring.io鏈接( https://spring.io/guides/gs/client-side-load-balancing/ ),並且我還將遵循此鏈接上給出的所有規范。 您可以在我的GitHub地址上查看代碼:( https://github.com/vickygupta0017/microservice-ribbon )。

我不確定我缺少什么或做錯了什么,有人可以幫我嗎?

如果未使用eureka服務器標識服務器是否可訪問,功能區客戶端將使用“ RibbonConfiguration”-pingUrl參數。 默認情況下,它是空字符串,這意味着它將在沒有任何上下文的情況下ping服務器列表以獲取服務器是否可訪問的答案。 所以在這里您可以做兩件事。

  1. 創建綁定到服務器“ /”的根上下文的服務,並發送肯定響應。

     @RequestMapping(value = "/") public String status(HttpServletRequest request) { return ""; } 
  2. 或更新功能區客戶端配置(EmployeeConfiguration)並返回帶有相關“服務名稱”的PingUrl。

     @Bean public IPing ribbonPing(IClientConfig config) { return new PingUrl(false,"/employees"); } 

選擇第一個,因為它將解決基本目的,以查看服務器是否可訪問。

參考: https : //spring.io/guides/gs/client-side-load-balancing/

我們的IPing是一個PingUrl,它將對URL進行ping操作以檢查每個服務器的狀態。 回想一下,您好,您好有一個映射到/路徑的方法; 這意味着Ribbon對運行中的Say Hello服務器執行ping操作時將獲得HTTP 200響應。 我們設置的IRule(可用性過濾規則)將使用Ribbon的內置斷路器功能來過濾處於“斷路”狀態的任何服務器:如果ping無法連接到給定的服務器,或者讀取失敗對於服務器,Ribbon將認為該服務器“死機”,直到它開始正常響應為止。

Ribbon Client的配置不正確,我在Ribbon Client中進行了以下更改,已成功執行了代碼,在Client調用時,由於沒有成功設置Ribbon Client的幾個參數,因此拋出Null Pointer Exception,我可以看到缺少@Configuration在EmployeeConfiguration類中,因此它將如何初始化Ribbon客戶端。

還在以下位置簽入了完整的可行代碼:

https://github.com/abhayjohri87/RibbonClientLBWithMicroServices.git

package com.ribbon.client;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.netflix.client.config.DefaultClientConfigImpl;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.ConfigurationBasedServerList;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerList;
//import com.ribbon.Employee.configuration.EmployeeConfiguration;
import com.ribbon.client.RibbonClientApplication.UserConfig;


@SpringBootApplication
@RestController
@RibbonClient(name = "employee-microservice", configuration = UserConfig.class)
public class RibbonClientApplication {

      @LoadBalanced
      @Bean
      RestTemplate restTemplate(){
        return new RestTemplate();
      }

      @Autowired
      RestTemplate restTemplate;

      @RequestMapping("/listEmployee")
      public List getEmployeeList() {
        List empList = this.restTemplate.getForObject("http://employee-microservice/employees", ArrayList.class);
        return empList;
      }

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


    @Configuration
    static class UserConfig {

        private String name = "employee-microservice";

        @Bean
        @ConditionalOnMissingBean
        public IClientConfig ribbonClientConfig() {
            DefaultClientConfigImpl config = new DefaultClientConfigImpl();
            config.loadProperties(this.name);
            return config;
        }

        @Bean
        ServerList<Server> ribbonServerList(IClientConfig config) {
            ConfigurationBasedServerList serverList = new ConfigurationBasedServerList();
            serverList.initWithNiwsConfig(config);
            return serverList;
        }

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM