项目如何整合sentinel

1、添加依赖

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

2、下载sentinel dashboard

首先,查看spring-cloud-starter-alibaba-sentinel依赖添加的sentinel-core的版本号

可以看到sentinel-core的版本号是1.7.1,所以 sentinel dashboard的版本号也要是1.7.1,所以本案例下载 1.7.1的dashboard

下载地址:

https://github.com/alibaba/Sentinel/releases

下载下来的jar包是: sentinel-dashboard-1.7.1.jar

使用java -jar sentinel-dashboard-1.7.1.jar 启动dashboard

java -jar sentinel-dashboard-1.7.1.jar

3、添加配置

添加spring.cloud.sentinel.transport.dashboard 和management.endpoints.web.exposure.include配置,如下:

spring:
  application:
    name: say
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
        namespace: b62b1132-d8c6-4740-b745-75bcfb4f66a5
        cluster-name: GD
    sentinel:
      transport:
        dashboard: localhost:8080
logging:
  config: classpath:log4j2.xml
server:
  port: 8091
feign:
  client:
    config:
      helloworld:
        loggerLevel: full
management:
  endpoints:
    web:
      exposure:
        include: '*'

4、添加FilterContextConfig 配置,如果不添加 链路流控模式会不生效

package com.codex.terry.configuration;

import com.alibaba.csp.sentinel.adapter.servlet.CommonFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FilterContextConfig {
	/**
     * @NOTE 在spring-cloud-alibaba v2.1.1.RELEASE及前,sentinel1.7.0及后,关闭URL PATH聚合需要通过该方式,spring-cloud-alibaba v2.1.1.RELEASE后,可以通过配置关闭:spring.cloud.sentinel.web-context-unify=false
     * 手动注入Sentinel的过滤器,关闭Sentinel注入CommonFilter实例,修改配置文件中的 spring.cloud.sentinel.filter.enabled=false
     * 入口资源聚合问题:https://github.com/alibaba/Sentinel/issues/1024 或 https://github.com/alibaba/Sentinel/issues/1213
     * 入口资源聚合问题解决:https://github.com/alibaba/Sentinel/pull/1111
     */
    @Bean
    public FilterRegistrationBean sentinelFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new CommonFilter());
        registration.addUrlPatterns("/*");
        // 入口资源关闭聚合
        registration.addInitParameter(CommonFilter.WEB_CONTEXT_UNIFY, "false");
        registration.setName("sentinelFilter");
        registration.setOrder(1);
        return registration;
    }
}

5、打开 sentinel dashboard进行流控、降级等配置

http://localhost:8080/

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sentinel是阿里巴巴开源的一款轻量级的流量控制和熔断框架,可以保护服务在高并发、复杂的网络环境下稳定运行。下面介绍如何将Sentinel整合到Spring Boot项目中。 1. 添加Sentinel依赖 在pom.xml中添加Sentinel的依赖: ``` <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> <version>2.2.1.RELEASE</version> </dependency> ``` 2. 配置Sentinel 在application.yml中添加Sentinel的配置: ``` spring: cloud: sentinel: transport: dashboard: localhost:8080 port: 8719 log-dir: ${user.home}/logs/csp/sentinel heartbeat-interval-ms: 20000 enable: true ``` 其中,dashboard是Sentinel控制台的地址,port是Sentinel客户端的端口号,log-dir是Sentinel日志存储路径,heartbeat-interval-ms是心跳间隔时间,enable表示是否启用Sentinel。 3. 配置Sentinel注解支持 在Spring Boot的启动类上添加@EnableSentinel注解,开启Sentinel的注解支持。 ``` @SpringBootApplication @EnableSentinel public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 4. 使用Sentinel 在需要进行流量控制和熔断的方法上添加@SentinelResource注解,指定资源名称和对应的限流规则和熔断规则。 ``` @Service public class UserService { // 指定资源名称,限流阈值为每秒最多处理5个请求 @SentinelResource(value = "getUser", blockHandler = "handleBlock", fallback = "handleFallback", blockHandlerClass = UserServiceBlockHandler.class, fallbackClass = UserServiceFallback.class) public User getUser(Long id) { // 查询用户信息 } } ``` 其中,blockHandler指定限流处理方法,fallback指定熔断处理方法,blockHandlerClass和fallbackClass分别指定限流处理和熔断处理类。需要注意的是,限流和熔断处理方法的参数和返回值类型需要与原方法保持一致。 以上就是将Sentinel整合到Spring Boot项目中的步骤,希望能帮助到你。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值