通过功能区启用客户端调用微服务时失败(没有eureka服务发现)

时间:2018-02-19 10:11:34

标签: java microservices netflix-ribbon

我试图通过功能区启用客户端(功能区客户端)来调用微服务器(微服务生产商),但它给了我一个错误。

  

java.lang.IllegalStateException:没有可用于employee-microservice的实例

我正在关注客户端负载平衡(https://spring.io/guides/gs/client-side-load-balancing/)的官方spring.io链接,我也遵循此链接中给出的所有规范。 你可以在我的GitHub地址看到代码:  (https://github.com/vickygupta0017/microservice-ribbon)。

我不确定我错过了什么或做错了,有人可以帮帮我吗?

2 个答案:

答案 0 :(得分:1)

如果未使用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");
    }
    
  3. 转到第一个,因为它将解决查看服务器是否可访问的基本目的。

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

      

    我们的IPing是一个PingUrl,它将ping一个URL以检查每个服务器的状态。如你所知,Say Hello有一个映射到/ path的方法;这意味着Ribbon在ping正在运行的Say Hello服务器时将获得HTTP 200响应。我们设置的IRule,AvailabilityFilteringRule,将使用Ribbon的内置断路器功能来过滤掉处于“开路”状态的任何服务器:如果ping无法连接到给定服务器,或者它是否读取失败对于服务器,Ribbon会认为该服务器“死”,直到它开始正常响应。

答案 1 :(得分:0)

功能区客户端的配置不正确,我已成功执行了代码,并在Ribbon Client中进行了以下更改,在客户端调用时它正在抛出Null Pointer Exception,因为Ribbon Client的几个参数未成功设置,我可以看到@ 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;
        }

    }
}