<Spring Cloud>入门五 hystrix
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> { @Override
public DeptClientService create(Throwable throwable) { return new DeptClientService() {
@Override
public boolean add(Dept dept) {
return false;
} @Override
public Dept get(Long id) {
return new Dept().setDeptNo(id).setDName("该服务已经关闭").setDb_source("no this database in Mysql");
} @Override
public 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
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}



<Spring Cloud>入门五 hystrix的更多相关文章
- Spring Cloud 入门 之 Hystrix 篇(四)
原文地址:Spring Cloud 入门 之 Hystrix 篇(四) 博客地址:http://www.extlight.com 一.前言 在微服务应用中,服务存在一定的依赖关系,如果某个目标服务调用 ...
- Spring Cloud入门教程-Hystrix断路器实现容错和降级
简介 Spring cloud提供了Hystrix容错库用以在服务不可用时,对配置了断路器的方法实行降级策略,临时调用备用方法.这篇文章将创建一个产品微服务,注册到eureka服务注册中心,然后我们使 ...
- Spring Cloud 入门 之 Zuul 篇(五)
原文地址:Spring Cloud 入门 之 Zuul 篇(五) 博客地址:http://www.extlight.com 一.前言 随着业务的扩展,微服务会不对增加,相应的其对外开放的 API 接口 ...
- spring cloud 入门系列四:使用Hystrix 实现断路器进行服务容错保护
在微服务中,我们将系统拆分为很多个服务单元,各单元之间通过服务注册和订阅消费的方式进行相互依赖.但是如果有一些服务出现问题了会怎么样? 比如说有三个服务(ABC),A调用B,B调用C.由于网络延迟或C ...
- Spring Cloud 入门教程(八): 断路器指标数据监控Hystrix Dashboard 和 Turbine
1. Hystrix Dashboard (断路器:hystrix 仪表盘) Hystrix一个很重要的功能是,可以通过HystrixCommand收集相关数据指标. Hystrix Dashboa ...
- Spring Cloud(五):Hystrix 监控面板【Finchley 版】
Spring Cloud(五):Hystrix 监控面板[Finchley 版] 发表于 2018-04-16 | 更新于 2018-05-10 | 在上一篇 Hystrix 的介绍中,我们提到 ...
- Spring Cloud 入门教程(五): Ribbon实现客户端的负载均衡
接上节,假如我们的Hello world服务的访问量剧增,用一个服务已经无法承载, 我们可以把Hello World服务做成一个集群. 很简单,我们只需要复制Hello world服务,同时将原来的端 ...
- 一起来学Spring Cloud | 第五章:熔断器 ( Hystrix)
在微服务项目中,一个系统可以分割成很多个不同的服务模块,不同模块之间我们通常需要进行相互调用.springcloud中可以使用RestTemplate+Ribbon和Feign来调用(工作中基本都是使 ...
- spring cloud 入门系列:总结
从我第一次接触Spring Cloud到现在已经有3个多月了,当时是在博客园里面注册了账号,并且看到很多文章都在谈论微服务,因此我就去了解了下,最终决定开始学习Spring Cloud.我在一款阅读A ...
随机推荐
- keepalived 原理、安装与使用
1. keepalived工作原理 keepalived是集群管理中保证集群高可用的一个服务软件,其功能类似于heartbeat,用来防止单点故障. keepalived是以VRRP协议为实现基础的, ...
- Docker镜像文件操作
1什么是Docker镜像 Docker镜像是由文件系统叠加而成(是一种文件的存储形式).最底端是一个文件引导系统,即bootfs,这很像典型的Linux/Unix的引导文件系统.Docker用户几乎永 ...
- 如何用Zookeeper来实现分布式锁?
什么是Zookeeper临时顺序节点? 例如 : / 动物 植物 猫 仓鼠 荷花 松树 Zookeeper的数据存储结构就像一棵树,这棵树由节点组成,这种节点叫做Zonde.# Znode分为四种类型 ...
- C#操作高低位
比如一个数 想把高位 与地位拆开,分别显示 可以用这个办法 x=(uint16)(x>>8) (高字节向右移动8位 相当于*256) X=(UINT16)(X &0X00FF) ...
- Centos 内存释放
原因:最近发现服务器老师提示内存不足的警报,很多时候内存都占用百分之80以上,查看运行的服务似乎并没有占用很大的内存,top查看运行的服务,然后按shift+m排名第一的才百分之1.x,看了别人的博客 ...
- jsp include
1.<%@ include file="a.jsp"%> 路径无法动态赋值,只能写成固定路径: 生成一个jsp页面,整个编译 2.<jsp:include pag ...
- bzoj 4456 [Zjoi2016]旅行者
题面 https://www.lydsy.com/JudgeOnline/problem.php?id=4456 题解 分治 设当前work的区间为(x1,y1,x2,y2) 我们将长边分成两半 不妨 ...
- AtCoder Grand Contest 012 B
B - Splatter Painting Time limit : 2sec / Memory limit : 256MB Score : 700 points Problem Statement ...
- android动画(2)自定义动画
public class CustomAnimation extends Animation { // 这个方法可以获得动画view的width,height,以及它父view的width @Over ...
- 对protected修饰符的范围用代码说明(同时说明用protected修饰的属性,在继承时,一定程度上破坏了封装)
目录结构: 本类: 本包: 子孙类: 其他包: