六:Spring Cloud 之服务降级与限流熔断-Ribbon+Hystrix

本文介绍了如何在Spring Cloud中使用Hystrix和Ribbon实现服务降级和熔断。通过创建Eureka Server、服务提供者和消费者,详细阐述了配置和代码实现步骤,包括Hystrix的回退策略。通过停止服务提供者的实例,验证了Hystrix在服务异常时能有效防止雪崩并提供回调功能。
摘要由CSDN通过智能技术生成

1. 简介

Netflix has created a library called Hystrix that implements the circuit breaker pattern. In a microservice architecture it is common to have multiple layers of service calls.
A service failure in the lower level of services can cause cascading failure all the way up to the user. When calls to a particular service reach a certain threshold (20 failures in 5 seconds is the default in Hystrix), the circuit opens and the call is not made. In cases of error and an open circuit a fallback can be provided by the developer.
Having an open circuit stops cascading failures and allows overwhelmed or failing services time to heal. The fallback can be another Hystrix protected call, static data or a sane empty value. Fallbacks may be chained so the first fallback makes some other business call which in turn falls back to static data.

  • Hystrix是netflix创建的一个用于服务降级、限流熔断的组件
  • 微服务架构中一个服务调用链任何一个环节都有可能出现异常
  • 若对异常没有合理的处理方式,则可能导致与此相关的功能不可用,甚至有可能引发雪崩现象
  • Hystrix可以很好的解决服务不可用问题,可采用直接返回错误状态码、静态提示数据、以及转调统一处理方法等方式对调用方及时做出相应

2. 代码实现

2.1涉及的模块

  • eureka-server-singleton:服务注册中心,端口8761
  • eureka-service: 服务提供者,通过profile指定不同端口模拟一组微服务,端口8762、8763
  • eureka-service-ribbon-hystrix:通过Ribbon调用服务提供者提供的服务,并提供限流熔断功能

2.2 源代码

2.2.1 Github地址

https://github.com/andyChenHuaYing/spring-cloud-demo

2.2.2 切换

通过tag切换git tag -d v1.0,若想修改,可根据此tag创建新的分支。

Spring Cloud 之服务发现与调用-Ribbon#2.3 eureka-server-singleton 没有任何区别

2.3 eureka-service

Spring Cloud 之服务发现与调用-Ribbon#2.4 eureka-service 没有任何区别

2.4 eureka-service-ribbon-hystrix

2.4.1整体实现步骤

  1. pom.xml中引入spring-cloud-starter-netflix-eureka-serverspring-cloud-starter-netflix-ribbonspring-cloud-starter-netflix-hystrix依赖
  2. application.yml中指定配置项:端口、application name、eureka-server地址
  3. SpringBoot 启动类添加注解@EnableHystrix开启使用Hystrix功能
  4. 使用@Bean @LoadBalanced向Spring容器注入org.springframework.web.client.RestTemplate 实例
  5. 在使用之处使用@Autowired public RestTemplate restTemplate; 获取实例,调用服务提供方提供的方法
  6. 在需要使用Hystrix服务降级、容错功能的方法上使用@HystrixCommand(fallbackMethod = "hiError")指定回调方法

2.4.2 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud-finchley-demo</artifactId>
        <groupId>org.oscar.scd</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-service-ribbon-hystrix</artifactId>

    <
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值