From c641f54f429c291aef427ad301c452b2da4d30c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com> Date: Thu, 16 Feb 2023 09:09:04 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E7=BD=91=E5=85=B3=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ConsumerUserController.java | 20 +++ .../src/main/resources/bootstrap.yml | 7 +- nacos-server-gateway/pom.xml | 66 ++-------- .../GatewayApplication.java | 25 ---- .../constant/RespConstant.java | 6 - .../controller/FlowLimitController.java | 120 ------------------ .../controller/LocalController.java | 52 -------- .../controller/SentinelController.java | 70 ---------- .../handler/UrlBlockHandler.java | 47 ------- .../src/main/resources/bootstrap.yml | 56 +++++--- .../SentinelConsumerApplicationTests.java | 13 -- pom.xml | 24 ++-- 12 files changed, 88 insertions(+), 418 deletions(-) delete mode 100644 nacos-server-gateway/src/main/java/com/kwan/springcloudalibaba/constant/RespConstant.java delete mode 100644 nacos-server-gateway/src/main/java/com/kwan/springcloudalibaba/controller/FlowLimitController.java delete mode 100644 nacos-server-gateway/src/main/java/com/kwan/springcloudalibaba/controller/LocalController.java delete mode 100644 nacos-server-gateway/src/main/java/com/kwan/springcloudalibaba/controller/SentinelController.java delete mode 100644 nacos-server-gateway/src/main/java/com/kwan/springcloudalibaba/handler/UrlBlockHandler.java delete mode 100644 nacos-server-gateway/src/test/java/com/kwan/springcloudalibaba/SentinelConsumerApplicationTests.java diff --git a/nacos-server-consumer/src/main/java/com/kwan/springcloudalibaba/controller/ConsumerUserController.java b/nacos-server-consumer/src/main/java/com/kwan/springcloudalibaba/controller/ConsumerUserController.java index b87320a..35d6d91 100644 --- a/nacos-server-consumer/src/main/java/com/kwan/springcloudalibaba/controller/ConsumerUserController.java +++ b/nacos-server-consumer/src/main/java/com/kwan/springcloudalibaba/controller/ConsumerUserController.java @@ -120,9 +120,29 @@ public class ConsumerUserController { /** * 获取配置的变量 + *
+ * http://127.0.0.1:8085/nacos/consumer/nameInfo
*/
@GetMapping(value = "/nameInfo", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Result nameInfo() {
return Result.ok();
}
+
+
+ /**
+ * http://127.0.0.1:8085/nacos/consumer/test
+ */
+ @GetMapping(value = "/test", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
+ public Result test() {
+ return Result.ok();
+ }
+
+
+ /**
+ * 获取配置的变量
+ */
+ @GetMapping(value = "/index", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
+ public Result index() {
+ return Result.ok();
+ }
}
\ No newline at end of file
diff --git a/nacos-server-consumer/src/main/resources/bootstrap.yml b/nacos-server-consumer/src/main/resources/bootstrap.yml
index 22f7dde..6ab670c 100644
--- a/nacos-server-consumer/src/main/resources/bootstrap.yml
+++ b/nacos-server-consumer/src/main/resources/bootstrap.yml
@@ -16,8 +16,5 @@ spring:
server-addr: http://120.79.36.53:8848 #配置中心地址
file-extension: yaml #文件类型
group: DEV_GROUP #组别
- namespace: e750dcd5-657b-489b-8d15-b7b71aa3e984 #命名空间
- refresh-enabled: true #默认自动刷新
-
-
-
+ namespace: 4cd9bd32-8f25-45cd-b919-df1a0df146e0 #命名空间
+ refresh-enabled: true #默认自动刷新
\ No newline at end of file
diff --git a/nacos-server-gateway/pom.xml b/nacos-server-gateway/pom.xml
index d640725..561120d 100644
--- a/nacos-server-gateway/pom.xml
+++ b/nacos-server-gateway/pom.xml
@@ -14,74 +14,36 @@
- * http://localhost:9091/user/1
- *
- * @param id
- * @return
- */
- @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
- public Result getUserById(@PathVariable Integer id) {
- return Result.ok(feignUserService.selectOne(id));
- }
-
- /**
- * 获取配置的变量
- * http://localhost:8086/sentinel/nameInfo
- */
- @GetMapping(value = "/nameInfo", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
- public Result nameInfo() {
- return Result.ok();
- }
-
- /**
- * http://localhost:8086/sentinel/world
- *
- * @return
- */
- @GetMapping("/world")
- @SentinelResource(value = "helloWorld", blockHandler = "helloBlock")
- public String helloWorld() {
- return "Hello world";
- }
-
- /**
- * 回调地址
- *
- * @param e
- * @return
- */
- public String helloBlock(BlockException e) {
- return "你已被流控";
- }
-}
\ No newline at end of file
diff --git a/nacos-server-gateway/src/main/java/com/kwan/springcloudalibaba/handler/UrlBlockHandler.java b/nacos-server-gateway/src/main/java/com/kwan/springcloudalibaba/handler/UrlBlockHandler.java
deleted file mode 100644
index 5f44b39..0000000
--- a/nacos-server-gateway/src/main/java/com/kwan/springcloudalibaba/handler/UrlBlockHandler.java
+++ /dev/null
@@ -1,47 +0,0 @@
-package com.kwan.springcloudalibaba.handler;
-
-import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
-import com.alibaba.csp.sentinel.slots.block.BlockException;
-import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
-import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
-import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
-import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
-import com.alibaba.csp.sentinel.slots.system.SystemBlockException;
-import com.fasterxml.jackson.annotation.JsonInclude;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.kwan.springcloudalibaba.common.Result;
-import org.springframework.stereotype.Component;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-@Component
-public class UrlBlockHandler implements BlockExceptionHandler {
-
- @Override
- public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws Exception {
- String msg = null;
- if (e instanceof FlowException) {//限流异常
- msg = "接口已被限流";
- } else if (e instanceof DegradeException) {//熔断异常
- msg = "接口已被熔断,请稍后再试";
- } else if (e instanceof ParamFlowException) { //热点参数限流
- msg = "热点参数限流";
- } else if (e instanceof SystemBlockException) { //系统规则异常
- msg = "系统规则(负载不满足要求)";
- } else if (e instanceof AuthorityException) { //授权规则异常
- msg = "授权规则不通过";
- }
- httpServletResponse.setStatus(500);
- httpServletResponse.setCharacterEncoding("UTF-8");
- httpServletResponse.setContentType("application/json;charset=utf-8");
- //ObjectMapper是内置Jackson的序列化工具类,这用于将对象转为JSON字符串
- ObjectMapper mapper = new ObjectMapper();
- //某个对象属性为null时不进行序列化输出
- mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
- mapper.writeValue(httpServletResponse.getWriter(),
- Result.error(300, msg)
- );
- }
-
-}
\ No newline at end of file
diff --git a/nacos-server-gateway/src/main/resources/bootstrap.yml b/nacos-server-gateway/src/main/resources/bootstrap.yml
index 0919312..fc83031 100644
--- a/nacos-server-gateway/src/main/resources/bootstrap.yml
+++ b/nacos-server-gateway/src/main/resources/bootstrap.yml
@@ -1,34 +1,52 @@
#端口号
server:
- port: 8088
+ port: 8089
+
+management:
+ endpoint:
+ health:
+ show-details: always
+ endpoints:
+ jmx:
+ exposure:
+ include: '*'
+ web:
+ exposure:
+ include: '*'
+ health:
+ sentinel:
+ enabled: false
#spring配置
spring:
application:
- name: nacos-server-sentinel-consumer
+ name: nacos-server-gateway
profiles:
active: dev
cloud:
+ gateway:
+ enabled: true #开启网关
+ discovery:
+ locator:
+ enabled: true #启用DiscoveryClient
+ routes:
+ - id: route1
+ uri: lb://nacos-server-consumer
+ predicates:
+ - Path=/nacos/consumer/test,/nacos/consumer/index
+ - id: route2
+ uri: lb://nacos-server-producer
+ predicates:
+ - Path=/service/**
+ filter:
+ - AddRequestHeader=X-Request-Id,12345
+
nacos:
discovery:
server-addr: http://120.79.36.53:8848 #服务注册地址
- config:
- server-addr: http://120.79.36.53:8848 #配置中心地址
- file-extension: yaml #文件类型
- group: DEV_GROUP #组别
- namespace: 4cd9bd32-8f25-45cd-b919-df1a0df146e0 #命名空间
- refresh-enabled: true #默认自动刷新
+ username: nacos
+ password: nacos
sentinel:
transport:
dashboard: 127.0.0.1:8181
- port: 8719 #默认端口,如果被占用则从8719依次+1扫描
- datasource:
- ds1:
- nacos:
- server-addr: http://120.79.36.53:8848
- username: nacos
- password: nacos
- dataId: nacos-server-sentinel-consumer # 微服务名称
- groupId: DEFAULT_GROUP
- data-type: json
- rule-type: flow # 流控规则
\ No newline at end of file
+ port: 8719 #默认端口,如果被占用则从8719依次+1扫描
\ No newline at end of file
diff --git a/nacos-server-gateway/src/test/java/com/kwan/springcloudalibaba/SentinelConsumerApplicationTests.java b/nacos-server-gateway/src/test/java/com/kwan/springcloudalibaba/SentinelConsumerApplicationTests.java
deleted file mode 100644
index 9a5f4be..0000000
--- a/nacos-server-gateway/src/test/java/com/kwan/springcloudalibaba/SentinelConsumerApplicationTests.java
+++ /dev/null
@@ -1,13 +0,0 @@
-package com.kwan.springcloudalibaba;
-
-import org.junit.jupiter.api.Test;
-import org.springframework.boot.test.context.SpringBootTest;
-
-@SpringBootTest
-class SentinelConsumerApplicationTests {
-
- @Test
- void contextLoads() {
- }
-
-}
diff --git a/pom.xml b/pom.xml
index 80446cb..dfd9998 100644
--- a/pom.xml
+++ b/pom.xml
@@ -15,8 +15,8 @@