spring cloud Alibaba 集成openfeign 和 loadbalancer
1.1 引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
<version>${feign-okhttp-version}</version>
</dependency>
<!--重试 -->
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
1.2 添加注解
@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients(basePackages = "io.pay.feign") // 扫描feign指定的包
public class PayApplication {
public static void main(String[] args) {
SpringApplication.run(PayApplication.class, args);
}
}
1.3 添加配置
# 以下配置为 LoadBalancerProperties 配置类
# default 表示全局配置,如要针对某个服务,则填写对应的服务名即可
spring.cloud.loadbalancer.clients.default.retry.enabled=true
# 是否所有的请求都重试,false 表示只有 GET 请求才重试
spring.cloud.loadbalancer.clients.default.retry.retryOnAllOperations=true
# 同一个实例的重试次数,不包括第一次调用;比如填了 3,实际会调用 4 次
spring.cloud.loadbalancer.clients.default.retry.maxRetriesOnSameServiceInstance=3
# 其他实例的重试次数,多节点的情况下使用
spring.cloud.loadbalancer.clients.default.retry.maxRetriesOnNextServiceInstance= 0
# 以下配置为 feign 配置类
#服务提供者的微服务ID #设置对应的负载均衡类 ?
soundssright-pay.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
# 开启请求数据的压缩功能
feign.compression.request.enabled=true
# 压缩支持的MIME类型
feign.compression.request.mime-types=text/xml,application/xml, application/json
# 数据压缩下限 1024表示传输数据大于1024 才会进行数据压缩(最小压缩值标准)
feign.compression.request.min-request-size=1024
# 开启响应数据的压缩功能
feign.compression.response.enabled=true
# 开启支持连接池的 http okhttp
feign.okhttp.enabled=true
1.4 异常问题处理
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'io.pay.feign.WarehouseServiceFeignClient': Unexpected exception during bean creation; nested exception is java.lang.IllegalStateException: No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalancer?
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:555)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154)
at com.xxl.job.core.executor.impl.XxlJobSpringExecutor.initJobHandlerMethodRepository(XxlJobSpringExecutor.java:87)
at com.xxl.job.core.executor.impl.XxlJobSpringExecutor.afterSingletonsInstantiated(XxlJobSpringExecutor.java:38)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:972)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:740)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:415)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1312)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301)
at io.pay.PayApplication.main(PayApplication.java:17)
Caused by: java.lang.IllegalStateException: No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalancer?
at org.springframework.cloud.openfeign.FeignClientFactoryBean.loadBalance(FeignClientFactoryBean.java:376)
at org.springframework.cloud.openfeign.FeignClientFactoryBean.getTarget(FeignClientFactoryBean.java:421)
at org.springframework.cloud.openfeign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:396)
at org.springframework.cloud.openfeign.FeignClientsRegistrar.lambda$registerFeignClient$0(FeignClientsRegistrar.java:235)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.obtainFromSupplier(AbstractAutowireCapableBeanFactory.java:1249)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1191)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
... 17 common frames omitted
[2022:06:01 14:25:21.715] Thread-13 WARN c.a.n.c.h.HttpClientBeanHolder - shutdown,108 - [HttpClientBeanHolder] Start destroying common HttpClient
Disconnected from the target VM, address: '127.0.0.1:1490', transport: 'socket'
由于Spring Cloud Feign在Hoxton.M2 RELEASED版本之后不再使用Ribbon而是使用spring-cloud-loadbalancer,所以不引入spring-cloud-loadbalancer会报错.
解决方法: 加入spring-cloud-loadbalancer依赖 并且在nacos中排除ribbon依赖,不然loadbalancer无效
排除ribbon依赖
<!-- 服务注册/发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon</artifactId>
</exclusion>
</exclusions>
</dependency>