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. python __builtins__ filter类 (24)

    24.'filter', 用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表.该接收两个参数,第一个为函数,第二个为序列,序列的每个元素作为参数传递给函数进行判,然后返回 True ...

  2. 51Nod 1174 区间中最大的数(RMQ)

    #include <iostream> #include <algorithm> #include <cstring> using namespace std; + ...

  3. 51Nod 1019 逆序数 (归并排序)

    #include <iostream> #include <cstring> using namespace std; ; int a[maxn]; int res[maxn] ...

  4. hdu1325 Is It A Tree? 基础并查集

    #include <stdio.h> #include <string.h> ], g[]; int find(int x) //并查集的查找,找到共同的父亲 { if (f[ ...

  5. Jmeter之聚合报告

    1.添加线程组,添加请求接口 2.设置线程组 3.线程组右击添加—>监听器—>聚合报告

  6. 网络流24题 一句话题解(updating)

      搭配飞行员问题   最简单的一道题 就是一个二分图匹配   太空飞行计划   最大权闭合子图 什么叫"最大权闭合子图"呢? 就是给定一个有向图,在里面选择一个点集,使得点集中的 ...

  7. 168 Excel Sheet Column Title Excel表列名称

    给定一个正整数,返回它在Excel表中相对应的列名称.示例:    1 -> A    2 -> B    3 -> C    ...    26 -> Z    27 -&g ...

  8. gbk编码文件传输json实例

    cline.php <?php $str='此地无银三百两'; $str = iconv('gbk', 'utf-8', $str); //Json只支持utf-8编码,如果不进行转码的话,服务 ...

  9. SonarQube+Svn+Jenkins环境搭建----问题总结

    1.配置SVN后提示unable to access to repository,原因是使用的账户没有访问svn的权限,创建新的用户即可.注意新的用户,用户名,密码要跟svn上的权限一致.     创 ...

  10. ASP.NET Web API 2 框架揭秘

    这不是一本传统意义上的入门书籍 任何 —本书都具有对应的受众群体,所以我不得不将这句话放在最前面,并且希望所有 打算购买此书的读者能够看到.如果你之前对As氵NET W山API(或者AsPNET MⅤ ...