两个常见错误
自定义负载均衡报错
Invalid bean definition with name 'myRule' defined in com.atguigu.MyLb.MySelfRule: Bean name derived from @Bean method 'myRule' clashes with bean name for containing configuration class; please make those names unique!
这是因为Bean的名称不唯一导致的错误,修改名称即可解决
No instances available for SPRINGCLOUD-PROVIDER-DEPT
这个错误是在Ribbon客户端启动的时候调用出错,我说下自己的原因,从开始项目,我使用的是SpringCloud的2020.0.4这个版本,然后其对应的Netfix的组件都是3.0.3,这个可以在Spring的官网查询到
然后在集成Ribbon的时候报错,我之前使用的Eureka的版本是如下pom文件
<!--服务端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>3.0.3</version>
</dependency>
<!--客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>3.0.3</version>
</dependency>
<!--Ribbon-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
然后启动报错No instances available for SPRINGCLOUD-PROVIDER-DEPT,网上有很多的解决方法。一一尝试都未能解决我的问题,如
将这个改成小写字母,或者是去掉下划线,但是尝试过后还是不能解决问题,还有就是去掉Ribbon的依赖,但是去掉这个依赖之后Ribbon自定义负载均衡算法就无法使用。查询很多帖子都是说spring-cloud-starter-netflix-eureka-client集成了Ribbon但是确实是无法使用,后经查询Maven仓库才知道
spring-cloud-starter-netflix-eureka-client 3.0以上的版本集成的是 spring-cloud-starter-loadbalancer 所以最终的解决方案就是降低SpringCloud的版本,由2020.0.4降低到Hoxton.SR12,需要注意的是,SpringBoot的版本也需要降低到2.3.12.RELEASE。
最终问题得以解决