使用Sentinel实现微服务容错

一、项目中整合Sentinel

1.1、在pom.xml中添加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

1.2、在application.yml中添加属性配置

# 配置actuator和Sentinel联动
management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always

 1.3、整合微服务

整合我们开发的微服务到控制台

spring:
  cloud:
    sentinel:
      transport:
        #指定sentin控制台的地址
        dashboard: localhost:8080

1.4、测试运行结果

二、搭建Sentinel控制台 

2.1、启动Sentinel控制台

打开终端窗口,输入启动命令:java -jar  sentinel-dashboard-1.6.2.jar

 启动成功:

2.2、浏览器访问控制台 

  三、流控规则

   限流的目的是通过限制并发访问数或者限制一个窗口内允许处理的请求数量来保护系统,一旦达到限制流量,则对当前请求处理采取对应的的拒绝策略。如跳转错误页面、进行排队、服务降级等。

1、快速失败:直接抛出异常
2、 Warm Up:根据codeFactor(默认3)的值,从阈值/codeFactor,经过预热时长,才到达设置的QPS阈值

Warm Up:根据codeFactor(默认3)的值,从阈值/codeFactor,经过预热时长,才到达设置的QPS阈值

3、排队等待(单位毫秒):适用于应对突发情况(突然流量剧增)的场景,匀速排队,让请求以均匀的速度通过,阈值类型必须设成QPS,否则无效

四、降级规则(熔断)

        服务熔断是指当前服务提供者无法正常为服务调用者提供服务时,比如请求超时、服务异常等,为了防止整个系统出现雪崩效应,暂时将出现故障的接口隔离出来,断绝与外部接口的联系,当触发熔断之后,后续一段时间内该服务调用者的请求都会直接失败,直到目标服务恢复正常。

五、 微服务对限流后的处理fallbackfactory

5.1新建工厂类

package com.example.cloud.contentcenter.feignclient.fallbackfactory;

import com.example.cloud.contentcenter.entity.User;
import com.example.cloud.contentcenter.feignclient.UserCenterFeignClient;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class UserCenterFeignClientFallbackFactory implements FallbackFactory<UserCenterFeignClient> {
    @Override
    public UserCenterFeignClient create(Throwable throwable) {
        return new UserCenterFeignClient(){

            @Override
            public User findUserById(String id) {
                log.warn("已被限流*************");
                User u=new User();
                u.setUsername("不好意思,访问人数太多,请稍候");
                return u;
            }
        };
    }
}

5.2、在Feign的客户端接口上添加引用

@FeignClient(name="user-center",fallbackFactory =UserCenterFeignClientFallbackFactory.class)
package com.example.cloud.contentcenter.feignclient;

import com.example.cloud.contentcenter.entity.User;
import com.example.cloud.contentcenter.feignclient.fallbackfactory.UserCenterFeignClientFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(name="user-center",fallbackFactory = UserCenterFeignClientFallbackFactory.class)
public interface UserCenterFeignClient {

    @GetMapping("/user/{id}")
    public User findUserById( @PathVariable String id);

//    @GetMapping("/users")
//    public int insertUser(User user);


}

5.3、测试效果

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值