微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SpringCloud 使用 Eureka with Ribbon 无法获取服务实例

如何解决SpringCloud 使用 Eureka with Ribbon 无法获取服务实例

我尝试将 ribbo 与 Eureka 服务器和 SpringCloud 下的客户端一起使用 2020.0.3,但是在调用消费者时,总是出现异常:没有SERVICEPROVIDER的实例。

服务提供者和消费者都可以在 Eureka 服务器端找到。

根据调试日志(附后),似乎ribbon负载均衡器找不到任何服务器,甚至没有尝试从Eureka获取服务器列表。

一个有线的事情是,当 Ribbon 负载平衡器尝试使用 id 搜索服务器时,它正在使用 com.netflix.loadbalancer.ConfigurationBasedServerList,这是正常行为吗? ?

供应商:

@EnablediscoveryClient
@SpringBootApplication
public class ServiceProviderApplication {

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

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

}
package com.study.serviceprovider.web.service;

import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class HelloService {
//
//    @Autowired
//    public RestTemplate restTemplate;
    @Value("${server.port}")
    String port;

    @RequestMapping("/hi")
    public String HelloService(@RequestParam String name) {
        return "Hi," + name + ",thanks calling from " + port;
    }
}
server.port=8762
spring.application.name=ServiceProvider

eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true

消费者:

package com.study.consumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.discovery.EnablediscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

//@EnablediscoveryClient
@EnableEurekaClient
@SpringBootApplication
public class ConsumerApplication {
    @Autowired
    RestTemplateBuilder restTemplateBuilder;

    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class,args);
    }
//
//    @Bean
//    @LoadBalanced
//    public RestTemplate restTemplate() {
//        return new RestTemplate();
//    }

    @Bean
    @LoadBalanced
    public RestTemplate ribbonRestTemplate() {
        return restTemplateBuilder.build();
    }
}
package com.study.consumer.service.impl;

import com.study.consumer.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class ConsumerServiceImpl implements ConsumerService {
    @Autowired
    public RestTemplate restTemplate;

    @Override
    public String callHello(String name) {
        return restTemplate.getForObject("http://SERVICEPROVIDER/hi?name=" + name,String.class);
    }
}
spring.application.name=Consumer
server.port=8763

eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka/
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=true

ribbon.eureka.enabled=true
#SERVICEPROVIDER.ribbon.eureka.enabled=true
logging.level.com.netflix.loadbalancer = debug

异常:

java.lang.IllegalStateException: No instances available for SERVICEPROVIDER
    at org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.execute(RibbonLoadBalancerClient.java:119) ~[spring-cloud-netflix-ribbon-2.2.9.RELEASE.jar:2.2.9.RELEASE]
    at org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.execute(RibbonLoadBalancerClient.java:99) ~[spring-cloud-netflix-ribbon-2.2.9.RELEASE.jar:2.2.9.RELEASE]
    at org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor.intercept(LoadBalancerInterceptor.java:56) ~[spring-cloud-commons-3.0.3.jar:3.0.3]
    at org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:93) ~[spring-web-5.3.9.jar:5.3.9]

日志:

2021-07-26 14:19:50.246  INFO 5972 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8763 (http) with context path ''
2021-07-26 14:19:50.247  INFO 5972 --- [           main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8763
2021-07-26 14:19:50.262  INFO 5972 --- [nfoReplicator-0] com.netflix.discovery.discoveryClient    : discoveryClient_CONSUMER/192.168.88.111:Consumer:8763 - registration status: 204
2021-07-26 14:19:50.268  INFO 5972 --- [           main] com.study.consumer.ConsumerApplication   : Started ConsumerApplication in 4.441 seconds (JVM running for 6.375)
2021-07-26 14:19:50.659  INFO 5972 --- [nio-8763-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring dispatcherServlet 'dispatcherServlet'
2021-07-26 14:19:50.659  INFO 5972 --- [nio-8763-exec-1] o.s.web.servlet.dispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-07-26 14:19:50.660  INFO 5972 --- [nio-8763-exec-1] o.s.web.servlet.dispatcherServlet        : Completed initialization in 1 ms
2021-07-26 14:19:50.796 DEBUG 5972 --- [nio-8763-exec-1] c.n.l.ZoneAffinityServerListFilter       : ZoneAffinity is set to false
2021-07-26 14:19:50.796 DEBUG 5972 --- [nio-8763-exec-1] c.n.l.ZoneAffinityServerListFilter       : activeReqeustsPerServerThreshold: 0.6
2021-07-26 14:19:50.797 DEBUG 5972 --- [nio-8763-exec-1] c.n.l.ZoneAffinityServerListFilter       : blackOutServerPercentageThreshold: 0.8
2021-07-26 14:19:50.797 DEBUG 5972 --- [nio-8763-exec-1] c.n.l.ZoneAffinityServerListFilter       : availableServersThreshold: 2
2021-07-26 14:19:50.811  INFO 5972 --- [nio-8763-exec-1] c.netflix.config.ChainedDynamicProperty  : Flipping property: SERVICEPROVIDER.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2021-07-26 14:19:50.879 DEBUG 5972 --- [nio-8763-exec-1] c.netflix.loadbalancer.BaseLoadBalancer  : LoadBalancer [SERVICEPROVIDER]:  pingIntervalSeconds set to 30
2021-07-26 14:19:50.879 DEBUG 5972 --- [nio-8763-exec-1] c.netflix.loadbalancer.BaseLoadBalancer  : LoadBalancer [SERVICEPROVIDER]: maxTotalPingTime set to 2
2021-07-26 14:19:50.879  INFO 5972 --- [nio-8763-exec-1] c.netflix.loadbalancer.BaseLoadBalancer  : Client: SERVICEPROVIDER instantiated a LoadBalancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=SERVICEPROVIDER,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null
2021-07-26 14:19:50.906  INFO 5972 --- [nio-8763-exec-1] c.n.l.DynamicServerListLoadBalancer      : Using serverListUpdater PollingServerListUpdater
2021-07-26 14:19:50.911 DEBUG 5972 --- [nio-8763-exec-1] c.n.l.DynamicServerListLoadBalancer      : List of Servers for SERVICEPROVIDER obtained from discovery client: []
2021-07-26 14:19:50.913 DEBUG 5972 --- [nio-8763-exec-1] c.n.l.DynamicServerListLoadBalancer      : Filtered List of Servers for SERVICEPROVIDER obtained from discovery client: []
2021-07-26 14:19:50.914 DEBUG 5972 --- [nio-8763-exec-1] c.netflix.loadbalancer.BaseLoadBalancer  : LoadBalancer [SERVICEPROVIDER]: clearing server list (SET op)
2021-07-26 14:19:50.916 DEBUG 5972 --- [nio-8763-exec-1] c.n.l.DynamicServerListLoadBalancer      : Setting server list for zones: {}
2021-07-26 14:19:50.916  INFO 5972 --- [nio-8763-exec-1] c.n.l.DynamicServerListLoadBalancer      : DynamicServerListLoadBalancer for client SERVICEPROVIDER initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=SERVICEPROVIDER,Server stats: []}ServerList:com.netflix.loadbalancer.ConfigurationBasedServerList@4d89459c
2021-07-26 14:19:51.063 DEBUG 5972 --- [nio-8763-exec-1] c.n.loadbalancer.ZoneAwareLoadBalancer   : Zone aware logic disabled or there is only one zone
2021-07-26 14:19:51.102 ERROR 5972 --- [nio-8763-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing Failed; nested exception is java.lang

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。