通过启用功能区的客户端调用微服务时失败(没有尤里卡服务发现)
Failure while calling a microservice via ribbon enabled client(Without eureka service discovery)
我正在尝试通过功能区启用客户端 (ribbon-client) 调用 'microservice'(microservice-producer),但它给我一个错误。
java.lang.IllegalStateException: No instances available for employee-microservice
我正在遵循客户端负载平衡的官方 spring.io link(https://spring.io/guides/gs/client-side-load-balancing/),并且我还遵循此 link 给出的所有规范。
你可以在我的 GitHub 地址看到代码:
(https://github.com/vickygupta0017/microservice-ribbon).
我不确定我遗漏了什么或做错了什么,有人可以帮我吗?
Ribbon Client 的配置不正确,我在 Ribbon Client 中通过以下更改成功执行了代码,在客户端调用时它抛出了 Null Pointer Exception,因为 Ribbon Client 的几个参数没有成功设置,我可以请参阅 EmployeeConfiguration Class 中缺少 @Configuration,因此它将如何初始化 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;
}
}
}
在不使用 eureka 服务器来识别服务器是否可达的情况下,ribbon 客户端使用 "RibbonConfiguration" - pingUrl 参数。
默认情况下,它是空字符串,这意味着它将在没有任何上下文的情况下 ping 服务器列表以获取服务器是否可达的答案。
所以在这里你可以做两件事。
创建绑定到服务器“/”的根上下文的服务并发送肯定响应。
@RequestMapping(value = "/")
public String status(HttpServletRequest request) {
return "";
}
或者用相关的"service-name".
更新Ribbon客户端配置(EmployeeConfiguration)和returnPingUrl
@Bean
public IPing ribbonPing(IClientConfig config) {
return new PingUrl(false,"/employees");
}
选择第一个,因为它将解决查看服务器是否可达的基本目的。
参考:https://spring.io/guides/gs/client-side-load-balancing/
Our IPing is a PingUrl, which will ping a URL to check the status of each server. Say Hello has, as you’ll recall, a method mapped to the / path; that means that Ribbon will get an HTTP 200 response when it pings a running Say Hello server. The IRule we set up, the AvailabilityFilteringRule, will use Ribbon’s built-in circuit breaker functionality to filter out any servers in an “open-circuit” state: if a ping fails to connect to a given server, or if it gets a read failure for the server, Ribbon will consider that server “dead” until it begins to respond normally.
我正在尝试通过功能区启用客户端 (ribbon-client) 调用 'microservice'(microservice-producer),但它给我一个错误。
java.lang.IllegalStateException: No instances available for employee-microservice
我正在遵循客户端负载平衡的官方 spring.io link(https://spring.io/guides/gs/client-side-load-balancing/),并且我还遵循此 link 给出的所有规范。 你可以在我的 GitHub 地址看到代码: (https://github.com/vickygupta0017/microservice-ribbon).
我不确定我遗漏了什么或做错了什么,有人可以帮我吗?
Ribbon Client 的配置不正确,我在 Ribbon Client 中通过以下更改成功执行了代码,在客户端调用时它抛出了 Null Pointer Exception,因为 Ribbon Client 的几个参数没有成功设置,我可以请参阅 EmployeeConfiguration Class 中缺少 @Configuration,因此它将如何初始化 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;
}
}
}
在不使用 eureka 服务器来识别服务器是否可达的情况下,ribbon 客户端使用 "RibbonConfiguration" - pingUrl 参数。 默认情况下,它是空字符串,这意味着它将在没有任何上下文的情况下 ping 服务器列表以获取服务器是否可达的答案。 所以在这里你可以做两件事。
创建绑定到服务器“/”的根上下文的服务并发送肯定响应。
@RequestMapping(value = "/") public String status(HttpServletRequest request) { return ""; }
或者用相关的"service-name".
更新Ribbon客户端配置(EmployeeConfiguration)和returnPingUrl@Bean public IPing ribbonPing(IClientConfig config) { return new PingUrl(false,"/employees"); }
选择第一个,因为它将解决查看服务器是否可达的基本目的。
参考:https://spring.io/guides/gs/client-side-load-balancing/
Our IPing is a PingUrl, which will ping a URL to check the status of each server. Say Hello has, as you’ll recall, a method mapped to the / path; that means that Ribbon will get an HTTP 200 response when it pings a running Say Hello server. The IRule we set up, the AvailabilityFilteringRule, will use Ribbon’s built-in circuit breaker functionality to filter out any servers in an “open-circuit” state: if a ping fails to connect to a given server, or if it gets a read failure for the server, Ribbon will consider that server “dead” until it begins to respond normally.