前言:

必需学会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 添加依赖

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-hystrix</artifactId>
  4. </dependency>

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

  1. package com.lwc;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  6. import org.springframework.cloud.netflix.hystrix.EnableHystrix;
  7.  
  8. /**
  9. * @author Eddie
  10. */
  11. @EnableHystrix
  12. @EnableDiscoveryClient
  13. @SpringBootApplication
  14. public class EurekaRibbonApplication {
  15.  
  16. public static void main(String[] args) {
  17. SpringApplication.run(EurekaRibbonApplication.class, args);
  18. }
  19. }

3.3 Service改造, 追加功能

  1. package com.lwc.service;
  2.  
  3. /**
  4. * @author eddie.lee
  5. * @Package com.lwc.service
  6. * @ClassName RibbonService
  7. * @description
  8. * @date created in 2018-03-26 19:55
  9. * @modified by
  10. */
  11. public interface RibbonService {
  12.  
  13. String ribbonService(String name);
  14.  
  15. /**
  16. * Hystrix 熔断 返回错误信息
  17. */
  18. String hystrixError(String name);
  19. }
  1. package com.lwc.service.impl;
  2.  
  3. import com.lwc.service.RibbonService;
  4. import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. import org.springframework.web.client.RestTemplate;
  8.  
  9. /**
  10. * @author eddie.lee
  11. * @Package com.lwc.service.impl
  12. * @ClassName RibbonServiceImpl
  13. * @description
  14. * @date created in 2018-03-26 19:57
  15. * @modified by
  16. */
  17. @Service
  18. public class RibbonServiceImpl implements RibbonService {
  19.  
  20. @Autowired
  21. private RestTemplate restTemplate;
  22.  
  23. @HystrixCommand(fallbackMethod = "hystrixError")
  24. @Override
  25. public String ribbonService(String name) {
  26. final String url = "http://eureka-client/eureka/client?name=" + name;
  27. return restTemplate.getForObject(url, String.class);
  28. }
  29.  
  30. @Override
  31. public String hystrixError(String name) {
  32. return "hi, " + name + " ,sorry, hystrix error!";
  33. }
  34. }

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添加依赖

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-hystrix</artifactId>
  4. </dependency>

4.2 *.yml添加开启断路器

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

feign.hystrix.enabled=true

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

feign:

hystrix:

enabled: true

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

  1. package com.lwc.service;
  2.  
  3. import com.lwc.config.FeignClientServiceHystric;
  4. import org.springframework.cloud.netflix.feign.FeignClient;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RequestParam;
  7.  
  8. /**
  9. * @author eddie.lee
  10. * @Package com.lwc.service
  11. * @ClassName FeignClientService
  12. * @description
  13. * @date created in 2018-03-27 10:43
  14. * @modified by
  15. */
  16. @FeignClient(value = "eureka-client", fallback = FeignClientServiceHystric.class)
  17. public interface FeignClientService {
  18.  
  19. @GetMapping("/eureka/client")
  20. String feignToClientByName(@RequestParam("name") String name);
  21.  
  22. }
  1. package com.lwc.config;
  2.  
  3. import com.lwc.service.FeignClientService;
  4. import org.springframework.stereotype.Component;
  5.  
  6. /**
  7. * @author eddie.lee
  8. * @Package com.lwc.config
  9. * @ClassName FeignClientServiceHystric
  10. * @description
  11. * @date created in 2018-03-27 15:21
  12. * @modified by
  13. */
  14. @Component
  15. public class FeignClientServiceHystric implements FeignClientService {
  16.  
  17. @Override
  18. public String feignToClientByName(String name) {
  19. return "Hystric Error, Sorry " + name;
  20. }
  21.  
  22. }

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添加依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-actuator</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.cloud</groupId>
  7. <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
  8. </dependency>

5.2 启动类添加注解

  1. @EnableHystrix
  1. @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

  1.  

标签

  1. 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. PHP日志 LOG4PHP 的配置与使用

    维护了 一个老项目, 没有日志功能, 就给加了这个log4php,  主要是集成进去很简单,使用起来也够用了. 1.下载log4php 2.创建配置文件 log4php_config.xml < ...

  2. Css3 实现关键帧动画

    <div class="person"> </div> <script> var str1 = "@keyframes move{&q ...

  3. ssh启动失败

    调试了两个小时.ssh启动不了. service ssh start /etc/init.d/ssh start 都尝试了,还是没法启动. [ 是否启动,可以命令行: ps -s | grep ssh ...

  4. git提交项目

    https://www.cnblogs.com/java-maowei/p/5950930.html

  5. 关于$NOIP2017$的题目讲解

    关于\(NOIP2017\)的题目讲解 1.小凯的疑惑 题目描述: 小凯手中有两种面值的金币,两种面值均为正整数且彼此互素.每种金币小凯都有 无数个.在不找零的情况下,仅凭这两种金币,有些物品他是无法 ...

  6. 2018年暑假ACM个人训练题6 解题报告

    A:水题 https://www.cnblogs.com/yinbiao/p/9311834.html B:考察进制的转化 https://www.cnblogs.com/yinbiao/p/9311 ...

  7. Docker 三种UI管理平台

    docker集中化web管理平台 一.shipyard 1.启动docker,下载镜像 # systemctl restart docker # docker pull alpine # docker ...

  8. javascript根据文件字节数返回文件大小

    function getFileSize(fileByte) { var fileSizeByte = fileByte; var fileSizeMsg = ""; if(fil ...

  9. mvc 页面 去掉转义字符

    mvc 页面 去掉转义字符   mvc 后台返回json数据,用ViewBag 传回前台页面,但是传到前台页面的时候,带有转义字符.一直想去掉这个转义字符,苦恼了好久. 解决方案: mvc 页面有个这 ...

  10. Web移动端商城 移动端商城手机网站html整套模板,web移动商城仿app手机模板下载

    --Web移动端商城移动端商城手机网站html整套模板,web移动商城仿app手机模板下载.原生的js和jquery-1.6.2.min.js,页面才有html5自适应.包括首页(轮播,导航).兼职( ...