From f21945a6b7edd6e710af486e27659e71a7e4686b Mon Sep 17 00:00:00 2001 From: xc-yjs Date: Thu, 19 Sep 2024 11:31:14 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=20=E6=9B=B4=E6=96=B0=E4=B8=9A?= =?UTF-8?q?=E5=8A=A1=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nla-common/pom.xml | 15 ++++ .../exception/SentinelBlockHandler.java | 30 ++++++++ ...{application.yml => application-local.yml} | 0 .../src/main/resources/bootstrap.yml | 32 +++++++++ nla-gateway/pom.xml | 43 +++++++++++ .../cn/nla/gateway/GatewayApplication.java | 13 ++++ .../src/main/resources/application-local.yml | 55 ++++++++++++++ nla-gateway/src/main/resources/bootstrap.yml | 23 ++++++ nla-gateway/src/main/resources/logback.xml | 71 +++++++++++++++++++ ...{application.yml => application-local.yml} | 0 .../src/main/resources/bootstrap.yml | 23 ++++++ ...{application.yml => application-local.yml} | 0 .../src/main/resources/bootstrap.yml | 23 ++++++ ...{application.yml => application-local.yml} | 0 .../src/main/resources/bootstrap.yml | 23 ++++++ pom.xml | 1 + 16 files changed, 352 insertions(+) create mode 100644 nla-common/src/main/java/cn/nla/common/exception/SentinelBlockHandler.java rename nla-coupon-service/src/main/resources/{application.yml => application-local.yml} (100%) create mode 100644 nla-coupon-service/src/main/resources/bootstrap.yml create mode 100644 nla-gateway/pom.xml create mode 100644 nla-gateway/src/main/java/cn/nla/gateway/GatewayApplication.java create mode 100644 nla-gateway/src/main/resources/application-local.yml create mode 100644 nla-gateway/src/main/resources/bootstrap.yml create mode 100644 nla-gateway/src/main/resources/logback.xml rename nla-order-service/src/main/resources/{application.yml => application-local.yml} (100%) create mode 100644 nla-order-service/src/main/resources/bootstrap.yml rename nla-product-service/src/main/resources/{application.yml => application-local.yml} (100%) create mode 100644 nla-product-service/src/main/resources/bootstrap.yml rename nla-user-service/src/main/resources/{application.yml => application-local.yml} (100%) create mode 100644 nla-user-service/src/main/resources/bootstrap.yml diff --git a/nla-common/pom.xml b/nla-common/pom.xml index 1f16d4d..15cdd8c 100644 --- a/nla-common/pom.xml +++ b/nla-common/pom.xml @@ -99,11 +99,26 @@ com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + org.springframework.cloud spring-cloud-starter-openfeign + + + com.alibaba.cloud + spring-cloud-starter-alibaba-sentinel + + + + com.alibaba.csp + sentinel-datasource-nacos + diff --git a/nla-common/src/main/java/cn/nla/common/exception/SentinelBlockHandler.java b/nla-common/src/main/java/cn/nla/common/exception/SentinelBlockHandler.java new file mode 100644 index 0000000..7fb23f9 --- /dev/null +++ b/nla-common/src/main/java/cn/nla/common/exception/SentinelBlockHandler.java @@ -0,0 +1,30 @@ +package cn.nla.common.exception; + +import cn.nla.common.enums.BizCodeEnum; +import cn.nla.common.util.CommonUtil; +import cn.nla.common.util.JsonData; +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 org.springframework.stereotype.Component; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@Component +public class SentinelBlockHandler implements BlockExceptionHandler { + @Override + public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws Exception { + JsonData jsonData = null; + if(e instanceof FlowException){ + jsonData = JsonData.buildResult(BizCodeEnum.CONTROL_FLOW); + }else if(e instanceof DegradeException){ + jsonData = JsonData.buildResult(BizCodeEnum.CONTROL_DEGRADE); + } else if(e instanceof AuthorityException){ + jsonData = JsonData.buildResult(BizCodeEnum.CONTROL_AUTH); + } + httpServletResponse.setStatus(200); + CommonUtil.sendJsonMessage(httpServletResponse,jsonData); + } +} diff --git a/nla-coupon-service/src/main/resources/application.yml b/nla-coupon-service/src/main/resources/application-local.yml similarity index 100% rename from nla-coupon-service/src/main/resources/application.yml rename to nla-coupon-service/src/main/resources/application-local.yml diff --git a/nla-coupon-service/src/main/resources/bootstrap.yml b/nla-coupon-service/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..358c713 --- /dev/null +++ b/nla-coupon-service/src/main/resources/bootstrap.yml @@ -0,0 +1,32 @@ +spring: + application: + name: nla-coupon-service + cloud: + nacos: + discovery: + server-addr: 117.72.43.105:8848 + config: + server-addr: 117.72.43.105:8848 + file-extension: yml + sentinel: + transport: + dashboard: 192.168.30.130:8080 + port: 9999 + #流控规则持久化到nacos配置中心 + datasource: + ds1: + nacos: + server-addr: 117.72.43.105:8848 + data-id: ${spring.application.name}.json + group-id: DEFAULT_GROUP + data-type: json + rule-type: flow + profiles: + active: dev + +#设置⽇志级别,ERROR/WARN/INFO/DEBUG,默认是INFO以上才显示 +logging: + level: + root: INFO + # nacos日志问题,一致打印 + com.alibaba.nacos.client.config.impl: WARN diff --git a/nla-gateway/pom.xml b/nla-gateway/pom.xml new file mode 100644 index 0000000..5e0d86b --- /dev/null +++ b/nla-gateway/pom.xml @@ -0,0 +1,43 @@ + + + + nla-shop + cn.nla + 1.0-SNAPSHOT + + 4.0.0 + nla-gateway + 网关模块 + + 11 + 11 + + + + org.springframework.cloud + spring-cloud-starter-gateway + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-sentinel + + + + com.alibaba.csp + sentinel-datasource-nacos + + + diff --git a/nla-gateway/src/main/java/cn/nla/gateway/GatewayApplication.java b/nla-gateway/src/main/java/cn/nla/gateway/GatewayApplication.java new file mode 100644 index 0000000..b2f2947 --- /dev/null +++ b/nla-gateway/src/main/java/cn/nla/gateway/GatewayApplication.java @@ -0,0 +1,13 @@ +package cn.nla.gateway; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; + +@SpringBootApplication +@EnableDiscoveryClient +public class GatewayApplication { + public static void main(String[] args) { + SpringApplication.run(GatewayApplication.class, args); + } +} diff --git a/nla-gateway/src/main/resources/application-local.yml b/nla-gateway/src/main/resources/application-local.yml new file mode 100644 index 0000000..5232ea9 --- /dev/null +++ b/nla-gateway/src/main/resources/application-local.yml @@ -0,0 +1,55 @@ +server: + port: 8089 +spring: + application: + name: nla-gateway + #注册中心地址 + cloud: + nacos: + discovery: + server-addr: 117.72.43.105:8848 + gateway: + routes: + - id: product-service + uri: lb://nla-product-service + order: 1 + predicates: + - Path=/product/** + filters: + - StripPrefix=1 + - id: user-service + uri: lb://nla-user-service + order: 1 + predicates: + - Path=/user/** + filters: + - StripPrefix=1 + - id: coupon-service + uri: lb://nla-coupon-service + order: 1 + predicates: + - Path=/coupon/** + filters: + - StripPrefix=1 + - id: order-service + uri: lb://nla-order-service + order: 1 + predicates: + - Path=/order/** + filters: + - StripPrefix=1 + #开启网关拉取nacos的服务 + discovery: + locator: + enabled: true +#设置⽇志级别,ERROR/WARN/INFO/DEBUG,默认是INFO以上才显示 +logging: + level: + root: INFO + # nacos日志问题,一致打印 + com.alibaba.nacos.client.config.impl: WARN + + + + + diff --git a/nla-gateway/src/main/resources/bootstrap.yml b/nla-gateway/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..076ed97 --- /dev/null +++ b/nla-gateway/src/main/resources/bootstrap.yml @@ -0,0 +1,23 @@ +spring: + application: + name: nla-gateway + cloud: + nacos: + discovery: + server-addr: 117.72.43.105:8848 + config: + server-addr: 117.72.43.105:8848 + file-extension: yml + sentinel: + transport: + dashboard: 192.168.30.130:8080 + port: 9999 + profiles: + active: dev + +#设置⽇志级别,ERROR/WARN/INFO/DEBUG,默认是INFO以上才显示 +logging: + level: + root: INFO + # nacos日志问题,一致打印 + com.alibaba.nacos.client.config.impl: WARN diff --git a/nla-gateway/src/main/resources/logback.xml b/nla-gateway/src/main/resources/logback.xml new file mode 100644 index 0000000..2734df7 --- /dev/null +++ b/nla-gateway/src/main/resources/logback.xml @@ -0,0 +1,71 @@ + + + + + + + + + + ${log.pattern} + + + + + ${log.path}/info.log + + + + ${log.path}/info.%d{yyyy-MM-dd}.log + + 60 + + + ${log.pattern} + + + + INFO + + ACCEPT + + DENY + + + + ${log.path}/error.log + + + + ${log.path}/error.%d{yyyy-MM-dd}.log + + 60 + + + ${log.pattern} + + + + ERROR + + ACCEPT + + DENY + + + + + + + + + + + + + + + + + + diff --git a/nla-order-service/src/main/resources/application.yml b/nla-order-service/src/main/resources/application-local.yml similarity index 100% rename from nla-order-service/src/main/resources/application.yml rename to nla-order-service/src/main/resources/application-local.yml diff --git a/nla-order-service/src/main/resources/bootstrap.yml b/nla-order-service/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..8717578 --- /dev/null +++ b/nla-order-service/src/main/resources/bootstrap.yml @@ -0,0 +1,23 @@ +spring: + application: + name: nla-order-service + cloud: + nacos: + discovery: + server-addr: 117.72.43.105:8848 + config: + server-addr: 117.72.43.105:8848 + file-extension: yml + sentinel: + transport: + dashboard: 192.168.30.130:8080 + port: 9999 + profiles: + active: dev + +#设置⽇志级别,ERROR/WARN/INFO/DEBUG,默认是INFO以上才显示 +logging: + level: + root: INFO + # nacos日志问题,一致打印 + com.alibaba.nacos.client.config.impl: WARN diff --git a/nla-product-service/src/main/resources/application.yml b/nla-product-service/src/main/resources/application-local.yml similarity index 100% rename from nla-product-service/src/main/resources/application.yml rename to nla-product-service/src/main/resources/application-local.yml diff --git a/nla-product-service/src/main/resources/bootstrap.yml b/nla-product-service/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..8ca6aa7 --- /dev/null +++ b/nla-product-service/src/main/resources/bootstrap.yml @@ -0,0 +1,23 @@ +spring: + application: + name: nla-product-service + cloud: + nacos: + discovery: + server-addr: 117.72.43.105:8848 + config: + server-addr: 117.72.43.105:8848 + file-extension: yml + sentinel: + transport: + dashboard: 192.168.30.130:8080 + port: 9999 + profiles: + active: dev + +#设置⽇志级别,ERROR/WARN/INFO/DEBUG,默认是INFO以上才显示 +logging: + level: + root: INFO + # nacos日志问题,一致打印 + com.alibaba.nacos.client.config.impl: WARN diff --git a/nla-user-service/src/main/resources/application.yml b/nla-user-service/src/main/resources/application-local.yml similarity index 100% rename from nla-user-service/src/main/resources/application.yml rename to nla-user-service/src/main/resources/application-local.yml diff --git a/nla-user-service/src/main/resources/bootstrap.yml b/nla-user-service/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..01cc883 --- /dev/null +++ b/nla-user-service/src/main/resources/bootstrap.yml @@ -0,0 +1,23 @@ +spring: + application: + name: nla-user-service + cloud: + nacos: + discovery: + server-addr: 117.72.43.105:8848 + config: + server-addr: 117.72.43.105:8848 + file-extension: yml + sentinel: + transport: + dashboard: 192.168.30.130:8080 + port: 9999 + profiles: + active: dev + +#设置⽇志级别,ERROR/WARN/INFO/DEBUG,默认是INFO以上才显示 +logging: + level: + root: INFO + # nacos日志问题,一致打印 + com.alibaba.nacos.client.config.impl: WARN diff --git a/pom.xml b/pom.xml index b0194db..7a144fd 100644 --- a/pom.xml +++ b/pom.xml @@ -13,6 +13,7 @@ nla-coupon-service nla-product-service nla-order-service + nla-gateway pom 拉新营销平台