1.服务熔断

1.1引入坐标

        <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency>

1.2 主启动类标识

package org.maple;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;/*** @author mapleins* @Date 2019-01-12 17:13* @Desc**/
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient//服务发现 可以看到服务信息
@EnableCircuitBreaker //开启断路器
public class App_Provider_Dept_8001_Hystrix {public static void main(String[] args) {SpringApplication.run(App_Provider_Dept_8001_Hystrix.class,args);}
}

1.3 添加熔断方法

    @GetMapping("/dept/get/{id}")//出错调用hystrixCommand中的方法@HystrixCommand(fallbackMethod = "hystrix_get")public Dept get(@PathVariable("id") Long id) {Dept dept = service.find(id);if (null == dept) {throw new RuntimeException("该id"+id+"没有对应信息");}return dept;}public Dept hystrix_get(@PathVariable("id") Long id){return new Dept().setDeptNo(id).setDName("该id"+id+"没有对应的信息,null--@HystrixCommand").setDb_source("no this database in Mysql");}

1.4 访问

2.服务降级

  添加服务熔断,会造成方法翻倍,每一个接口都需要一个服务熔断,此时就可以使用服务降级,类似异常处理+切面编程

2.1 针对接口编写回调函数工厂,在接口上声明工厂类

  之前将服务的调用通过feign来实现接口调用,现在对接口操作实现服务降级,对整个方法进行统一管理,实现解耦

package org.maple.service;import feign.hystrix.FallbackFactory;
import org.maple.entity.Dept;
import org.springframework.stereotype.Component;import java.util.List;/*** @author mapleins* @Date 2019-01-13 12:01* @Desc 服务降级**/
@Component
public class DeptClientServiceFallbackFactory implements FallbackFactory<DeptClientService> {@Overridepublic DeptClientService create(Throwable throwable) {return new DeptClientService() {@Overridepublic boolean add(Dept dept) {return false;}@Overridepublic Dept get(Long id) {return new Dept().setDeptNo(id).setDName("该服务已经关闭").setDb_source("no this database in Mysql");}@Overridepublic List<Dept> list() {return null;}};}
}

package org.maple.service;import org.maple.entity.Dept;
import org.springframework.cloud.openfeign.FeignClient;
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.RequestBody;import java.util.List;/*** @author mapleins* @Date 2019-01-12 23:10* @Desc 通过接口和注解 面向接口编程访问微服务**/
//@FeignClient("ms-provider-dept")
@FeignClient(value = "ms-provider-dept",fallbackFactory = DeptClientServiceFallbackFactory.class) //服务降级类
public interface DeptClientService {@PostMapping("/dept/add")boolean add(@RequestBody Dept dept);@GetMapping("/dept/get/{id}")Dept get(@PathVariable("id") Long id);@GetMapping("/dept/list")List<Dept> list();}

2.2 在调用接口的消费方开启hystrix

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

2.3 调用

开启2个eureka server,1个provider-dept,1个consumer

关闭provider

3.Hystrix Dashboard 准实时调用监控

3.1 操作

需要监控的服务必须依赖:

        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency>

新建一个hystrix-dashboard工程

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>spring-cloud-learning</artifactId><groupId>org.org.maple</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>ms-consumer-hystrix-dashboard</artifactId><dependencies><dependency><groupId>org.org.maple</groupId><artifactId>ms-common-api</artifactId><version>0.0.1-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId></dependency></dependencies></project>

server:port: 9001

package org.maple;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;/*** @author mapleins* @Date 2019-01-13 12:44* @Desc**/
@SpringBootApplication
@EnableHystrixDashboard //开启仪表盘
public class App_Hystrix_Dashboard_9001 {public static void main(String[] args) {SpringApplication.run(App_Hystrix_Dashboard_9001.class,args);}
}

3.2 启动界面

如果访问路径404,则需要在监控的服务中配置一个Bean

    @Beanpublic ServletRegistrationBean getServlet() {HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);registrationBean.setLoadOnStartup(1);registrationBean.addUrlMappings("/hystrix.stream");registrationBean.setName("HystrixMetricsStreamServlet");return registrationBean;}

转载于:https://www.cnblogs.com/mapleins/p/10262366.html

Spring Cloud入门五 hystrix相关推荐

  1. Spring Cloud 入门 之 Hystrix 篇(四)

    一.前言 在微服务应用中,服务存在一定的依赖关系,如果某个目标服务调用慢或者有大量超时造成服务不可用,间接导致其他的依赖服务不可用,最严重的可能会阻塞整条依赖链,最终导致业务系统崩溃(又称雪崩效应). ...

  2. Spring Cloud入门教程-Hystrix断路器实现容错和降级

    简介 Spring cloud提供了Hystrix容错库用以在服务不可用时,对配置了断路器的方法实行降级策略,临时调用备用方法.这篇文章将创建一个产品微服务,注册到eureka服务注册中心,然后我们使 ...

  3. Spring Cloud 入门 之 Zuul 篇(五)

    一.前言 随着业务的扩展,微服务会不对增加,相应的其对外开放的 API 接口也势必增多,这不利于前端的调用以及不同场景下数据的返回,因此,我们通常都需要设计一个 API 网关作为一个统一的 API 入 ...

  4. 一起来学Spring Cloud | 第五章:熔断器 ( Hystrix)

    在微服务项目中,一个系统可以分割成很多个不同的服务模块,不同模块之间我们通常需要进行相互调用.springcloud中可以使用RestTemplate+Ribbon和Feign来调用(工作中基本都是使 ...

  5. Spring Cloud 入门 之 Config 篇(六)

    一.前言 随着业务的扩展,为了方便开发和维护项目,我们通常会将大项目拆分成多个小项目做成微服务,每个微服务都会有各自配置文件,管理和修改文件起来也会变得繁琐.而且,当我们需要修改正在运行的项目的配置时 ...

  6. Spring Cloud 入门 之 Ribbon 篇(二)

    一.前言 上一篇<Spring Cloud 入门 之 Eureka 篇(一)> 介绍了微服务的搭建,服务注册与发现.但在文章中留了一个小尾巴--如何正确使用 Eureka 进行服务发现并调 ...

  7. Spring Cloud 入门 之 Eureka 篇(一)

    一.前言 Spring Cloud 是一系列框架的有序集合.它利用 Spring Boot 的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.消息总线.负载均衡.断路器.数据 ...

  8. Spring Cloud入门-Admin服务监控中心(Hoxton版本)

    文章目录 Spring Cloud入门系列汇总 摘要 Spring Boot Admin 简介 创建admin-server模块 创建admin-client模块 监控信息演示 结合注册中心使用 修改 ...

  9. Spring Cloud入门-Sentinel实现服务限流、熔断与降级(Hoxton版本)

    文章目录 Spring Cloud入门系列汇总 摘要 Sentinel简介 安装Sentinel控制台 创建sentinel-service模块 限流功能 创建RateLimitController类 ...

  10. Spring Cloud入门-Ribbon服务消费者(Hoxton版本)

    文章目录 Spring Cloud入门系列汇总 摘要 Ribbon简介 RestTemplate的使用 GET请求方法 getForObject方法 getForEntity方法 POST请求方法 p ...

最新文章

  1. 德鲁克管理思想:管理的7大理论、43条原则,每一条都是精华
  2. python爬虫教程i-Python 爬虫速成教程,还有35个实战项目送给你!
  3. Delphi 2009 新增单元 Character[2]: IsLetter、IsUpper、IsLower、IsDigit、IsNumber
  4. SendMessage、PostMessage原理
  5. Mysql索引基本概念及案例总结(含索引的使用注意事项)
  6. 【渝粤教育】国家开放大学2019年春季 0736-22T烹饪工艺学(2) 参考试题
  7. 算法导论 CLRS 22.4-4 解答
  8. 关系型数据库设计范式
  9. [vb] Set 语句
  10. python verilog就业_Verilog会被淘汰吗?
  11. 静态static与方法重载
  12. c语言求45678所有非平凡因子,近世代数基础习题课答案到第二章9题
  13. php无法找到该页,UCHOME出现问题(转帖)
  14. Linux命令行运行多线程程序 和 QT集成IDE下运行多线程程序的问题。
  15. 时间计算题100道_这三个马政经计算题,你能对几个?
  16. 高校计算机实验员岗位职责,计算机学院实验与实训中心主任岗位职责(参考Word)...
  17. Android内存泄漏总结,成功拿下大厂offer
  18. 深度学习的发展历史是什么?
  19. html tr隐藏 边框存在,CSS 设置tr的边框
  20. 沈阳服务器主板维修,沈阳铁西区附近电脑主板维修

热门文章

  1. ElasticSearch全文搜索引擎之入门以及环境搭建
  2. Spring Cloud Zuul网关集成JWT身份验证学习总结
  3. 面试题之对象创建的五种方式
  4. [MetalKit]37-Using-ARKit-with-Metal使用ARKit与Metal
  5. CYQ.Data 轻量数据访问层(七) 自定义数据表实现绑定常用的数据控件(上)
  6. Windows server 2008计划任务(批处理命令)不执行
  7. Enterprise Library 2.0 发布了...
  8. [环境搭建]-IIS下搭建FTP过程 解决无法连接及534 Policy requires SSL错误
  9. 使用Source Insight查看Android Framework 源码
  10. java获取电脑配置_Java.Utils:获取电脑配置信息