首页 > 编程知识 正文

服务降级的下一步服务熔断,hystrix服务熔断降级原理

时间:2023-05-04 13:27:36 阅读:23890 作者:1065

服务熔断和服务下降服务熔断(发生在服务端,如果某个服务超时或异常,会引起熔断,类似于现实生活中的保险丝;

服务降级:发生在客户端。 从整个网站的请求负载来看,当服务熔断或关闭时,将不再调用服务

这次使用Hystrix实现服务熔断和服务降级。

服务熔断实现步骤:在与部署相关的pom依赖服务端部署依赖项

! 熔断hystrix的依赖关系--- dependencygroupidorg.spring framework.cloud/groupidartifactidspring-cloud-starter-hy strix/artrtix - https://mvn repository.com/artifact/org.spring framework.cloud/spring-cloud-starter-eureka -从属组组-充实监视信息- -! - https://mvn资料库.com/artifact/org.spring framework.boot/spring-boot-starter-actuator-dependency groups gratior artifact id/dependencydependencygroupidcom.jxau/groupidartifactidspring artifactidversion 1.0 -快照/版本/dependion groupidartifactidjunit/artifact id/dependen cid groupidartifactidmysql-connector-Java/artifact id/dependencydendendencyl groupidartifactiddruid/artifactidruid ependencydependencygroupidch.QoS.logback/groupidartifactidlogback-core/artifacy 从属关系组组组内存batis-spring-boot-starter/artifact id /从属关系组

dency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-test</artifactId> <version>2.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> <version>2.1.4.RELEASE</version> </dependency> 添加相应注解

在controller层的方法上添加相应的注解

@GetMapping("/dept/get/{id}") @HystrixCommand(fallbackMethod = "doGetHystrix") public Dept doGet(@PathVariable("id") Long id){ Dept dept= deptService.queryById(id); if(dept==null){ throw new RuntimeException("不好意思,服务已降级,服务器关闭,你无法继续访问"); // 降级操作 } return dept; } public Dept doGetHystrix(@PathVariable("id") Long id){ Dept dept=new Dept(); dept.setDeptno(id); dept.setDname("id=>"+id+"不存在该用户,或者信息无法查询~"); dept.setDb_source("no this database in MySQL"); return dept; }

@HystrixCommand(fallbackMethod = "doGetHystrix")fallbackMethod 属性表示的是熔断后调用的方法,用来化解异常,doGetHystrix(@PathVariable("id") Long id)可以说是熔断处理方法。

服务降级实现步骤

服务降级主要在服务消费端实现,比如:当双十一来临时,淘宝就会关停用户注册的相关服务器,暂停相关服务,以保证购物的服务端足够支撑如此大规模的并发。

导入相关的pom依赖

在api客户端导入依赖:

<!--引入Feign依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> 编写服务降级后处理的方法

方法需要实现FallbackFactory接口,并重写父类接口的方法。

public class DeptClientServiceFallbackFactory implements FallbackFactory { @Override public DeptClientService create(Throwable throwable) { return new DeptClientService() { @PostMapping("/dept/add") public Boolean doAddDept(Dept dept){ return null; } @GetMapping("/dept/get/{id}") public Dept doGet(@PathVariable("id") Long id){ Dept dept=new Dept(); dept.setDeptno(id); dept.setDname("id=>"+id+"不存在该用户,或者信息无法查询~"); dept.setDb_source("no this database in MySQL"); return dept; } @GetMapping("/dept/list") public List<Dept> doGetAll(){ return null; } }; }}

在服务接口添加相应的注解
fallbackFactory

@Component@FeignClient(fallbackFactory=DeptClientServiceFallbackFactory.class)public interface DeptClientService { @PostMapping("/dept/add") public Boolean doAddDept(Dept dept); @GetMapping("/dept/get/{id}") public Dept doGet(@PathVariable("id") Long id); @GetMapping("/dept/list") public List<Dept> doGetAll();} 更改配置文件

在服务调用方开启相应服务降级

# 开启服务降级feign: hystrix: enabled: true

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。