spring整合Sentinel

安装sentinel:

执行命令;

java -jar sentinel-dashboard-1.8.6.jar

注:sentinel的默认端口为8080,容易出现tomcat的冲突。

当端口冲突,可以使用该指令修改sentinel的端口

默认账号和密码都为sentinel

Springcloud整合sentinel:

导入依赖:

 <!--SpringCloud alibaba sentinel -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <!--nacos-discovery-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

application.yml文件配置:

server:
  port: 8401

spring:
  application:
    name: cloudalibaba-sentinel-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848         #Nacos服务注册中心地址
    sentinel:
      transport:
        dashboard: localhost:8858 #配置Sentinel dashboard控制台服务地址
        port: 8719 #默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口

测试类:

package com.wen.cloud.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class FlowLimitController
{

    @GetMapping("/testA")
    public String testA()
    {
        return "------testA";
    }

    @GetMapping("/testB")
    public String testB()
    {
        return "------testB";
    }
}

测试:

先访问接口  sentinel才会监测出相应的接口:

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud集成Sentinel非常简单,只需要添加SentinelSentinel Dashboard的依赖,然后在启动类上加上`@EnableCircuitBreaker`注解即可。下面我们来详细介绍一下整个过程。 1. 添加依赖 在pom.xml文件中添加如下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-sentinel-datasource-nacos</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-nacos-discovery</artifactId> </dependency> ``` 其中,`spring-cloud-starter-alibaba-sentinel`是Sentinel的依赖,`spring-cloud-alibaba-sentinel-datasource-nacos`是Sentinel使用Nacos作为数据源的依赖,`spring-cloud-alibaba-nacos-discovery`是Nacos的依赖。 2. 启用Sentinel 在启动类上添加`@EnableCircuitBreaker`注解,启用Sentinel: ```java @SpringBootApplication @EnableDiscoveryClient @EnableCircuitBreaker public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 3. 配置Sentinel 在application.yml文件中添加如下配置: ```yaml spring: application: name: service-provider cloud: sentinel: transport: dashboard: localhost:8080 datasource: ds1: nacos: server-addr: localhost:8848 dataId: ${spring.application.name}-flow-rules groupId: DEFAULT_GROUP rule-type: flow ``` 其中,`transport.dashboard`配置Sentinel Dashboard的地址,`datasource`配置Sentinel使用Nacos作为数据源,`nacos.server-addr`配置Nacos的地址,`dataId`指定流控规则的Data ID,`groupId`指定Nacos的Group ID,`rule-type`指定规则类型。 4. 配置Sentinel Dashboard 下载Sentinel Dashboard,启动Sentinel Dashboard: ```bash java -jar sentinel-dashboard-1.8.1.jar ``` 访问http://localhost:8080即可查看Sentinel Dashboard。 5. 配置流控规则 在Nacos中添加流控规则。以service-provider服务为例,添加Data ID为service-provider-flow-rules的配置项,内容为: ```json [ { "resource": "hello", "count": 3.0, "grade": 1, "limitApp": "default", "strategy": 0, "controlBehavior": 0, "clusterMode": false } ] ``` 其中,`resource`指定资源名称,`count`指定阈值,`grade`指定流控模式,`limitApp`指定流控针对的调用来源,`strategy`指定流控策略,`controlBehavior`指定流控效果,`clusterMode`指定是否为集群模式。 6. 测试 启动服务提供者和服务消费者,访问http://localhost:8081/hello,即可触发流控规则,Sentinel会阻止请求并返回限流信息。 以上就是Spring Cloud集成Sentinel的整个过程,希望对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值