sentinel环境搭建

简单搭建sentinel环境

sentinel 是什么?

        主要目标是流量控制

sentinel 分两个部分:

1、 服务端,是一个 spring boot 服务

2、客户端API,需要 应用接入 

首先需要启动 sentinel服务。

1. 下载 sentinel 服务端jar包

2.  像启动自己写的应用一样,使用 java -jar 来启动,具体可以参考java -server -Xms64m -Xmx256m -Dserver.port=8849 -Dcsp.sentinel.dashboard.server=localhost:8849 -Dproject.name=sentinel-dashboard -jar /work/sentinel-dashboard-1.7.1.jar 这个命令

3. 启动 就可以使用 浏览器 访问了

其次,被 sentinel 管控的 应用 需要接入:

1. 在spring boot项目中增加依赖及配置

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

        <!-- sentinel-nacos规则持久化 -->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
            <version>1.7.0</version>
        </dependency>

2. .yml文件增加配置

spring:
  cloud:
    sentinel:
      transport:
        dashboard: localhost:8080
      datasource:
        ds1:
          nacos:
            server-addr: localhost:8848 # nacos 地址(数据源地址)
            data-id: sentinel-test-config # 配置项名称
            data-type: json # 数据格式
            rule-type: flow

3. 项目中增加资源

@Service
public class TestService {

    @SentinelResource("hello,wxj")
    public String sayHello(String name){

        return "hello," + name;
    }
}

4. 对资源的使用

@RestController
@RequestMapping("/v1")
public class TestController {

    @Autowired
    TestService testService;

    @GetMapping("/hellotest")
    public String t1(HttpServletRequest request){

        return testService.sayHello("ok");

    }
}

5. 在nacos 增加对资源的控制的 配置,如下

[
    {
        "resource": "hello,wxj",
        "limitApp": "default",
        "grade": 1,
        "count": 1,
        "strategy": 0,
        "controlBehavior": 0,
        "clusterMode": false
    }
]

6. 启动项目

在sentinel控制台可以看到 nacos的配置信息,访问资源时会被有效的控制

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值