springboot 整合sentinel

1. 安装sentinel

        下载地址:https://github.com/alibaba/Sentinel/releases/tag/1.7.0 ,由于我无法下载,所以使用docker安装,

yuchunfang@yuchunfangdeMacBook-Pro ~ % docker pull bladex/sentinel-dashboard:1.7.0 
yuchunfang@yuchunfangdeMacBook-Pro ~ % docker run --name sentinel -d -p 8858:8858 bladex/sentinel-dashboard:1.7.0

登录地址,默认端口是8080,由于我的已被占用,所以使用8858,http://localhost:8858

账号密码:sentinel/sentinel,能够打开访问页面,说明安装成功。

2.客户端连接

       1. 准备springboot项目,在pom中引入sentinel

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
            <version>2.2.5.RELEASE</version>
        </dependency>

        2.配置文件配置服务器地址信息

spring.cloud.sentinel.transport.dashboard=localhost:8858
spring.cloud.sentinel.transport.heartbeat-interval-ms=500
spring.cloud.sentinel.eager=true

        3.自定义异常提示信息

package org.example.config;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class SentinelExceptionHandler implements BlockExceptionHandler {


    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse response, BlockException e) throws Exception {
        response.setStatus(HttpStatus.TOO_MANY_REQUESTS.value());
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().print("sentinel 限流。。。");
    }
}

        BlockException包含很多个子类,分别对应不同的场景:

异常                       说明
FlowException             限流异常
ParamFlowException        热点参数限流的异常
DegradeException          降级异常
AuthorityException        授权规则异常
SystemBlockException      系统规则异常

        4.在sentinel中配置限流的接口

        当客户端连接好sentinel以后,首页会增加一列客户端的服务,在其中增加流控规则,其中资源名为接口地址,配置阀值,查看是否生效,可以配置流控,降级,热点等相关配置,默认情况下,发生限流、降级、授权拦截时,都会进入自定义异常。

可以看到当配置的请求地址访问频率大于设置的阀值时,流控生效,起到保护服务器的作用

Spring BootSentinel是两个独立的开源项目,可以通过一些配置和依赖来实现它们的整合。下面是一个简单的步骤: 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项目中进行更多的配置和调整。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值