南宁网站开发推广,中学生做的安全作业网站,网站建设时间怎么查,安徽休宁建设厅网站文章目录 前言一、sentinel服务端安装1.1 服务端下载1.2 启动sentinel服务 二、客户端使用sentinel2.1.pom增加sentinel包2.2 增加配置2.3 启动服务 三、验证3.1 给hello接口增加流控规则3.2 测试结果如下 总结 前言
前面完成了gateway项目部署并且测试#xff0c;现在部署搭… 文章目录 前言一、sentinel服务端安装1.1 服务端下载1.2 启动sentinel服务 二、客户端使用sentinel2.1.pom增加sentinel包2.2 增加配置2.3 启动服务 三、验证3.1 给hello接口增加流控规则3.2 测试结果如下 总结 前言
前面完成了gateway项目部署并且测试现在部署搭建sentinel服务并且测试。 一、sentinel服务端安装
1.1 服务端下载
下载地址 这里选择的是目前最新的sentinel版本 直接下载启动jar包使用命令安装服务
1.2 启动sentinel服务
java -Dserver.port8480 -Dcsp.sentinel.dashboard.server192.168.184.131:8480 -Dproject.namesentinel-dashboard -Dsentinel.dashboard.auth.usernamesentinel -Dsentinel.dashboard.auth.password123456 -jar sentinel-dashboard-1.8.6.jar # 指定控制台的端口为8480
-Dserver.port8480
# 指定要被哪个控制台监控这里指定的是自己监控自己
-Dcsp.sentinel.dashboard.server192.168.184.131:8480
# 指定实例名称名称会在控制台左侧以菜单显示
-Dproject.namesentinel-dashboard
# 设置登录的帐号为sentinel
-Dsentinel.dashboard.auth.usernamesentinel
# 设置登录的密码为123456
-Dsentinel.dashboard.auth.password123456
使用设置的账号密码登录如下图所示启动成功
二、客户端使用sentinel
2.1.pom增加sentinel包 dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-sentinel/artifactId/dependency完整pom如下
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.springcloudalibaba/groupIdartifactIdserver/artifactIdversion0.0.1-SNAPSHOT/versionnameserver/namedescriptionDemo project for Spring Boot/descriptionpropertiesjava.version8/java.versionspring-boot.version2.6.13/spring-boot.versionspring-cloud.version2021.0.5/spring-cloud.versionspring-cloud-alibaba.version2021.0.5.0/spring-cloud-alibaba.version/propertiesdependenciesdependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-sentinel/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!-- SpringCloud Alibaba Nacos --dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId/dependency!-- SpringCloud Alibaba Nacos Config --dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-config/artifactId/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-bootstrap/artifactId/dependency/dependenciesdependencyManagementdependencies!-- SpringCloud 微服务 --dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-dependencies/artifactIdversion${spring-cloud.version}/versiontypepom/typescopeimport/scope/dependency!-- SpringCloud Alibaba 微服务 --dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-alibaba-dependencies/artifactIdversion${spring-cloud-alibaba.version}/versiontypepom/typescopeimport/scope/dependency!-- SpringBoot 依赖配置 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-dependencies/artifactIdversion${spring-boot.version}/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagementbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdversion2.6.0/versionconfigurationexcludesexcludegroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/exclude/excludes/configuration/plugin/plugins/build/project
2.2 增加配置 sentinel:#取消懒加载eager: true#sentinel服务地址transport:dashboard: 192.168.184.131:8480完整的配置如下
# Tomcat
server:port: 8081# Spring
spring:application:# 应用名称name: serverprofiles:# 环境配置active: devmain:allow-bean-definition-overriding: truecloud:nacos:discovery:# 服务注册地址server-addr: 192.168.184.130:8848group: alibabanamespace: 7dd9fa65-9c9d-484f-94f8-d621ca05d0e5register-enabled: trueconfig:# 配置中心地址server-addr: 192.168.184.130:8848# 配置文件格式file-extension: ymlgroup: ${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 Idgroup: ${spring.cloud.nacos.discovery.group} # 默认为DEFAULT_GROUPrefresh: falsesentinel:#取消懒加载eager: true#sentinel服务地址transport:dashboard: 192.168.184.131:8480
logging:level:com.alibaba.nacos.client: info2.3 启动服务
启动服务后查看控制台发现server服务已经可以看到监控情况。 可以看到server服务已经在监控下了
三、验证
3.1 给hello接口增加流控规则
接口如下
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接口增加流控规则 资源名为请求接口路径qps为1s请求数
3.2 测试结果如下 可以看到流控规则已经生效
总结
1.sentinel启动使用比较简单后面还可以配置流控规则持久化这里就不演示了大概知道了sentinel如何使用。 2.sentinel不仅仅可以限流还可以熔断配置接口白名单黑名单。 3.使用sentinelResource注解还可以自定义异常返回更加灵活的使用sentinel配置异常返回。