原文:https://www.cnblogs.com/songlu/p/9949203.html

1、启动【服务中心】集群,工程名:springcloud-eureka-server

2、启动【服务提供者】集群,工程名:springcloud-eureka-client

3、启动【服务消费者】,工程名:springcloud-eureka-ribbon

4、未加入熔断机制,【服务提供者】出现问题,对【服务消费者】的影响

4.1、停掉【服务提供者】集群中的其中一个服务。本例:停掉端口为 52602 这个服务。

4.2、开浏览器窗口,访问 http://localhost:52610/ribbonInfo,多次刷新该地址

调用 52601、52603 服务是正常的,但是调用52602服务的时候,出现了阻塞等待,并最终返回了红框内的错误信息。

假如这是正式的生产环境,访问量很大的情况下,那么就会有很多请求阻塞。这会造成【服务消费者】服务器内存消耗陡增,导致应用崩溃。如果有其他应用需要【服务消费者】返回资源信息,那么调用【服务消费者】的应用也会出现阻塞。这就导致了连锁反应,也就是所谓的雪崩。

所以,为了避免这种悲剧发生。顺应而生的出现了熔断保护机制。即:访问不通时,要及时作出响应,而不是等待至超时。

5、修改【服务消费者】,加入熔断机制

5.1、打开工程:springcloud-eureka-ribbon

5.2、工程pom.xml文件添加如下依赖:

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

5.3、修改工程启动类中,添加注解 @EnableHystrix

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.miniooc.eurekaribbon;
 
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;
 
/**
 * EurekaRibbonApplication
 * 应用程序启动类,程序入口
 *
 * @author 宋陆
 * @version 1.0.0
 */
@EnableHystrix // 启用 hystrix 熔断机制相关配置
@EnableDiscoveryClient // 启用 Eureka 服务发现 相关配置
@SpringBootApplication
public class EurekaRibbonApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(EurekaRibbonApplication.class, args);
    }
 
}

5.4、修改服务消费者 Service 类, EurekaRibbonService

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.miniooc.eurekaribbon.service;
 
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;
 
/**
 * EurekaRibbonService
 * 服务消费者服务类,调用服务提供者提供的服务,实现业务
 *
 * @author 宋陆
 * @version 1.0.0
 */
@Service
public class EurekaRibbonService {
 
    @Autowired
    RestTemplate restTemplate;
 
    /**
     *  加入了@HystrixCommand注解,并设置了服务调用失败的回调方法 fallbackMethod = "getInfoFailure"
     * @return
     */
    @HystrixCommand(fallbackMethod = "getInfoFailure")
    public String getInfo() {
        String message;
        try {
            System.out.println("调用 服务 EUREKA-CLIENT/info");
            message = restTemplate.getForObject("http://EUREKA-CLIENT/info", String.class);
            System.out.println("服务 EUREKA-CLIENT/info 返回信息 : " + message);
            System.out.println("调用 服务 EUREKA-CLIENT/info 成功!");
        } catch (Exception ex) {
            message = ex.getMessage();
        }
        return message;
    }
 
    /**
     * 服务 EUREKA-PROVIDER/hello 调用失败的回调方法
     *
     * @return
     */
    public String getInfoFailure() {
        String message = "网络繁忙,请稍后再试-_-。PS:服务消费者自己提供的信息";
        return message;
    }
 
}

6、加入熔断机制后,【服务提供者】出现问题,对【服务消费者】的影响

6.1、重启【服务提供者】集群,重启【服务消费者】

6.2、停掉【服务提供者】集群中的其中一个服务。本例:停掉端口为 52602 这个服务。

6.3、开浏览器窗口,访问 http://localhost:52610/ribbonInfo,多次刷新该地址

调用 52601、52603 服务是正常的,调用52602服务的时候,没有出现阻塞等待,而是返回了【服务消费者】自己提供的返回信息

【服务消费者】加入熔断机制结束

SpringCloud2.0 Hystrix Ribbon 基于Ribbon实现断路器的更多相关文章

  1. SpringCloud2.0 Hystrix Feign 基于Feign实现断路器

    原文:https://www.cnblogs.com/songlu/p/9968953.html 1.启动[服务中心]集群,工程名:springcloud-eureka-server 参考 Sprin ...

  2. SpringCloud2.0 Hystrix Feign 基于Feign实现断路器 基础教程(七)

    1.启动[服务中心]集群,工程名:springcloud-eureka-server 参考 SpringCloud2.0 Eureka Server 服务中心 基础教程(二) 2.启动[服务提供者]集 ...

  3. SpringCloud2.0 Hystrix Ribbon 基于Ribbon实现断路器 基础教程(六)

    1.启动[服务中心]集群,工程名:springcloud-eureka-server 参考 SpringCloud2.0 Eureka Server 服务中心 基础教程(二) 2.启动[服务提供者]集 ...

  4. SpringCloud2.0 Hystrix Dashboard 断路器指标看板

    原文:https://www.cnblogs.com/songlu/p/9973856.html 1.启动基础工程 1.1.启动[服务中心]集群,工程名称:springcloud-eureka-ser ...

  5. SpringCloud2.0 Hystrix Dashboard 断路器指标看板 基础教程(八)

    1.启动基础工程 1.1.启动[服务中心]集群,工程名称:springcloud-eureka-server 参考 SpringCloud2.0 Eureka Server 服务中心 基础教程(二) ...

  6. SpringCloud2.0 Turbine 断路器集群监控 基础教程(九)

    1.启动基础工程 1.1.启动[服务中心]集群,工程名称:springcloud-eureka-server 参考 SpringCloud2.0 Eureka Server 服务中心 基础教程(二) ...

  7. SpringCloud2.0 Zuul 网关路由 基础教程(十)

    1.启动基础工程 1.1.启动[服务注册中心],工程名称:springcloud-eureka-server 参考 SpringCloud2.0 Eureka Server 服务中心 基础教程(二) ...

  8. 玩转SpringCloud(F版本) 三.断路器(Hystrix)RestTemplate+Ribbon和Feign两种方式

    此文章基于: 玩转SpringCloud 一.服务的注册与发现(Eureka) 玩转SpringCloud 二.服务消费者(1)ribbon+restTemplate 转SpringCloud 二.服 ...

  9. SpringCloud2.0 Ribbon 服务发现 基础教程(四)

    1.启动[服务中心]集群,即 Eureka Server 参考 SpringCloud2.0 Eureka Server 服务中心 基础教程(二) 2.启动[服务提供者]集群,即 Eureka Cli ...

随机推荐

  1. IPython 安装

    从 python 第三方库列表下载安装包 https://www.lfd.uci.edu/~gohlke/pythonlibs/ 然后使用 pip 进行安装 软件会自动安装到 python 安装路径的 ...

  2. 如何保证redis数据都是热点数据

    mySQL里有2000w数据,redis中只存20w的数据,如何保证redis中的数据都是热点数据? 1.限定 Redis 占用的内存,Redis 会根据自身数据淘汰策略,加载热数据到内存.所以,计算 ...

  3. 《Linux就该这么学》培训笔记_ch16_使用Squid部署代理缓存服务

    <Linux就该这么学>培训笔记_ch16_使用Squid部署代理缓存服务 文章最后会post上书本的笔记照片. 文章主要内容: 代理缓存服务 配置Squid服务程序 正向代理 标准正向代 ...

  4. maven site 命令报错

    在执行 mvn site 命令时报错: java.lang.NoClassDefFoundError: org/apache/maven/doxia/siterenderer/DocumentCont ...

  5. Python 中如何判断 list 中是否包含某个元素

    在python中判断 list 中是否包含某个元素: ——可以通过in和not in关键字来判读 例如: abcList=['a','b','c',1,2,3] if 'a' in abcList: ...

  6. 浅谈设计模式-visitor访问者模式

    先看一个和visitor无关的案例.假设你现在有一个书架,这个书架有两种操作,1添加书籍2阅读每一本书籍的简介. //书架public class Bookcase { List<Book> ...

  7. myeclipse导入项目后中文乱码

    window----preference ----general-----workspace-------text file encoding 可以多测试下:gbk,utf-8.....

  8. 【转】用 async/await 来处理异步

    原文地址:https://www.cnblogs.com/SamWeb/p/8417940.html 昨天看了一篇vue的教程,作者用async/ await来发送异步请求,从服务端获取数据,代码很简 ...

  9. Topshelf 搭建 Windows 服务

    Topshelf 是一个用来部署基于.NET Framework 开发的服务的框架.简化服务创建于部署过程,并且支持控制台应用程序部署为服务.本文基于 .net core 控制台应用程序部署为服务(. ...

  10. 第十七节:.Net Core中新增HttpClientFactory的前世今生

    一. 背景 1.前世 提到HttpClient,在传统的.Net版本中简直臭名昭著,因为我们安装官方用法 using(var httpClient = new HttpClient()),当然可以Di ...