摘要:公众号乐园上编说了整合断路器,这篇来看看如何整合断路器,整合断路器也是相对比较简单的。默认已经自带断路器,所以不需要像整合断路器那样需要在的启动类添加注解。但是自带断路器并没有打开,需要做些额外的配置。
公众号: java乐园
上编说了《RestTemplate+Ribbon整合断路器Hystrix》,这篇来看看如何Feign整合断路器Hystrix,Feign整合断路器Hystrix也是相对比较简单的。Feign默认已经自带断路器Hystrix,所以不需要像RestTemplate+Ribbon整合断路器Hystrix那样需要在SpringBoot的启动类添加注解。但是Feign自带断路器并没有打开,需要做些额外的配置。
</>复制代码
feign:
hystrix:
enabled: true
1、 新建项目sc-eureka-client-consumer-feign-hystrix,对应的pom.xml文件如下
</>复制代码
4.0.0
spring-cloud
sc-eureka-client-consumer-feign
0.0.1-SNAPSHOT
jar
sc-eureka-client-consumer-feign
http://maven.apache.org
org.springframework.boot
spring-boot-starter-parent
2.0.4.RELEASE
org.springframework.cloud
spring-cloud-dependencies
Finchley.RELEASE
pom
import
UTF-8
1.8
1.8
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-openfeign
备注:从继续关系可以看出spring-cloud-starter-openfeign已经集成断路器Hystrix
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.yml和application.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的配置项
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 getUser(@PathVariable(value ="id") Long id);
@RequestMapping("/user/listUser")
Map listUser();
@PostMapping("/user/addUser")
Map addUser(@RequestBody User user);
@PutMapping("/user/updateUser")
Map updateUser(@RequestBody User user);
@DeleteMapping("/user/deleteUser/{id}")
Map deleteUser(@PathVariable(value ="id") Long id);
}
可以看到在该类的FeignClient注解上添加了一个属性fallback
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 getUser(Long id) {
Map result = new HashMap();
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 listUser() {
Map result = new HashMap();
result.put("code", "000000");
result.put("msg", "success");
List list = new ArrayList();
User u = new User();
u.setId(-1L);
u.setUserName("failBackName");
list.add(u);
result.put("body", list);
return result;
}
@Override
public Map addUser(User user) {
Map result = new HashMap();
result.put("code", "000000");
result.put("msg", "success");
result.put("body", 0);
return result;
}
@Override
public Map updateUser(User user) {
Map result = new HashMap();
result.put("code", "000000");
result.put("msg", "success");
result.put("body", 0);
return result;
}
@Override
public Map deleteUser(Long id) {
Map result = new HashMap();
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,并验证是否启动成功
方式一:查看日志看看对应的端口是启动
方式二:查看注册中心是否注册成功
8、 使用postman验证断路器是否启用
上篇从服务提供者是否正常启动验证断路器是否启动,今天看看从数据库是否启动来验证断路器是否启动
(1)MySQL服务正常启动时访问:
http://127.0.0.1:5800/feign/user/listUser
(2)MySQL服务关闭时访问
http://127.0.0.1:5800/feign/user/listUser
再看下后台日志:
其他接口可以自行按照以上方式进行验证,看看断路器是否启动
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/76053.html
摘要:多层服务调用常见于微服务架构中较底层的服务如果出现故障,会导致连锁故障。 Spring Cloud 体验 简介 Spring Cloud为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、 事件总线、全局锁、决策竞选、分布式会话等等 基于Spring Boot,Spring Cloud将各公司成熟服务框架组合起来,通过Spring Boo...
摘要:开公众号差不多两年了,有不少原创教程,当原创越来越多时,大家搜索起来就很不方便,因此做了一个索引帮助大家快速找到需要的文章系列处理登录请求前后端分离一使用完美处理权限问题前后端分离二使用完美处理权限问题前后端分离三中密码加盐与中异常统一处理 开公众号差不多两年了,有不少原创教程,当原创越来越多时,大家搜索起来就很不方便,因此做了一个索引帮助大家快速找到需要的文章! Spring Boo...
摘要:服务注册中心一个服务注册中心,所有的服务都在注册中心注册,负载均衡也是通过在注册中心注册的服务来使用一定策略来实现。在客户端实现了负载均衡。 文章参考于史上最简单的 SpringCloud 教程 | 终章 Spring Cloud 是一个微服务框架,与 Spring Boot 结合,开发简单。将一个大工程项目,分成多个小 web 服务工程,可以分别独立扩展,又可以共同合作。 环境 ...
摘要:在该配置中,加入这个方法的话,表明使用了该配置的地方,就会禁用该模块使用容灾降级的功能添加访问层添加电影微服务启动类电影微服务,定制,一个功能禁用,另一个功能启用。 SpringCloud(第 016 篇)电影微服务,定制Feign,一个Feign功能禁用Hystrix,另一个Feign功能启用Hystrix - 一、大致介绍 1、在一些场景中,部分功能需要使用断路器功能,部分功能不需...
阅读 3827·2021-11-11 16:55
阅读 1723·2021-10-08 10:04
阅读 3665·2021-09-27 13:36
阅读 2888·2019-08-30 15:53
阅读 1937·2019-08-30 11:17
阅读 1335·2019-08-29 16:55
阅读 2171·2019-08-29 13:57
阅读 2591·2019-08-29 13:13