1.Sentinel的构成:
- 核心库-后台默认的端口是8719
- 控制台-前台默认的是8080端口
2.2.搭建Sentinel环境:
a.下载Sentinel:
- 1.sentinel官方提供了UI控制台,方便我们对系统做限流设置。可以在GitHub下载
b.下载后运行Sentinel:
- 1.提前配置好jdk:
- 2.运行:将jar包放到任意非中文目录,执行命令:
java -jar sentinel-dashboard-1.8.1.jar
,sentinel默认使用8080端口: - 3.如果要修改Sentinel的默认端口、账户、密码,可以通过下列配置:
配置项 | 默认值 | 说明 |
---|---|---|
server.port | 8080 | 服务端口 |
sentinel.dashboard.auth.username | sentinel | 默认用户名 |
sentinel.dashboard.auth.password | sentinel | 默认密码 |
- 4.例如修改端口:
java -Dserver.port=8090 -jar sentinel-dashboard-1.8.1.jar
c.访问Sentinel管理页面:
- 1.访问:访问
http://localhost:8080
页面,就可以看到sentinel的控制台了,需要输入账号和密码,默认都是:sentinel - 2.登录后,发现一片空白,什么都没有,这是因为我们还没有与微服务整合
2.3.微服务8401整合Sentinel:
a.启动Nacos-8848服务:
- 1.
startup.cmd -m standalone
- 2.访问地址
b.启动Sentinel8080
- 1.java -jar sentinel-dashboard-1.8.6.jar
c.新建微服务8401模块:
- 1.建model:建模块cloudalibaba-sentinel-service8401,将被哨兵纳入管控的8401微服务提供者
- 2.更改pom:引入sentinel依赖
xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.atguigu.cloud</groupId>
<artifactId>mscloudV5</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>cloudalibaba-sentinel-service8401</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--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>
<!-- 引入自己定义的api通用包 -->
<dependency>
<groupId>com.atguigu.cloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--SpringBoot通用依赖模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--hutool-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
<scope>provided</scope>
</dependency>
<!--test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 3.改yml:修改application.yaml文件,配置控制台,添加下面内容:
yaml
server:
port: 8401
spring:
application:
name: cloudalibaba-sentinel-service
cloud:
nacos:
discovery:
server-addr: localhost:8848 #Nacos服务注册中心地址
sentinel:
transport:
dashboard: localhost:8080 #配置Sentinel dashboard控制台服务地址
port: 8719 #默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
- 4.主启动:
java
package com.atguigu.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @auther zzyy
* @create 2023-11-27 18:17
*/
@EnableDiscoveryClient
@SpringBootApplication
public class Main8401
{
public static void main(String[] args)
{
SpringApplication.run(Main8401.class,args);
}
}
- 4.业务类:
java
package com.atguigu.cloudalibaba.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @auther zzyy
* @create 2023-05-24 15:35
*/
@RestController
public class FlowLimitController
{
@GetMapping("/testA")
public String testA()
{
return "------testA";
}
@GetMapping("/testB")
public String testB()
{
return "------testB";
}
}
- 5.启动服务并访问:
d.启动8401微服务后查看sentinel控制台
- 1.服务启动,访问order-service的任意端点:打开浏览器,访问
http://localhost:8088/order/101
,这样才能触发sentinel的监控。然后再访问sentinel的控制台,查看效果: