首页 » 技术 » 分布式 » 正文

springcloud + nacos + sentinel 遇到的坑

1、feign熔断callback出错

Unsatisfied dependency expressed through field ‘client’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘BaseAdminServiceClient’: FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: Incompatible fallback instance. Fallback/fallbackFactory of type class

出现问题的原因的

1、fallback没有设置@Component

2、fallback的实现需要实现feign的接口,而不是feign的基类接口,在我们的项目中,feign的接口是一个空接口,用来实现 client基类接口的。

3、没有开启feign的熔断

需要在配置里设置:

feign:
    sentinel:
        enabled: true

4、熔断是基于消费端的(feign 的fallback),限流是基于服务端的(sentinel的blockHandler )。限流设置的主要参数:

5、sentinel 获取不到配置。这里有一个巨坑,nacos的conf和discovery用的是group,而sentinel的datasource使用的是groupId,看了源代码才知道原因。

6、FeignClientSpecification’, defined in null

当有多个Feign的名字指向一个服务的时候,就会报此错误,解决办法
a、在application.yml中配置:

spring:
main:
allow-bean-definition-overriding: true

b、把多个feign接口合并成一个,并在fallback里实现多个方法

c、@FeignClient注解上添加contextId,如:@FeignClient(name=”common-service”, contextId = “exp”)

 

7、多模块下,spring或者feign找不到bean。

排除引入的问题,还需要在application server里指定路径。

@SpringBootApplication(scanBasePackages = {"com.zhemitech.colasaas"})
@EnableFeignClients(basePackages = {"com.zhemitech.colasaas"})

8、redis 的RedisTemplateConfig重复,错误信息如下:

Description:

The bean ‘redisTemplate’, defined in class path resource [com/my/test/common/data/cache/RedisTemplateConfig.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/boot/autoconfigure/data/redis/RedisAutoConfiguration.class] and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
解决办法:

a、按Action说的,配置 spring.main.allow-bean-definition-overriding=true

b、如果没有依赖,去掉redis的依赖,如果有依赖,把@Autowired 换成 @Resource

c、 使用自定义的redis,参考 https://blog.csdn.net/yeyinglingfeng/article/details/87790700

发表评论