java.lang.IllegalStateException: No instances available for 10.114.10.121
org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient.execute(BlockingLoadBalancerClient.java:84) ~[spring-cloud-loadbalancer-3.1.1.jar:3.1.1]
org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor.intercept(LoadBalancerInterceptor.java:56) ~[spring-cloud-commons-3.1.1.jar:3.1.1]
org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:93) ~[spring-web-5.3.15.jar:5.3.15]
org.springframework.http.client.InterceptingClientHttpRequest.executeInternal(InterceptingClientHttpRequest.java:77) org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:66)
org.springframework.web.client.CheckTokenRestTemplate.doExecute(CheckTokenRestTemplate.java:776)
org.springframework.web.client.CheckTokenRestTemplate.exchange(CheckTokenRestTemplate.java:660)
(BlockingLoadBalancerClient.java:84)
- // 这个http访问时进行了进一步的实现,会通过 注册中心的 服务名称寻找对应的 微服务,通过服务名称进行调用,我在这里配置IP之后,它那这个IP不能找到对应的服务实例,就报错了。
- @Override
- public
ServiceInstance choose(String serviceId, Request request) { - ReactiveLoadBalancer
loadBalancer = loadBalancerClientFactory.getInstance(serviceId); - if (loadBalancer == null) {
- return null;
- }
- Response
loadBalancerResponse = Mono.from(loadBalancer.choose(request)).block(); - if (loadBalancerResponse == null) {
- return null;
- }
- return loadBalancerResponse.getServer();
- }
处理,排除jia,这个jar对微服务的请求进行的进一步的实现,导致不可通过ip直接访问注册中心的服务接口
-
-
org.springframework.cloud -
spring-cloud-starter-loadbalancer -
情形2:
- java.lang.IllegalStateException: No instances available for www.baidu.com
-
- at org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.execute(RibbonLoadBalancerClient.java:89)
- at org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor.intercept(LoadBalancerInterceptor.java:55)
- at org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:92)
- at org.springframework.http.client.InterceptingClientHttpRequest.executeInternal(InterceptingClientHttpRequest.java:76)
- at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
- at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
- at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:723)
- at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:698)
- at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:350)
- at Test.error(Test.java:26)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
- at java.lang.reflect.Method.invoke(Method.java:497)
- at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
- at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
- at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
- at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
原因:
使用了Ribbon进行负载均衡
- @Bean
- @LoadBalanced // 负载均衡 必须使用注册中心上的服务名 不能用ip或域名
- @ConditionalOnMissingBean({RestTemplate.class})
- public RestTemplate restTemplate() {
- return new RestTemplate();
- }
原理:
- @Override
- public <T> T execute(String serviceId, LoadBalancerRequest<T> request) throws IOException {
- ILoadBalancer loadBalancer = getLoadBalancer(serviceId);
- Server server = getServer(loadBalancer);// 这里会获取服务名 因为我们是IP或域名访问 所以找不到server 抛异常
- if (server == null) {
- throw new IllegalStateException("No instances available for " + serviceId);
- }
- RibbonServer ribbonServer = new RibbonServer(serviceId, server, isSecure(server,
- serviceId), serverIntrospector(serviceId).getMetadata(server));
-
- return execute(serviceId, ribbonServer, request);
- }
因为ribbon的作用是负载均衡,那么你直接使用ip地址,那么就无法起到负载均衡的作用,因为每次都是调用同一个服务,当你使用的是服务名称的时候,他会根据自己的算法去选择具有该服务名称的服务。
链接:http://events.jianshu.io/p/2b50393de3a3