前言
前面完成了gateway項(xiàng)目部署并且測(cè)試,現(xiàn)在部署搭建sentinel服務(wù)并且測(cè)試。
一、sentinel服務(wù)端安裝
1.1 服務(wù)端下載
下載地址
這里選擇的是目前最新的sentinel版本
直接下載啟動(dòng)jar包,使用命令安裝服務(wù)
1.2 啟動(dòng)sentinel服務(wù)
java -Dserver.port=8480 -Dcsp.sentinel.dashboard.server=192.168.184.131:8480 -Dproject.name=sentinel-dashboard -Dsentinel.dashboard.auth.username=sentinel -Dsentinel.dashboard.auth.password=123456 -jar sentinel-dashboard-1.8.6.jar &
# 指定控制臺(tái)的端口為8480
-Dserver.port=8480
# 指定要被哪個(gè)控制臺(tái)監(jiān)控(這里指定的是自己監(jiān)控自己)
-Dcsp.sentinel.dashboard.server=192.168.184.131:8480
# 指定實(shí)例名稱(名稱會(huì)在控制臺(tái)左側(cè)以菜單顯示)
-Dproject.name=sentinel-dashboard
# 設(shè)置登錄的帳號(hào)為:sentinel
-Dsentinel.dashboard.auth.username=sentinel
# 設(shè)置登錄的密碼為:123456
-Dsentinel.dashboard.auth.password=123456
使用設(shè)置的賬號(hào)密碼登錄如下圖所示啟動(dòng)成功
二、客戶端使用sentinel
2.1.pom增加sentinel包
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
完整pom如下
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.springcloudalibaba</groupId>
<artifactId>server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>8</java.version>
<spring-boot.version>2.6.13</spring-boot.version>
<spring-cloud.version>2021.0.5</spring-cloud.version>
<spring-cloud-alibaba.version>2021.0.5.0</spring-cloud-alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringCloud Alibaba Nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- SpringCloud Alibaba Nacos Config -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!-- SpringCloud 微服務(wù) -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- SpringCloud Alibaba 微服務(wù) -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- SpringBoot 依賴配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.6.0</version>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.2 增加配置
sentinel:
#取消懶加載
eager: true
#sentinel服務(wù)地址
transport:
dashboard: 192.168.184.131:8480
完整的配置如下
# Tomcat
server:
port: 8081
# Spring
spring:
application:
# 應(yīng)用名稱
name: server
profiles:
# 環(huán)境配置
active: dev
main:
allow-bean-definition-overriding: true
cloud:
nacos:
discovery:
# 服務(wù)注冊(cè)地址
server-addr: 192.168.184.130:8848
group: alibaba
namespace: 7dd9fa65-9c9d-484f-94f8-d621ca05d0e5
register-enabled: true
config:
# 配置中心地址
server-addr: 192.168.184.130:8848
# 配置文件格式
file-extension: yml
group: ${spring.cloud.nacos.discovery.group}
namespace: ${spring.cloud.nacos.discovery.namespace}
shared-configs[0]:
data-id: application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} # 配置文件名-Data Id
group: ${spring.cloud.nacos.discovery.group} # 默認(rèn)為DEFAULT_GROUP
refresh: false
sentinel:
#取消懶加載
eager: true
#sentinel服務(wù)地址
transport:
dashboard: 192.168.184.131:8480
logging:
level:
com.alibaba.nacos.client: info
2.3 啟動(dòng)服務(wù)
啟動(dòng)服務(wù)后,查看控制臺(tái),發(fā)現(xiàn)server服務(wù)已經(jīng)可以看到監(jiān)控情況。
可以看到server服務(wù)已經(jīng)在監(jiān)控下了
三、驗(yàn)證
3.1 給hello接口增加流控規(guī)則
接口如下
package com.springcloudalibaba.gateway.control;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Control {
@RequestMapping(value = "/hello")
public String index() {
System.out.println("testname: ");
return "testName";
}
}
給hello接口增加流控規(guī)則
資源名為請(qǐng)求接口路徑,qps為1s請(qǐng)求數(shù)
3.2 測(cè)試結(jié)果如下
可以看到流控規(guī)則已經(jīng)生效文章來源:http://www.zghlxwxcb.cn/news/detail-725990.html
總結(jié)
1.sentinel啟動(dòng)使用,比較簡(jiǎn)單,后面還可以配置流控規(guī)則持久化,這里就不演示了,大概知道了sentinel如何使用。
2.sentinel不僅僅可以限流,還可以熔斷,配置接口白名單,黑名單。
3.使用@sentinelResource注解還可以自定義異常返回,更加靈活的使用sentinel配置異常返回。文章來源地址http://www.zghlxwxcb.cn/news/detail-725990.html
到了這里,關(guān)于4.springcloudalibaba sentinel v1.8.6版本服務(wù)搭建的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!