第一步引入相关依赖
| <dependency> |
| <groupId>org.springframework.cloud</groupId> |
| <artifactId>spring-cloud-starter-openfeign</artifactId> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.springframework.cloud</groupId> |
| <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> |
| </dependency> |
第二步 在yml中开启重试机制
| spring: |
| cloud: |
| loadbalancer: |
| retry: |
| enabled: true |
第三步 启动类中开启熔断的注解,然后在yaml中设置断路器超时时间等
@EnableCircuitBreaker

| # 断路器的超时时间需要大于Ribbon的超时时间,不然不会触发重试。 |
| hystrix: |
| command: |
| default: |
| execution: |
| isolation: |
| thread: |
| timeoutInMilliseconds: 6000 |
| ribbon: |
| # 请求连接超时时间 |
| ConnectTimeout: 3000 |
| # 请求处理的超时时间 |
| ReadTimeout: 3000 |
| # 对所有操作请求都进行重试 |
| OkToRetryOnAllOperations: true |
| # 对当前实例的重试次数 |
| maxAutoRetries: 3 |
| # 切换实例的重试次数 |
| # MaxAutoRetriesNextServer: 2 |
| |
关注我的公众号SpaceObj 领取idea系列激活码

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)