Spring Cloud Circuit Breaker 使用示例
Spring Cloud Circuit Breaker 使用示例
作者: Grey
原文地址:
博客园:Spring Cloud Circuit Breaker 使用示例
CSDN:Spring Cloud Circuit Breaker 使用示例
说明
Spring Cloud Circuit breaker提供了一个跨越不同断路器实现的抽象。它提供了一个一致的API,可以在你的应用程序中使用,允许你的开发者选择最适合你的应用程序需求的断路器实现。
它还支持的实现有如下几种
完整代码
spring-cloud-circuit-breaker-usage
环境
JDK 1.8+
Maven 3.5+
Spring Boot 版本:2.7.5
Spring Cloud 版本:2021.0.5
项目结构和说明
- spring-cloud-circuit-breaker-usage:父项目名称
- server : 服务端端模块
- src/
- pom.xml
- client : 客户端模块
- src/
- pom.xml
- pom.xml:父项目 pom 配置
- server : 服务端端模块
代码说明
服务端需要引入如下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
然后暴露一个简单服务
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
@RestController
public class BookController {
@RequestMapping(value = "/recommended")
public Mono<String> readingList() {
return Mono.just("book1,book2,book3");
}
}
暴露端口
server.port=8090
客户端的配置也很简单,核心在ReactiveCircuitBreaker
的初始化,客户端的依赖如下
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-circuitbreaker-reactor-resilience4j</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.circuitbreaker.ReactiveCircuitBreaker;
import org.springframework.cloud.client.circuitbreaker.ReactiveCircuitBreakerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
@Service
public class BookService {
private static final Logger LOG = LoggerFactory.getLogger(BookService.class);
private final WebClient webClient;
private final ReactiveCircuitBreaker readingListCircuitBreaker;
public BookService(ReactiveCircuitBreakerFactory circuitBreakerFactory) {
this.webClient = WebClient.builder().baseUrl("http://localhost:8090").build();
this.readingListCircuitBreaker = circuitBreakerFactory.create("recommended");
}
public Mono<String> readingList() {
return readingListCircuitBreaker.run(webClient.get().uri("/recommended").retrieve().bodyToMono(String.class), throwable -> {
LOG.warn("Error making request to book service", throwable);
return Mono.just("local store book");
});
}
}
如果需要配置一些熔断条件,则做如下设置即可
CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom()
.failureRateThreshold(50)
.waitDurationInOpenState(Duration.ofMillis(1000))
.slidingWindowSize(2)
.build();
TimeLimiterConfig timeLimiterConfig = TimeLimiterConfig.custom()
.timeoutDuration(Duration.ofSeconds(4))
.build();
this.readingListCircuitBreaker = circuitBreakerFactory.configureDefault(
id -> new Resilience4JConfigBuilder(id)
.timeLimiterConfig(timeLimiterConfig)
.circuitBreakerConfig(circuitBreakerConfig)
客户端暴露一个接口用于测试
@RestController
public class ReadingController {
private final BookService bookService;
public ReadingController(BookService bookService) {
this.bookService = bookService;
}
@RequestMapping("/to-read")
public Mono<String> toRead() {
return bookService.readingList();
}
}
客户端设置端口
server.port=8080
接下来,启动服务端,然后启动客户端,用 Postman 或者其他相关工具访问: http://localhost:8080/to-read
然后把服务端停止,模拟服务中断,再次访问: http://localhost:8080/to-read
显示了降级后的内容。
参考文档
spring-cloud-circuitbreaker-demo
Spring Cloud Circuit Breaker Guide
Quick Guide to Spring Cloud Circuit Breaker
Spring Cloud Circuit Breaker 使用示例的更多相关文章
- Spring Cloud App(Service) Pom示例
都配对了才能找到jar包(无法访问外网时是如何配的?) parent dependencyManageMent repositories plugInRepositories <groupId& ...
- Spring Cloud Hystrix理解与实践(一):搭建简单监控集群
前言 在分布式架构中,所谓的断路器模式是指当某个服务发生故障之后,通过断路器的故障监控,向调用方返回一个错误响应,这样就不会使得线程因调用故障服务被长时间占用不释放,避免故障的继续蔓延.Spring ...
- 微服务组件--限流框架Spring Cloud Hystrix分析
Hystrix的介绍 [1]Hystrix是springCloud的组件之一,Hystrix 可以让我们在分布式系统中对服务间的调用进行控制加入一些调用延迟或者依赖故障的容错机制. [2]Hystri ...
- Spring Cloud是怎么运行的?
导读 在之前的文章中给大家介绍了Spring Boot的基本运行原理(链接),收到了很多读者朋友们关于目前比较流行的微服务框架Spring Cloud的问题反馈.因此,在这篇文章中小码哥打算和大家一起 ...
- Spring Cloud 学习--Hystrix应用
上一篇介绍了Hystrix基本功能和单独使用的方式,今天继续学习如何将Hystrix融入SpringCloud组件中去. 在Ribbon上使用熔断器 在 pom.xml 文件中引入 hystrix 的 ...
- Spring Cloud Gateway的断路器(CircuitBreaker)功能
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- spring cloud 学习(1) - 基本的SOA示例
有过dubbo/dubbox使用经验的朋友,看到下面这张图,一定很熟悉,就是SOA架构的最基本套路. 与dubbo对比,上图的3大要素中,spring cloud是借助以下组件来实现的: 1.注册中心 ...
- 《Spring Cloud构建微服务架构》系列博文示例
SpringCloud-Learning 源码下载地址:http://download.csdn.net/detail/k21325/9650968 本项目内容为Spring Cloud教 ...
- Spring Cloud 入门教程(二): 配置管理
使用Config Server,您可以在所有环境中管理应用程序的外部属性.客户端和服务器上的概念映射与Spring Environment和PropertySource抽象相同,因此它们与Spring ...
- Spring Cloud(Dalston.SR5)--Feign 声明式REST客户端
Spring Cloud 对 Feign 进行了封装,集成了 Ribbon 并结合 Eureka 可以实现客户端的负载均衡,Spring Cloud 实现的 Feign 客户端类名为 LoadBala ...
随机推荐
- ESP8266 NONOS SDK学习
一.概况 1.存储 ESP8266 带有 160 KB 的 RAM,其中 64 KB 为 iRAM,96 KB 为 dRAM.iRAM 进一步 分成两块:32 KB iRAM 块运行标有 IRAM_A ...
- day35-IO流02
JavaOI流02 4.常用的类 4.1文件字节流输入流-FileInputStream InputStream抽象类是所有类字节输入流的超类 InputStream常用的子类: FileInputS ...
- PHP函数小工具
PHP检测IP是否内网地址.保留地址 /** * @param string $ip 被检测的IP * @return bool 是否内网或者保留IP */ public function isInt ...
- Docker 数据共享与持久化
- 第三章:模版层 - 2:Django内置模板标签
Django内置标签总览 可以查询下表来总览Django的内置标签: 标签 说明 autoescape 自动转义开关 block 块引用 comment 注释 csrf_token CSRF令牌 cy ...
- SQL通用语法和SQL分类
SQL通用语法 1.SQL 语句可以单行或多行书写,以分号结尾 2.可使用空格和缩进来增强语句的可读性 3.MySQL 数据库的SQL语句不区分大小写,关键字建议使用大写 4.3种注释 单行注释: - ...
- 第 46 届 ICPC 国际大学生程序设计竞赛亚洲区域赛(沈阳)
有时候,很简单的模板题,可能有人没有做出来,(特指 I ),到时候一定要把所有的题目全部看一遍 目录 B 题解 E F 题解 H I 题解&代码 J B 输入样例 3 2 1 2 1 2 3 ...
- laravel config()获取null
常规开发获取config值的是否发现获取值为null找了下文档,发现laravel是要把config注册到$app里面. 找到这个目录.加入这一行.就可以了
- linux 自动备份mysql数据库
今天一早打开服务器.13W个木马.被爆破成功2次,漏洞3个.数据库被删.这是个悲伤的经历 还好之前有备份,服务器也升级了安全机制,只是备份是上个月的备份.所以想写个脚本,试试自动备份数据库. 1. 先 ...
- Vue学习之--------组件在Vue脚手架中的使用(代码实现)(2022/7/24)
文章目录 1.第一步编写组件 1.1 编写一个 展示学校的组件 1.2 定义一个展示学生的信息组件 2.第二步引入组件 3.制作一个容器 4.使用Vue接管 容器 5.实际效果 6.友情提示: 7.项 ...