当前位置: 首页 > 图灵资讯 > 技术篇> 12、Feign整合断路器Hystrix

12、Feign整合断路器Hystrix

来源:图灵教育
时间:2023-05-30 09:28:30

上编说了《RestTemplate+Ribon集成断路器Hystrix,看看Feign集成断路器Hystrix,Feign集成断路器Hystrix也比较简单。Feign默认有自己的断路器Hystrix,因此不需要在SpringBot的启动类中添加注释,如Restemplate+Ribon集成断路器Hystrix。但Feign自带的断路器还没有打开,需要做一些额外的配置。

feign:   hystrix:     enabled: true

1、 新项目sc-eureka-client-consumer-feign-hystrix,相应的pom.xml文件如下

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>spring-cloud</groupId><artifactId>sc-eureka-client-consumer-feign</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>sc-eureka-client-consumer-feign</name><url>http://maven.apache.org</url><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.4.RELEASE</version></parent><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies><!-- 说明是一个 eureka 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.cloud</groupId>         <artifactId>spring-cloud-starter-feign</artifactId>     <version>1.4.5.RELEASE</version>    </dependency> --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency></dependencies></project>

注:spring可以从继续关系中看出-cloud-starter-openfeign已经集成了断路器Hystrix

12、Feign整合断路器Hystrix_spring

 

2、 新建springboot启动类

package sc.consumer;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication@EnableEurekaClient@EnableFeignClientspublic class ConsumerFeignApplication {public static void main(String[] args) {SpringApplication.run(ConsumerFeignApplication.class, args);}}

3、 botstrapp新配置文件.yml和application.yml bootstrap.yml

server:  port: 5800aplication.ymlspring:  application:    name: sc-eureka-client-consumer-feign-hystrixeureka:  client:    registerWithEureka: true #是否在Eureka服务中注册自己,默认为true    fetchRegistry: true #是否从Eureka获取注册信息默认为true?    serviceUrl:      defaultZone: http://localhost:5001/eureka/ feign:  hystrix:enabled: true

说明:application.开启断路器Hystrix的配置项添加了yml配置文件

12、Feign整合断路器Hystrix_User_02

4、 Userserviceer新服务消费者.java

package sc.consumer.service;import java.util.Map;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.DeleteMapping;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.PutMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import sc.consumer.model.User;import sc.consumer.service.hystrix.UserServiceHystrix;@FeignClient(value="sc-eureka-client-provider", fallback=UserServiceHystrix.class)public interface UserService {@GetMapping("/user/getUser/{id}")Map<String, Object> getUser(@PathVariable(value ="id") Long id);@RequestMapping("/user/listUser")Map<String, Object> listUser();@PostMapping("/user/addUser")Map<String, Object> addUser(@RequestBody User user);@PutMapping("/user/updateUser")Map<String, Object> updateUser(@RequestBody User user);@DeleteMapping("/user/deleteUser/{id}")Map<String, Object> deleteUser(@PathVariable(value ="id") Long id);}

可以看出,在这种FeignClient注释中添加了属性fallback

12、Feign整合断路器Hystrix_断路器_03

5、 新建断路器处理类Userservicehystrix.java

package sc.consumer.service.hystrix;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.springframework.stereotype.Component;import sc.consumer.model.User;import sc.consumer.service.UserService;@Componentpublic class UserServiceHystrix  implements UserService{@Overridepublic Map<String, Object> getUser(Long id) {Map<String, Object> result = new HashMap<String, Object>();result.put("code", "000000");result.put("msg", "success");User u = new User();u.setId(-1L);u.setUserName("failBackName");result.put("body", u);return result;}@Overridepublic Map<String, Object> listUser() {Map<String, Object> result = new HashMap<String, Object>();result.put("code", "000000");result.put("msg", "success");List<User> list = new ArrayList<User>();User u = new User();u.setId(-1L);u.setUserName("failBackName");list.add(u);result.put("body", list);return result;}@Overridepublic Map<String, Object> addUser(User user) {Map<String, Object> result = new HashMap<String, Object>();result.put("code", "000000");result.put("msg", "success");result.put("body", 0);return result;}@Overridepublic Map<String, Object> updateUser(User user) {Map<String, Object> result = new HashMap<String, Object>();result.put("code", "000000");result.put("msg", "success");result.put("body", 0);return result;}@Overridepublic Map<String, Object> deleteUser(Long id) {Map<String, Object> result = new HashMap<String, Object>();result.put("code", "000000");result.put("msg", "success");result.put("body", 0);return result;}}

该类实现了UserService接口,并实现了该接口的所有方法。

6、 注册中心scc分别启动-eureka-Scerver和服务提供商-eureka-client-provider

7、 启动sc-eureka-client-consumer-feign-hystrix,并验证启动是否成功

方法1:查看日志,查看相应的端口

12、Feign整合断路器Hystrix_User_04

方法二:检查注册中心注册是否成功

12、Feign整合断路器Hystrix_User_05

8、 使用postman验证断路器是否启用

上一篇文章从服务提供商是否正常启动来验证断路器是否启动。今天,让我们来看看断路器是否从数据库中启动

(1)MySQL服务正常启动时访问:

http://127.0.0.1:5800/feign/user/listUser

12、Feign整合断路器Hystrix_User_06

(2)MySQL服务关闭时访问

http://127.0.0.1:5800/feign/user/listUser

12、Feign整合断路器Hystrix_spring_07

 

再看后台日志:
其他接口可以通过上述方式自行验证,看断路器是否启动