一)项目的创建
1.创建项目测试feign下的降级处理
2.pom文件
注意还没添加Hystrix的依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sxt</groupId>
<artifactId>23-feign-customer-fallback</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>23-feign-customer-fallback</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 注入服务项目-->
<dependency>
<groupId>com.sxt</groupId>
<artifactId>14-feign-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!--添加的HttpClient的坐标-->
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<!-- 添加Feign支持的HttpClient的jar包-->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
<version>10.1.0</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.全局配置文件
1.默认Feign是不使用Hystrix的,需要在全局配置文件内开启Hystrix
server:
port: 9092
spring:
application:
name: 23-feign-customer-fallback
eureka:
client:
service-url:
defaultZone: http://peer1:8081/eureka/,http://peer2:8082/eureka/,http://peer3:8083/eureka/
#开启Hystri默认是支持的。
feign:
hystrix:
enabled: true
4.测试服务降级
1.修改ProductCustomerService,以往使用声明式调用的时候,需要继承接口,此时不需要继承,需要在接口内声明要远程调用的抽象方法
2.使用降级的时候,需要@FeignClient注解内的fallback属性内书写关于托底数据的编写
/**
* @FeignClient:注解的作用是指定的一个Feign客户端
* name:找到指定的实现了服务的应用名字,当前案例中是15-feign-provider实现了所以name的值
* 应该为15-feign-provider(这个的值是Spring.application.name的值)
* fallback:返回托底数据的类
* feign进行降级处理的时候不能和服务接口有联系
*/
@FeignClient(name = "15-feign-provider",fallback = ProductCustomerServiceFallback.class)
public interface ProductCustomerService
{
@GetMapping(value = "/product/findAllProduct")
public List<Product> findAllProduct();
}
5.托底数据编写需要编写一个类实现Constomer接口内的方法即可,在@FeignClient注解内引用,如果Constomer内的方法出现异常会根据fallback内的类查找指定方法的托底数据。
/**
* @project_name:springcloud
* @date:2019/9/4:14:22
* @author:shinelon
* @Describe:托底数据类
*/
@Component
public class ProductCustomerServiceFallback implements ProductCustomerService
{
@Override
public List<Product> findAllProduct()
{
List<Product> list = Arrays.asList(new Product("我是托底数据", -1));
return list;
}
}
服务能正常的访问到
关掉服务能够返回托底数据,