在feign声明式调用中,在@FeignClient注解中添加fallback参数
@FeignClient(value = "product-server",fallback = ProductServiceFallback.class)
这个ProductServiceFallback类是ProductService接口的实现类,要作为组件注入
如果需要有服务的异常信息,那么@FeignClient注解中使用fallbackFactory参数
@FeignClient(value = "product-server",fallbackFactory = ProductServiceFallbackFactory.class)
测试feign雪崩时候,productService的服务停了就能测试雪崩了
fallback兜底的两种类
实现ProductService
接口
package com.qiangqiang.fallback;
import com.qiangqiang.entity.Product;
import com.qiangqiang.service.ProductService;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
public class ProductServiceFallback implements ProductService {
@Override
public List<Product> getProduct() {
Product product1 = new Product(1, "手机兜底兜底",1);
Product product2 = new Product(2, "电脑兜底兜底",1);
List<Product> list = new ArrayList<>();
list.add(product1);
list.add(product2);
return list;
}
@Override
public List<Product> getProductById(Integer id) {
Product product = new Product(id, "手机兜底"+id,1);
List<Product> list = new ArrayList<>();
list.add(product);
return list;
}
}
实现FallbackFactory<ProductService>
接口
package com.qiangqiang.fallback;
import com.qiangqiang.entity.Product;
import com.qiangqiang.service.ProductService;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
public class ProductServiceFallbackFactory implements FallbackFactory<ProductService> {
@Override
public ProductService create(Throwable throwable) {
Logger logger = LoggerFactory.getLogger(ProductServiceFallbackFactory.class);
return new ProductService() {
@Override
public List<Product> getProduct() {
logger.error("product-server服务的getProduct方法异常,异常信息为:" + throwable);
Product product1 = new Product(1, "手机兜底兜底",1);
Product product2 = new Product(2, "电脑兜底兜底",1);
List<Product> list = new ArrayList<>();
list.add(product1);
list.add(product2);
return list;
}
@Override
public List<Product> getProductById(Integer id) {
logger.error("product-server服务的getProductById方法异常,异常信息为:" + throwable);
Product product = new Product(id, "手机兜底"+id,1);
List<Product> list = new ArrayList<>();
list.add(product);
return list;
}
};
}
}
启动类开启feign注解
package com.qiangqiang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class SpringCloudOrderServerFeignApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudOrderServerFeignApplication.class, args);
}
}
yml配置要额外增加feign对于hystrix的支持
server:
port: 8005
tomcat:
max-threads: 11 #降低最大线程数方便模拟高并发
eureka:
client:
service-url:
defaultZone: http://root:root@eureka2:8762/eureka/,http://root:root@eureka1:8761/eureka/
register-with-eureka: true #不注册到注册中心
instance:
instance-id: ${spring.cloud.client.ip-address}:${server.port} #显示ip:port
prefer-ip-address: true
lease-renewal-interval-in-seconds: 5 #心跳间隔
lease-expiration-duration-in-seconds: 15
spring:
application:
name: order-server
product-server:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
ribbon:
ConnecTimeout: 5000
ReadTimeout: 5000
#额外开启feign对hystrix的支持
feign:
hystrix:
enabled: true
依赖项
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--注册到注册中心的服务就用client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.0</version>
</dependency>
</dependencies>