From 8e02153be95527d4ad3d612182e62e7d45bd86a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com> Date: Wed, 15 Feb 2023 11:38:45 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E6=9C=AC=E5=9C=B0=E5=90=AF=E5=8A=A8nacos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nacos-server-sentinel-consumer/pom.xml | 1 - .../controller/FlowLimitController.java | 46 +++++++++++++++++++ .../src/main/resources/bootstrap.yml | 13 ++++-- 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/nacos-server-sentinel-consumer/pom.xml b/nacos-server-sentinel-consumer/pom.xml index cd39d86..3a8b52f 100644 --- a/nacos-server-sentinel-consumer/pom.xml +++ b/nacos-server-sentinel-consumer/pom.xml @@ -84,7 +84,6 @@ 1.8.2 - \ No newline at end of file diff --git a/nacos-server-sentinel-consumer/src/main/java/com/kwan/springcloudalibaba/controller/FlowLimitController.java b/nacos-server-sentinel-consumer/src/main/java/com/kwan/springcloudalibaba/controller/FlowLimitController.java index 16555d2..566b234 100644 --- a/nacos-server-sentinel-consumer/src/main/java/com/kwan/springcloudalibaba/controller/FlowLimitController.java +++ b/nacos-server-sentinel-consumer/src/main/java/com/kwan/springcloudalibaba/controller/FlowLimitController.java @@ -1,8 +1,11 @@ package com.kwan.springcloudalibaba.controller; import com.alibaba.csp.sentinel.annotation.SentinelResource; +import com.alibaba.csp.sentinel.slots.block.BlockException; +import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.concurrent.TimeUnit; @@ -15,6 +18,7 @@ import java.util.concurrent.TimeUnit; * @version : 2.2.0 * @date : 2023/2/2 17:13 */ +@Slf4j @RestController @RequestMapping("/flowLimit") public class FlowLimitController { @@ -71,4 +75,46 @@ public class FlowLimitController { } return "------testE"; } + + /** + * 热点规则 + * + * @param p1 + * @param p2 + * @return + */ + @GetMapping("/testHotKey") + @SentinelResource(value = "testHotKey") + public String testHotKey(@RequestParam(value = "p1", required = false) String p1, + @RequestParam(value = "p2", required = false) String p2) { + return "------testHotKey"; + } + + + @GetMapping("/testHotKey2") + @SentinelResource(value = "testHotKey2", blockHandler = "dealHandler_testHotKey") + public String testHotKey2(@RequestParam(value = "p1", required = false) String p1, + @RequestParam(value = "p2", required = false) String p2) { + log.info("testE 热点参数"); + return "------testHotKey2"; + } + + /** + * 自定义异常 + * + * @param p1 + * @param p2 + * @param exception + * @return + */ + public String dealHandler_testHotKey(String p1, String p2, BlockException exception) { + // 默认 Blocked by Sentinel (flow limiting) + return "-----dealHandler_testHotKey"; + } + + @GetMapping("/persistent") + public String persistent() { + log.info("persistent 测试持久化"); + return "------persistent 测试持久化"; + } } \ No newline at end of file diff --git a/nacos-server-sentinel-consumer/src/main/resources/bootstrap.yml b/nacos-server-sentinel-consumer/src/main/resources/bootstrap.yml index ca00495..e2c6ba1 100644 --- a/nacos-server-sentinel-consumer/src/main/resources/bootstrap.yml +++ b/nacos-server-sentinel-consumer/src/main/resources/bootstrap.yml @@ -11,9 +11,9 @@ spring: cloud: nacos: discovery: - server-addr: http://120.79.36.53:8848 #服务注册地址 + server-addr: localhost:8848 #服务注册地址 config: - server-addr: http://120.79.36.53:8848 #配置中心地址 + server-addr: localhost:8848 #配置中心地址 file-extension: yaml #文件类型 group: DEV_GROUP #组别 namespace: 4cd9bd32-8f25-45cd-b919-df1a0df146e0 #命名空间 @@ -22,4 +22,11 @@ spring: transport: dashboard: 127.0.0.1:8181 port: 8719 #默认端口,如果被占用则从8719依次+1扫描 - + datasource: + ds1: + nacos: + server-addr: localhost:8848 + dataId: nacos-server-sentinel-consumer # 微服务名称 + groupId: DEFAULT_GROUP + data-type: json + rule-type: flow # 流控规则 \ No newline at end of file -- GitLab