php如何做熔断降级,spring cloud 如何实现服务熔断服务降级

Why

在微服务架构中,由于调用关系的复杂性,如果调用链路中的某个资源不稳定,最终会导致请求发生堆积。可能导致服务间延迟增加,备份队列,线程和其他系统资源紧张,导致整个系统发生更多的级联故障。

为了维护服务的稳定,我们需要在调用链路中某个资源出现不稳定状态时(例如调用超时或异常比例升高),对这个资源的调用进行限制,让请求快速失败,避免影响到其它的资源而导致级联错误。当资源被降级后,在接下来的降级时间窗口之内,对该资源的调用都自动熔断(默认行为是抛出 DegradeException)。

How

服务熔断

一般发生于下游服务,下游服务发生故障时,将服务熔断不可用

电路中的保险丝,超过负荷,直接熔断,断电,不可用,保护整体安全

服务降级

将某一服务降级暂时不可用,为了服务整体的稳定,牺牲部分不重要的功能,保证核心功能的进行

妈妈让小明去买酱油,给了10块钱,小明到超市看到自己喜欢的玩具2块钱,拿上玩具和酱油去柜台结账发现一共需要12元,为了完成妈妈的任务,选择不买玩具。等下次在买玩具

所以从上述分析来看,两者其实从有些角度看是有一定的类似性的:

目的很一致:都是从可用性可靠性着想,为防止系统的整体缓慢甚至崩溃,采用的技术手段;

最终表现类似:对于两者来说,最终让用户体验到的是某些功能暂时不可达或不可用;

自治性要求很高:熔断模式一般都是服务基于策略的自动触发,降级虽说可人工干预,但在微服务架构下,完全靠人显然不可能,开关预置、配置中心都是必要手段;

而两者的区别也是明显的:

触发原因不太一样,服务熔断一般是某个服务(下游服务)故障引起,而服务降级一般是从整体负荷考虑;

What

由于我司使用nacos作为服务注册和发现,所以我们使用sentinel进行熔断降级。

首先导入依赖

// Gradle

implementation('com.alibaba.cloud:spring-cloud-starter-alibaba-sentinel') {

exclude module: 'guava'

}

// Maven

com.alibaba.cloud

spring-cloud-starter-alibaba-sentinel

配置文件中打开sentinel

feign:

sentinel:

enabled: true

使用语言 kotlin

阅读@FeignClient源码,我们发现fallback是由spring创建

/**

* Fallback class for the specified Feign client interface. The fallback class must

* implement the interface annotated by this annotation and be a valid spring bean.

* @return fallback class for the specified Feign client interface

*/

Class> fallback() default void.class;

/**

* Define a fallback factory for the specified Feign client interface. The fallback

* factory must produce instances of fallback classes that implement the interface

* annotated by {@link FeignClient}. The fallback factory must be a valid spring bean.

*

* @see feign.hystrix.FallbackFactory for details.

* @return fallback factory for the specified Feign client interface

*/

Class> fallbackFactory() default void.class;

1.fallbackFactory

package com.karl.cloud;

import org.springframework.cloud.openfeign.FeignClient;

import org.springframework.web.bind.annotation.GetMapping;

import com.karl.DTO.UserInfoDTO;

@FeignClient(value = "karl-service-user", fallbackFactory = UserFallbackFactory::class)

interface UserFeign {

@GetMapping("/karl/user")

fun getUser(): UserInfoDTO

}

@Component

class UserFallbackFactory: FallbackFactory {

val logger = LoggerFactory.getLogger(UserFallbackFactory::class.java)!!

override fun create(cause: Throwable): DataFeign {

logger.error(cause.message, cause)

return UserFallback()

}

}

class UserFallback: UserFeign {

override fun getUser(): UserInfoDTO{

return UserInfoDTO()

}

}

2.fallback

package com.karl.cloud;

import org.springframework.cloud.openfeign.FeignClient;

import org.springframework.web.bind.annotation.GetMapping;

import com.karl.DTO.UserInfoDTO;

@FeignClient(value = "karl-service-user", fallback = UserFallback::class)

interface UserFeign {

@GetMapping("/karl/user")

fun getUser(): UserInfoDTO

}

@Component

class UserFallback: UserFeign {

override fun getUser(): UserInfoDTO{

return UserInfoDTO()

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
熔断降级是一种在分布式系统中保护服务稳定性和可用性的机制。当某个服务出现故障或响应时间过长时,熔断机制会暂时停止对该服务的调用,并返回一个预先设定的默认值或错误信息,以避免故障的扩散。Spring Cloud中的熔断降级组件Hystrix提供了实现熔断降级的功能。 要使用Hystrix,首先需要在项目的pom.xml文件中添加Hystrix的依赖: ```xml <!-- Hystrix依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> ``` 接下来,可以在需要进行熔断降级的方法上添加`@HystrixCommand`注解,该注解指定了当方法调用失败或超时时的降级处理方法。例如: ```java @Service public class UserService { @HystrixCommand(fallbackMethod = "getDefaultUser") public User getUserById(Long id) { // 调用其他服务获取用户信息 // ... } public User getDefaultUser(Long id) { // 降级处理逻辑,返回默认用户信息 // ... } } ``` 在上述示例中,`getUserById`方法使用了`@HystrixCommand`注解,并指定了降级处理方法`getDefaultUser`。当调用`getUserById`方法失败或超时时,将会调用`getDefaultUser`方法返回默认的用户信息。 除了使用注解方式,还可以通过编程方式实现熔断降级。可以使用Hystrix提供的`HystrixCommand`和`HystrixObservableCommand`类来封装需要进行熔断降级的逻辑。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值