12、Feign整合断路器Hystrix

简介: 这篇来看看如何Feign整合断路器Hystrix,Feign整合断路器Hystrix也是相对比较简单的。Feign默认已经自带断路器Hystrix,所以不需要像RestTemplate+Ribbon整合断路器Hystrix那样需要在SpringBoot的启动类添加注解。但是Feign自带断路器并没有打开,需要做些额外的配置。

上编说了《RestTemplate+Ribbon整合断路器Hystrix》,这篇来看看如何Feign整合断路器HystrixFeign整合断路器Hystrix也是相对比较简单的。Feign默认已经自带断路器Hystrix,所以不需要像RestTemplate+Ribbon整合断路器Hystrix那样需要在SpringBoot的启动类添加注解。但是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


微信图片_20220501173958.png


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
@EnableFeignClients
public class ConsumerFeignApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerFeignApplication.class, args);
}
}


3、 新建配置文件bootstrap.ymlapplication.yml


bootstrap.yml


server:
  port: 5800


application.yml


spring:
  application:
    name: sc-eureka-client-consumer-feign-hystrix
eureka:
  client:
    registerWithEureka: true #是否将自己注册到Eureka服务中,默认为true
    fetchRegistry: true #是否从Eureka中获取注册信息,默认为true
    serviceUrl:
      defaultZone: http://localhost:5001/eureka/
feign:
  hystrix:
enabled: true


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


微信图片_20220501174001.png


4、 新建服务消费者类UserService.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


微信图片_20220501174003.png


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;
@Component
public class UserServiceHystrix  implements UserService{
@Override
public 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;
}
@Override
public 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;
}
@Override
public 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;
}
@Override
public 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;
}
@Override
public 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、 分别启动注册中心sc-eureka-server和服务提供者sc-eureka-client-provider


 

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


方式一:查看日志看看对应的端口是启动


微信图片_20220501174007.png


方式二:查看注册中心是否注册成功


微信图片_20220501174009.png


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


上篇从服务提供者是否正常启动验证断路器是否启动,今天看看从数据库是否启动来验证断路器是否启动


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


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


微信图片_20220501174012.png


2MySQL服务关闭时访问


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


微信图片_20220501174015.png


再看下后台日志:


微信图片_20220501174018.png


其他接口可以自行按照以上方式进行验证,看看断路器是否启动


 

相关文章
|
9月前
|
Cloud Native Go 微服务
golang 微服务中的断路器 hystrix
golang 微服务中的断路器 hystrix
|
10月前
|
缓存 监控 Java
12SpringCloud - 断路器(Hystrix)引入
12SpringCloud - 断路器(Hystrix)引入
44 0
|
11月前
|
监控 微服务
Hystrix断路器执行原理
Hystrix断路器执行原理
68 0
SpringCloud极简入门-Feign开启Hystrix
1.支付服务集成Hystrix 官方文档:https://cloud.spring.io/spring-cloud-static/Greenwich.SR5/single/spring-cloud.html#spring-cloud-feign-hystrix 支付服务 springcloud-pay-server-1040 之前集成了Feign,修改该工程集成Hystrix。我们除了要给Feign开启Hystrix以外还需要为Feign接口编写托底类。
136 0
|
27天前
|
监控 Java 数据中心
通用快照方案问题之服务雪崩问题如何解决
通用快照方案问题之服务雪崩问题如何解决
12 0
Ribbon、Feign、Hystrix超时&重试&熔断问题
在使用Ribbon、Feign、Hystrix组合时,因为配置的问题出现以下现象,让我的大脑CPU烧的不行不行(拿我老家话说就是“脑子ran滴奥”)
143 0
|
3月前
|
监控 负载均衡 数据可视化
SpringCloud - Hystrix断路器-服务熔断与降级和HystrixDashboard
SpringCloud - Hystrix断路器-服务熔断与降级和HystrixDashboard
48 0
|
9月前
|
Cloud Native 测试技术 Go
golang 微服务中的断路器 hystrix 小案例
golang 微服务中的断路器 hystrix 小案例
|
10月前
|
监控 Java 微服务
16SpringCloud - 断路器项目示例(Hystrix Dashboard)
16SpringCloud - 断路器项目示例(Hystrix Dashboard)
39 0
|
10月前
|
Java Spring
15SpringCloud - 断路器项目示例(Feign Hystrix)
15SpringCloud - 断路器项目示例(Feign Hystrix)
31 0