SpringCloud之Gateway整合Sentinel服务降级和限流

1.下载Sentinel.jar可以图形界面配置限流和降级规则

地址:可能需要翻墙
下载jar文件

2.引入maven依赖

xml 复制代码
   <!--    spring cloud gateway整合sentinel的依赖-->
   <dependency>
       <groupId>com.alibaba.cloud</groupId>
       <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
       <version>2.2.2.RELEASE</version>
   </dependency>

   <!--    sentinel的依赖-->
   <dependency>
       <groupId>com.alibaba.cloud</groupId>
       <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
       <version>2.2.2.RELEASE</version>
   </dependency>
   <dependency>
       <groupId>com.alibaba.csp</groupId>
       <artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
       <version>1.8.0</version>
   </dependency>

3.写个自动注入Resource的过滤器类(可以不写注解直接使用)

java 复制代码
@Configuration
public class GatewayConfiguration {
 
    private final List<ViewResolver> viewResolvers;
    private final ServerCodecConfigurer serverCodecConfigurer;
 
    public GatewayConfiguration(ObjectProvider<List<ViewResolver>> viewResolversProvider,
                                ServerCodecConfigurer serverCodecConfigurer) {
        this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);
        this.serverCodecConfigurer = serverCodecConfigurer;
    }
 
    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() {
        // Register the block exception handler for Spring Cloud Gateway.
        return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);
    }
 
    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public GlobalFilter sentinelGatewayFilter() {
        return new SentinelGatewayFilter();
    }
}

4.写配置文件 application.properties

xml 复制代码
# 服务端口
server.port=80
# 服务名
spring.application.name=service-gateway
#服务熔断
spring.cloud.sentinel.transport.dashboard=localhost:18080
# nacos服务地址
spring.cloud.nacos.discovery.server-addr=192.168.56.1:8848
spring.main.allow-bean-definition-overriding=true
spring.profiles.active=dev

#使用服务发现路由
spring.cloud.gateway.discovery.locator.enabled=true

#设置路由id
spring.cloud.gateway.routes[0].id=service-cmn
#设置路由的uri
spring.cloud.gateway.routes[0].uri=lb://service-cmn
#设置路由断言,代理servicerId为auth-service的/auth/路径
spring.cloud.gateway.routes[0].predicates= Path=/*/cmn/**

5.cmd命令行启动jar文件访问localhost:18080页面,自己设置QPS

java -jar -server.port=18080 sentinel-dashboard.jar

--------不在微服务中使用,在普通springboot也可以使用--------

1.maven依赖

xml 复制代码
  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

2.service中写

java 复制代码
@Service

public class UserService { 
	//不可以用在类上
    @SentinelResource(value = "sayHello",fallback = "sayHellofail")
    public String sayHello(){
        return "Hello,World";
    }

    public  String sayHellofail(){ //限流的方法
        return "I'am sorry";
    }

}

3.controller

java 复制代码
@RestController
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/hello")
    public String hello(){
      return userService.sayHello();
    }
}

3.sentinel控制台查看

//快速访问

相关推荐
wclass-zhengge4 小时前
SpringCloud篇(配置中心 - Nacos)
java·spring·spring cloud
yaosheng_VALVE6 小时前
稀硫酸介质中 V 型球阀的材质选择与选型要点-耀圣
运维·spring cloud·自动化·intellij-idea·材质·1024程序员节
二十雨辰9 小时前
[Java]微服务治理
java·spring cloud
AskHarries9 小时前
Spring Cloud Gateway快速入门Demo
java·后端·spring cloud
wclass-zhengge9 小时前
SpringCloud篇(注册中心 - Eurea)
后端·spring·spring cloud
草明9 小时前
HTTP 客户端怎么向 Spring Cloud Sleuth 传输跟踪 ID
spring cloud·sleuth
.生产的驴12 小时前
SpringBootCloud 服务注册中心Nacos对服务进行管理
java·spring boot·spring·spring cloud·tomcat·rabbitmq·java-rabbitmq
customer0812 小时前
【开源免费】基于SpringBoot+Vue.JS课程答疑系统(JAVA毕业设计)
java·jvm·vue.js·spring boot·spring cloud·kafka·开源
cui_win12 小时前
Redis高可用-Sentinel(哨兵)
redis·bootstrap·sentinel
Wx-bishekaifayuan14 小时前
springboot市社保局社保信息管理与分析系统-计算机设计毕业源码03479
java·css·spring boot·spring·spring cloud·servlet·guava