sentinel官网
下载sentinel地址:https://github.com/alibaba/Sentinel/releases
下载 sentinel-dashboard-1.7.2.jar
在这里插入图片描述
通过java -jar启动sentinel ,默认sentinel占用的端口是8080 --server.port=8888是将端口设置为8888,如果有改端口需求则更改对应端口即可

java  -jar .\sentinel-dashboard-1.7.2.jar --server.port=8888

用户和密码都是sentinel
在这里插入图片描述
加入依赖管理

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

加入依赖

<!--   spring-cloud-starter-alibaba-nacos-discovery  nacos服务注册中心   -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

<!-- spring-cloud-starter-alibaba-sentinel sentinel客户端-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

<!-- sentinel-datasource-nacos sentinel持久化数据 -->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
</dependency>

注意自己修改端口等对应的信息,在这里sentinel使用来自定义的8888端口

server:
  port: 8400
spring:
  application:
    name: sentinel-client  #应用名
  cloud:
    nacos:
      discovery: #服务注册中心地址
        server-addr: 127.0.0.1:8848
      config:  #nacos的配置中心地址,如果不需要用到则暂时注释掉
        server-addr: 127.0.0.1:8848
        file-extension: yml
    sentinel:
      transport:
        dashboard: 127.0.0.1:8888
        port: 8719

启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class SentinelClient8400 {
    public static void main(String[] args) {
        SpringApplication.run(SentinelClient8400.class, args);
    }
}

需要流控的Controller类,示例代码

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

@RestController
public class SentinelDemoController {

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

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

启动nacos,启动sentinel,然后再启动上面这个启动类

需要注意的是sentinel是懒加载的机制,也意味着需要访问一下上面Controller中的 /test001 才会在sentinel后台看到服务
浏览器访问一下 http://localhost:8400/test001
然后刷新一下sentinel后台 http://localhost:8080/#/dashboard/home
在这里插入图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐