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. (水题)洛谷 - P1464 - Function

    https://www.luogu.org/problemnew/show/P1464 #include<bits/stdc++.h> using namespace std; #defi ...

  2. spoj SUBLEX - Lexicographical Substring Search【SAM】

    先求出SAM,然后考虑定义,点u是一个right集合,代表了长为dis[son]+1~dis[u]的串,然后根据有向边转移是添加一个字符,所以可以根据这个预处理出si[u],表示串u后加字符能有几个本 ...

  3. 大数据系列文章-Hadoop的HDFS读写流程(二)

    在介绍HDFS读写流程时,先介绍下Block副本放置策略. Block副本放置策略 第一个副本:放置在上传文件的DataNode:如果是集群外提交,则随机挑选一台磁盘不太满,CPU不太忙的节点. 第二 ...

  4. GitHub笔记---邮箱访问错误

    GitHub地址太长,所以需要一个变量来保存 把远程仓库赋值给一个变量,以后就用就这变量代表这个地址 GitHub推送push 推送过程中发生一个小插曲,出现了错误,错误提示我复制过来吧 remote ...

  5. LuoguP2657 [SCOI2009]windy数 【数位dp】By cellur925

    题目传送门 题目大意:在A和B之间,包括A和B,总共有多少个不含前导零且相邻两个数字之差至少为2的正整数? 显然是数位dp啦=w=. 显然与上一位有关,我们$dfs$的时候就要记录$pre$.因为这是 ...

  6. 我的spring-boot开发环境

    我的spring-boot开发环境,目的方便我快速搭建开发环境,同时可以最佳实践.使用spring-boot 2.1.x. 代码地址:GitHub my-springboot-examples 目的是 ...

  7. ArrayList和LinkedList的共同点和区别

    ArrayList和LinkedList的相同点和不同点 共同点:都是单列集合中List接口的实现类.存取有序,有索引,可重复 不同点: 1.底层实现不同: ArrayList底层实现是数组,Link ...

  8. PyQt5编程入门

    1  25行的弹出式闹钟 import sys import time from PyQt5 import QtCore from PyQt5.QtWidgets import QLabel from ...

  9. WPF日常需要使用的操作

    窗体如何居中弹出 在窗体上添加属性  WindowStartupLocation="CenterScreen" 窗体如何隐藏掉windows边框 添加属性WindowStyle=& ...

  10. List<DTO>转 Map<String,List<DTO>> 两种写法

    List<TeamScheduleDTO> list = JSON.parseArray(response.getData().getJSONArray("list") ...