原文: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. JS 实现复制、全选文本

    先上效果,由于截图,高亮蓝色成了灰色 参考这位大佬的   https://www.cnblogs.com/dreambin/p/9046999.html HTML 部分 <form name=t ...

  2. SCDM导入点数据

    我们有时候需要把外部的点导入SCDM当中,但是SCDM没有像ICEM或者DM那样直接提供点导入的选项,是不是SCDM就无法导入点的数据了呢?答案当然是否定的.把点导入SCDM中的方法总结如下(示例数据 ...

  3. Zuul之路由熔断

    Zuul作为Netflix组件,可以与Ribbon.Eureka.Hystrix等组件结合,实现负载均衡.熔断器的功能 Spring boot2X集成zuul与consul实现负载均衡和反向代理 当后 ...

  4. zabbix 搭建 mysql 连接报错

    如图所示: 查看 MySQL的配置文件 [root@zbxtest ~]# cat /etc/my.cnf [mysqld] datadir=/data/mysql socket=/data/mysq ...

  5. SuperMemo

    SuperMemo Your friend, Jackson is invited to a TV show called SuperMemo in which the participant is ...

  6. Symbol 小妙处

    input 框输入后发送异步请求,页面拿到响应进行渲染.但偶尔会遇到问题:响应内容和输入结果不一致.因为 http 无法保证响应到达的顺序. 如何解决呢?提供一个小思路. myRequest.js i ...

  7. ThinkPHP5 使用 JWT 进行加密

    使用 Github 的 firebase\JWT - 使用 Composer 安装此扩展 - 代码示例 <?php /** * [InterCommon-接口公用] * @Author Rain ...

  8. Java选择结构和循环结构

    1.选择结构 ①.ifif(){ } if(){}else{} if(){}else if(){}else if(){}else{} ②.switch switch (表达式) { case 常量 1 ...

  9. 2019 C/C++《阿里》面试题总结

    一.C和C++的区别是什么? C是面向过程的语言,C++是在C语言的基础上开发的一种面向对象编程语言,应用广泛. C中函数不能进行重载,C++函数可以重载 C++在C的基础上增添类,C是一个结构化语言 ...

  10. day58——模板继承、组件、自定义标签和过滤器、inclusion_tag、静态文件配置、url别名和反向解析、url命名空间

    day58 模板相关 模板继承(母版继承) 1. 创建一个xx.html页面(作为母版,其他页面来继承它使用) 2. 在母版中定义block块(可以定义多个,整个页面任意位置) {% block co ...