No instances available for XXX

 spring cloud 使用nacos作为服务发现,ribbon做负载均衡,然后报错,No instances available for cloud-alibaba-study。

版本

  Spring boot  2.6.7

  Spring cloud  2021.0.3

  Spring cloud alibaba 2021.0.6.0

过程

  当我引入nacos包如下,2021.0.6.0包移除了ribbon,(除非使用2.2.7.RELEASE版本,参考https://blog.csdn.net/qq_30946821/article/details/117433812

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

  所以我手动加入了ribbon包,

<!-- Ribbon 依赖 -->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-ribbon -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
    <version>2.2.7.RELEASE</version>
</dependency>

  接下来报错,

Description:

Parameter 0 of method loadBalancerWebClientBuilderBeanPostProcessor in org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerBeanPostProcessorAutoConfiguration required a bean of type 'org.springframework.cloud.client.loadbalancer.reactive.DeferringLoadBalancerExchangeFilterFunction' that could not be found.

Action:

Consider defining a bean of type 'org.springframework.cloud.client.loadbalancer.reactive.DeferringLoadBalancerExchangeFilterFunction' in your configuration.

  然后我加入了spring-cloud-starter-loadbalancer包,

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

  结果报错No instances available for cloud-alibaba-study。

  服务列表也有,如下图

  查了文档才知道

  从 Spring Cloud 2020.0.0 版本开始,Spring Cloud 官方推荐使用 spring-cloud-starter-loadbalancer 作为负载均衡器,而不是 Ribbon。你可以考虑使用 Spring Cloud LoadBalancer 替代 Ribbon。

  所以我在想是不是loadbalancer和ribbon包冲突,

  删除 spring-cloud-starter-netflix-ribbon 包,正常。

  

调用方式如下,

@Bean
@LoadBalanced
public RestTemplate restTemplate(){
    return new RestTemplate();
}
@RequestMapping("/loadBalance")
public List<ServiceInstance> loadBalance(){
    return this.restTemplate.getForObject("http://cloud-alibaba-study/getInstances",List.class);
}

 

posted @ 2024-10-07 20:43  伟衙内  阅读(111)  评论(0编辑  收藏  举报