Springboot整合Sentinel

【笔记于学习尚硅谷课程所作】

1、 高并发方法论&简介

  • 熔断:A服务调用B服务的某个功能,由于网络不稳定问题,或者B服务卡机,导致功能时间超长。如果这样子的次数太多。我们就可以直接将B断路了(A不再请求B接口),凡是调用B的直接返回降级数据,不必等待B的超长执行。这样 B的故障问题,就不会级联影响到A。
  • 降级:整个网站处于流量高峰期,服务器压力剧增,根据当前业务情祝及流量,对一些服务和页面进行有策略的降级(停止服务,所有的调用直接返回降级数据)。以此缓解服务器资源的的压力,以保证核心业务的正常运行,同时也保持了客户和大部分客户的得到正确的相应。
  • 限流:对打入服务的请求流量进行控制,使服务能够承担不超过自己能力的流量压力

熔断和降级异同:

  • 相同点:
    • 为了保证集群大部分服务的可用性和可靠性,防止崩溃,牺牲小我
    • 用户最终都是体验到某个功能不可用
  • 不同点:
    • 熔断是被调用方故障,触发的系统主动规则
    • 降级是基于全局考虑,停止一些正常服务,释放资源

2、 Sentinel整合

(1)导入依赖

<!--Sentinel场景启动器 - 流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

(2)下载对应版本的控制台

(3)启动控制台,在控制台的目录下

java -jar sentinel-dashboard-1.7.1.jar --server.port=9999

(4)访问localhost:9999,默认用户名密码sentinel/sentinel

(5)在需要保护的服务的application.properties添加配置

spring.cloud.sentinel.transport.dashboard=localhost:9999
spring.cloud.sentinel.transport.port=8719

(6)导入actuator

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

(7)在application.properties添加配置

management.endpoints.web.exposure.include=*

(8)自定义sentinel的流控返回规则(核心包1.6.x之前有效,1.7开始内部依赖出现改版,参考Spring Cloud Alibaba v2.2.0 升级问题整理

@Configuration
public class MySentinelConfig {

    public MySentinelConfig() {
        WebCallbackManager.setUrlBlockHandler(new UrlBlockHandler() {
            @Override
            public void blocked(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws IOException {
                R error = R.error(BizCodeEnume.TO_MANY_REQUEST.getCode(), BizCodeEnume.TO_MANY_REQUEST.getMsg());
                httpServletResponse.setCharacterEncoding("UTF-8");
                httpServletResponse.setContentType("application/json");
                httpServletResponse.getWriter().write(JSON.toJSONString(error));
            }
        });
    }
}

(9)整合网关

 <!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-alibaba-sentinel-gateway -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>

(10) 定制网关流控返回

@Configuration
public class MySentinelGatewayConfig {

    public MySentinelGatewayConfig() {
        GatewayCallbackManager.setBlockHandler(new BlockRequestHandler() {
            /**
             * 网关限流了请求,就会调用此回调
             * @param serverWebExchange
             * @param throwable
             * @return
             */
            @Override
            public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) {
                R error = R.error(BizCodeEnume.TO_MANY_REQUEST.getCode(), BizCodeEnume.TO_MANY_REQUEST.getMsg());
                String errJson = JSON.toJSONString(error);
                Mono<ServerResponse> responseMono = ServerResponse.ok().body(Mono.just(errJson), String.class);
                return responseMono;
            }
        });
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot和Sentinel是两个独立的开源项目,可以通过一些配置和依赖来实现它们的整合。下面是一个简单的步骤: 1. 在你的Spring Boot项目中,添加Sentinel的依赖。你可以在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-spring-webmvc-adapter</artifactId> <version>x.x.x</version> </dependency> ``` 2. 创建一个配置类,用于配置Sentinel的相关参数。在该类上添加`@Configuration`注解,并使用`@PostConstruct`注解来初始化Sentinel的相关配置。例如: ```java import org.springframework.context.annotation.Configuration; @Configuration public class SentinelConfig { @PostConstruct public void init() { // 初始化Sentinel的相关配置,例如规则的加载等 } } ``` 3. 在你的Controller类上使用`@SentinelResource`注解来定义资源,并配置对应的限流规则。例如: ```java import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api") public class MyController { @GetMapping("/hello") @SentinelResource(value = "hello", blockHandler = "handleBlock") public String hello() { return "Hello, Sentinel!"; } public String handleBlock(BlockException ex) { return "Blocked by Sentinel"; } } ``` 在上面的例子中,`@SentinelResource`注解用于定义一个资源,其中`value`属性指定资源名称,`blockHandler`属性指定当资源被限流时的处理方法。 4. 最后,启动你的Spring Boot应用程序,并访问定义的API进行测试。 这只是一个简单的示例,你可以根据自己的需要在Spring Boot项目中进行更多的配置和调整。希望对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值