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的更多相关文章

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

    原文地址:Spring Cloud 入门 之 Hystrix 篇(四) 博客地址:http://www.extlight.com 一.前言 在微服务应用中,服务存在一定的依赖关系,如果某个目标服务调用 ...

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

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

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

    原文地址:Spring Cloud 入门 之 Zuul 篇(五) 博客地址:http://www.extlight.com 一.前言 随着业务的扩展,微服务会不对增加,相应的其对外开放的 API 接口 ...

  4. spring cloud 入门系列四:使用Hystrix 实现断路器进行服务容错保护

    在微服务中,我们将系统拆分为很多个服务单元,各单元之间通过服务注册和订阅消费的方式进行相互依赖.但是如果有一些服务出现问题了会怎么样? 比如说有三个服务(ABC),A调用B,B调用C.由于网络延迟或C ...

  5. Spring Cloud 入门教程(八): 断路器指标数据监控Hystrix Dashboard 和 Turbine

    1. Hystrix Dashboard (断路器:hystrix 仪表盘)  Hystrix一个很重要的功能是,可以通过HystrixCommand收集相关数据指标. Hystrix Dashboa ...

  6. Spring Cloud(五):Hystrix 监控面板【Finchley 版】

    Spring Cloud(五):Hystrix 监控面板[Finchley 版]  发表于 2018-04-16 |  更新于 2018-05-10 |  在上一篇 Hystrix 的介绍中,我们提到 ...

  7. Spring Cloud 入门教程(五): Ribbon实现客户端的负载均衡

    接上节,假如我们的Hello world服务的访问量剧增,用一个服务已经无法承载, 我们可以把Hello World服务做成一个集群. 很简单,我们只需要复制Hello world服务,同时将原来的端 ...

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

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

  9. spring cloud 入门系列:总结

    从我第一次接触Spring Cloud到现在已经有3个多月了,当时是在博客园里面注册了账号,并且看到很多文章都在谈论微服务,因此我就去了解了下,最终决定开始学习Spring Cloud.我在一款阅读A ...

随机推荐

  1. 使用PDO操作数据库的好处

    PDO一是PHP数据对象(PHP Data Object)的缩写. 并不能使用PDO扩展本身执行任何数据库操作,必须使用一个database-specific PDO driver(针对特定数据库的P ...

  2. Linux 问题 卸载setup.py方式安装的python包

    python ./setup.py install --record install.txt  cat install.txt | xargs rm -rf

  3. bzoj 4753: [Jsoi2016]最佳团体【01分数规划+二分+树上背包】

    01分数规划,二分答案然后把判别式变成Σp[i]-Σs[i]*mid>=0,然后树上背包判断,设f[i][j]为在i点子树里选j个的最大收益,随便背包一下就好 最丧病的是神卡常--转移的时候要另 ...

  4. Codeforces731E Funny Game

    dp[i][0]表示从i出发,轮到先手走的最优值. dp[i][1]表示从i出发,轮到后手走的最优值. dp[i][0]=max(dp[j][1]+sum[j]) dp[i][1]=min(dp[j] ...

  5. JZOJ4307. 【NOIP2015模拟11.3晚】喝喝喝

    Description

  6. Angular 项目开发中父子组件传参

    在项目开发中经常会遇到 组件之间传参的问题.今天总结下在使用angular的项目中父子组件传参的问题: 1.父组件向子组件传参: 然后在父组件中 然后在父组件的html中 然后就可以在子组件中使用了 ...

  7. PJzhang:微信公众号短连接与微信好友验证

    猫宁!!! 参考链接:https://mp.weixin.qq.com/s/LPuYrDEyEXHyhcK3_HokSg 之前看到他们有人把微信公众号文章的长链接转为短链接,很受用,百度搜索一下方法, ...

  8. CentOS7下如何正确安装并启动Docker(图文详解)

    我使用了CentOS 7操作系统,可以非常容易地安装Docker环境.假设,下面我们都是用root用户进行操作,执行如下命令进行准备工作: yum install -y yum-utils yum-c ...

  9. Investigation LightOJ - 1068

    Investigation LightOJ - 1068 常规数位dp题,对于不同k分开记忆化.注意:k大于82(1999999999的数位和)时不会有答案,直接输出0即可.还有,按照这种记录不同k时 ...

  10. Android APK加壳技术方案

    Android APK加壳技术方案[1] Android APK加壳技术方案[2]