前言:

必需学会SpringBoot基础知识

简介:

spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等。它运行环境简单,可以在开发人员的电脑上跑。

工具:

JDK8

apache-maven-3.5.2

IntelliJ IDEA 2017.3 x64

一、断路器简介

Netflix开源了Hystrix组件,实现了断路器模式,SpringCloud对这一组件进行了整合。 在微服务架构中,一个请求需要调用多个服务是非常常见的; 参考图片: http://projects.spring.io/spring-cloud/spring-cloud.html#_circuit_breaker_hystrix_clients

简单来说: 在架构里面那个服务器挂了, 就会返回报错.

二、准备工作

本篇是基于第一篇为基础, 首先, 启动 eureka-server:8761 和 eureka-client 项目的dev:8762

三、在ribbon使用断路器

3.1 pom.xml 添加依赖

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

3.2 启动类追加@EnableHystrix注解开启Hystrix

package com.lwc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix; /**
* @author Eddie
*/
@EnableHystrix
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaRibbonApplication { public static void main(String[] args) {
SpringApplication.run(EurekaRibbonApplication.class, args);
}
}

3.3 Service改造, 追加功能

package com.lwc.service;

/**
* @author eddie.lee
* @Package com.lwc.service
* @ClassName RibbonService
* @description
* @date created in 2018-03-26 19:55
* @modified by
*/
public interface RibbonService { String ribbonService(String name); /**
* Hystrix 熔断 返回错误信息
*/
String hystrixError(String name);
}
package com.lwc.service.impl;

import com.lwc.service.RibbonService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; /**
* @author eddie.lee
* @Package com.lwc.service.impl
* @ClassName RibbonServiceImpl
* @description
* @date created in 2018-03-26 19:57
* @modified by
*/
@Service
public class RibbonServiceImpl implements RibbonService { @Autowired
private RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "hystrixError")
@Override
public String ribbonService(String name) {
final String url = "http://eureka-client/eureka/client?name=" + name;
return restTemplate.getForObject(url, String.class);
} @Override
public String hystrixError(String name) {
return "hi, " + name + " ,sorry, hystrix error!";
}
}

3.4 浏览器查看 A:成功, B:失败  (先是启动相关服务, 请求一次地址, 然后在关闭client服务器, 在请求一次地址)

A:  http://localhost:8764/eureka/ribbon?name=eddie

hi eddie, i am from port:8762

B: http://localhost:8764/eureka/ribbon?name=eddie

hi, eddie ,sorry, hystrix error!

四、Feign中使用断路器

4.1 pom.xml添加依赖

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

4.2 *.yml添加开启断路器

如果是properties文件,请在文件中加入:

feign.hystrix.enabled=true

如果是yml文件,请在文件中加入:

feign:

hystrix:

enabled: true

4.3 FeignClientService 改造, 添加 fallback 错误返回

package com.lwc.service;

import com.lwc.config.FeignClientServiceHystric;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam; /**
* @author eddie.lee
* @Package com.lwc.service
* @ClassName FeignClientService
* @description
* @date created in 2018-03-27 10:43
* @modified by
*/
@FeignClient(value = "eureka-client", fallback = FeignClientServiceHystric.class)
public interface FeignClientService { @GetMapping("/eureka/client")
String feignToClientByName(@RequestParam("name") String name); }
package com.lwc.config;

import com.lwc.service.FeignClientService;
import org.springframework.stereotype.Component; /**
* @author eddie.lee
* @Package com.lwc.config
* @ClassName FeignClientServiceHystric
* @description
* @date created in 2018-03-27 15:21
* @modified by
*/
@Component
public class FeignClientServiceHystric implements FeignClientService { @Override
public String feignToClientByName(String name) {
return "Hystric Error, Sorry " + name;
} }

4.4 浏览器查看 A:成功, B:失败  (先是启动相关服务, 请求一次地址, 然后在关闭client服务器, 在请求一次地址)

A:  http://localhost:8765/eureka/feign?name=eddie

hi eddie, i am from port:8762

B: http://localhost:8765/eureka/feign?name=eddie

Hystric Error, Sorry eddie

五、Hystrix Dashboard (断路器:Hystrix 仪表盘)

eureka-ribbon 或者 eureka-feign 改造, 添加仪表盘

5.1 pom.xml添加依赖

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

5.2 启动类添加注解

@EnableHystrix
@EnableHystrixDashboard

5.3 浏览器查看

地址: http://localhost:8765/hystrix.stream

输入: http://localhost:8765/hystrix.stream

Title:{name}

测试: 请求 http://localhost:8765/eureka/feign?name=eddie 就会在仪表盘出现对应数据, 比如次数

5.4 错误

提示: Unable to connect to Command Metric Stream.

解决: 查看是否缺少依赖,或者注解

六、参考资料

eureka-feign
http://localhost:8765/eureka/feign?name=eddie

eureka-ribbon
http://localhost:8764/eureka/ribbon?name=eddie


标签

4-1, 4-2

源码下载
https://github.com/eddie-code/SpringCloudDemo#springclouddemo

【SpringCloud】第四篇:断路器(Hystrix)的更多相关文章

  1. 跟我学SpringCloud | 第四篇:熔断器Hystrix

    跟我学SpringCloud | 第四篇:熔断器Hystrix 1. 熔断器 服务雪崩 在正常的微服务架构体系下,一个业务很少有只需要调用一个服务就可以返回数据的情况,这种比较常见的是出现在demo中 ...

  2. SpringCloud学习系列之三----- 断路器(Hystrix)和断路器监控(Dashboard)

    前言 本篇主要介绍的是SpringCloud中的断路器(Hystrix)和断路器指标看板(Dashboard)的相关使用知识. SpringCloud Hystrix Hystrix 介绍 Netfl ...

  3. SpringCloud教程 | 第四篇:断路器(Hystrix)

    在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用.为了保证其高可用,单个服务 ...

  4. 史上最简单的SpringCloud教程 | 第四篇:断路器(Hystrix)

    在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用.为了保证其高可用,单个服务 ...

  5. SpringCloud教程 | 第四篇:断路器(Hystrix)(Finchley版本)

    在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用.为了保证其高可用,单个服务 ...

  6. 史上最简单的SpringCloud教程 | 第四篇:断路器(Hystrix)(Finchley版本)

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f4-hystrix/ 本文出自方志朋的博客 在微服务架构中, ...

  7. springcloud 入门 6 (断路器hystrix)

    hystrix:断路器 断路器是为了解决服务故障的“雪崩”,   雪崩是指,由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请 ...

  8. Spring Cloud学习笔记【四】断路器Hystrix

    雪崩效应 在微服务架构中通常会有多个服务层调用,基础服务的故障可能会导致级联故障,进而造成整个系统不可用的情况,这种现象被称为服务雪崩效应.服务雪崩效应是一种因 “服务提供者” 的不可用导致 “服务消 ...

  9. 跟我学SpringCloud | 第十四篇:Spring Cloud Gateway高级应用

    SpringCloud系列教程 | 第十四篇:Spring Cloud Gateway高级应用 Springboot: 2.1.6.RELEASE SpringCloud: Greenwich.SR1 ...

随机推荐

  1. C#与C++通过socket传送结构体

    C#服务端: using System; using System.Net.Sockets; using System.Net; using System.IO; using System.Diagn ...

  2. VC MFC工具栏(CToolBar)控件(转)

    工具栏 工具栏控件在控件面板里没有对应的选项(图标),但有一个工具栏控件类CToolBar,所以我们如果要创建一个工具栏控件并显示在窗口里的话,只能用代码来完成,事实上任何一种控件,都可以用代码创建, ...

  3. 锐捷交换机RG-3760-24 的简单配置与VLAN搭建

    要做的事 将交换机和主机连通. 建立vlan,并将主机配置到vlan当中. 连接主机和交换机 安装配置软件 选用SecureCRT 8.0来配置交换机,可在网上下载. 插入配置线 把配置线插入cons ...

  4. TortoiseSVN 分支创建与合并

    前提准备: 确保本地Work Copy 和 服务器上的 版本一致.( 所有代码都提交到SVN,并update一次) 1  从主干创建分支代码 在本地Work Copy  选中项目文件夹,鼠标右键选择 ...

  5. REST解惑

    本文是「架构风格:你真的懂REST吗?」的补充! REST全称是Representational State Transfer,目前普遍接受的中文翻译为「表述性状态转移」! 即使翻译过来了,你依然有一 ...

  6. 系统优化怎么做-Tomcat优化

    大家好,这里是「聊聊系统优化 」,并在下列地址同步更新 博客园:http://www.cnblogs.com/changsong/ 知乎专栏:https://zhuanlan.zhihu.com/yo ...

  7. Jumpserver堡垒机搭建(脚本自动化)

    #!/bin/bash # coding: utf- # Copyright (c) set -e #返回值为非0时,退出脚本 echo "0. 系统的一些配置" setenfor ...

  8. Reading Notes : 180214 计算机的总线结构

    读书<计算机组成原理>,百度百科 基本上接触过计算机的人,都多少知道计算机的具体构成,但是真正能讲明白的却说了很多,本节将讲解一下计算机的基本硬件构成和一些基本信息,简单认识,以后再深入了 ...

  9. Notes 20180306 : 变量与常量

    1.1 变量与常量 我们在开发中会经常听到常量和变量,那么常量和变量指的又是什么呢?顾名思义,在程序执行过程中,其值不能被改变的量称为常量,其值能被改变的量称为变量.变量与常量的命名都必须使用合法的标 ...

  10. python 输入一个整数,判断其是否既是3的倍数,又是5的倍数

    v = int(input('请输入一个整数:')) if v % 3 == 0 and v % 5 ==0: print(v,'即是3的倍数又是5的倍数') else: print('不是3或5的倍 ...