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

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

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

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

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

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

4.2、打开浏览器,访问 http://localhost:52620/feignInfo,多次刷新该地址

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

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

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

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

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

5.2、修改工程 pom.xml 文件,追加 Hystrix 依赖,添加如下内容:

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
25
26
27
package com.miniooc.eurekafeign;
 
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.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
 
/**
 * EurekaFeignApplication
 * 应用程序启动类,程序入口
 *
 * @author 宋陆
 * @version 1.0.0
 */
@EnableHystrix // Feign默认是开启,这个注解可以不加的
@EnableDiscoveryClient // 启用 Eureka 服务发现
@EnableFeignClients // 启用 Feign
@SpringBootApplication
public class EurekaFeignApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(EurekaFeignApplication.class, args);
    }
 
}

5.4、修改【服务消费者】服务类, EurekaFeignService,追加 fallback 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.miniooc.eurekafeign.service;
 
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
 
/**
 * EurekaFeignService
 * 服务消费者,调用服务提供者提供的服务,实现业务
 *
 * @author 宋陆
 * @version 1.0.0
 */
@FeignClient(value = "EUREKA-CLIENT", fallback = EurekaFeignServiceFailure.class) // 调用的服务的名称
public interface EurekaFeignService {
 
    @RequestMapping(value = "/info")
    String getInfo();
 
}

5.5、新增【服务消费者】服务调用失败,回调处理类,EurekaFeignServiceFailure

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.miniooc.eurekafeign.service;
 
import org.springframework.stereotype.Service;
 
/**
 * EurekaFeignServiceFailure
 * 服务消费者,调用服务提供者提供的服务失败,回调处理类
 *
 * @author 宋陆
 * @version 1.0.0
 */
@Service
public class EurekaFeignServiceFailure implements EurekaFeignService {
 
    @Override
    public String getInfo() {
        String message = "网络繁忙,请稍后再试-_-。PS:服务消费者自己提供的信息";
        return message;
    }
 
}

5.6、修改工程配置文件 application.yml, 开启 hystrix,追加如下配置

1
2
3
feign:
  hystrix:
    enabled: true

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

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

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

6.3、打开浏览器,访问 http://localhost:52620/feignInfo,多次刷新该地址

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

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

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

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

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

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

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

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

    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. SpringCloud2.0 Feign 服务发现 基础教程(五)

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

  9. SpringCloud 2020.0.4 系列之 Feign

    1. 概述 老话说的好:任何问题都有不止一种的解决方法,当前的问题没有解决,只是还没有发现解决方法,而并不是无解. 言归正传,之前我们聊了 SpringCloud 的服务治理组件 Eureka,今天我 ...

随机推荐

  1. 学习-spring data jpa

    spring data jpa对照表 Keyword Sample JPQL snippet And findByLastnameAndFirstname - where x.lastname = ? ...

  2. 【Step-By-Step】第 三 周

    本周面试题一览: 什么是XSS攻击,XSS 攻击可以分为哪几类?我们如何防范XSS攻击? 如何隐藏页面中的某个元素? 浏览器事件代理机制的原理是什么? setTimeout 倒计时为什么会出现误差? ...

  3. [LeetCode] 918. Maximum Sum Circular Subarray 环形子数组的最大和

    Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...

  4. [LeetCode] 592. Fraction Addition and Subtraction 分数加减法

    Given a string representing an expression of fraction addition and subtraction, you need to return t ...

  5. 关于Adobe Premiere Pro视音频不同步的解决方法

    在Potplayer和系统播放器都正常 但Pr里面就是音画不同步 原因是Pr识别错误,让音频比视频快了,这时要将视频后拉一点就ok了

  6. leetcode 903. DI序列的有效排列

    题目描述: 我们给出 S,一个源于 {'D', 'I'} 的长度为 n 的字符串 .(这些字母代表 “减少” 和 “增加”.)有效排列 是对整数 {0, 1, ..., n} 的一个排列 P[0], ...

  7. python2升级python3

    需求: centos环境,python2.7需要升级为python3.x 1.请先手动(再次)安装 openssl .否则你升级之后,你的pip不能下载,会各种报错的. 比如这种错误: ImportE ...

  8. linux写shell注意的问题

    linux写shell注意的问题一定要vi crontab.sh来写 ps:在windows系统中编辑过这个文件,就会出现类似的换行符 这样导致linux系统中运行sh报错 比如会出现$MQ字符 如果 ...

  9. 动态引用存储——集合&&精确的集合定义——泛型

    1,集合宏观理解 1.1,为什么引入集合? 对于面向对象的语言来说,操作对象的功能不可或缺. 为了方便对对象进行操作和处理,就必须要对对象进行暂时的存储.[数据最终存在数据库里] 使用数组来存储对象的 ...

  10. [转帖]ASML EUV光刻机累计生产450万块晶圆:一台12亿元

    ASML EUV光刻机累计生产450万块晶圆:一台12亿元 来源驱动之家 ...网页被我关了 就这样吧. 截至目前,华为麒麟990 5G是唯一应用了EUV极紫外光刻的商用芯片,台积电7nm EUV工艺 ...