From 08e52ae9c87699329696a9b9a8f6417966a9a0ff Mon Sep 17 00:00:00 2001 From: zhangsan <646228430@qq.com> Date: Mon, 26 May 2025 20:59:19 +0800 Subject: [PATCH] 5.26 sentinel --- cart-service/pom.xml | 5 ++ .../java/com/hmall/cart/CartApplication.java | 4 +- .../src/main/resources/application-local.yaml | 2 +- .../src/main/resources/application.yaml | 7 ++ .../src/main/resources/bootstrap.yaml | 5 +- .../java/com/hmall/api/client/ItemClient.java | 3 +- .../fallback/ItemClientFallbackFactory.java | 33 ++++++++ .../hmall/api/config/DefaultFeignConfig.java | 6 ++ hm-gateway/pom.xml | 11 ++- .../gateway/routers/DynamicRouteLoader.java | 77 +++++++++++++++++++ hm-gateway/src/main/resources/application.yml | 28 ------- hm-gateway/src/main/resources/bootstrap.yaml | 12 +++ item-service/pom.xml | 14 ++++ .../hmall/item/controller/ItemController.java | 3 + .../src/main/resources/application-local.yaml | 2 +- .../src/main/resources/application.yaml | 63 +++------------ .../src/main/resources/bootstrap.yaml | 15 ++++ pay-service/pom.xml | 14 ++++ .../src/main/resources/application-local.yaml | 2 +- .../src/main/resources/application.yaml | 59 ++------------ pay-service/src/main/resources/bootstrap.yaml | 15 ++++ trade-service/pom.xml | 14 ++++ .../src/main/resources/application-local.yaml | 2 +- .../src/main/resources/application.yaml | 58 ++------------ .../src/main/resources/bootstrap.yaml | 15 ++++ user-service/pom.xml | 14 ++++ .../src/main/resources/application-local.yaml | 2 +- .../src/main/resources/application.yaml | 57 ++------------ .../src/main/resources/bootstrap.yaml | 15 ++++ 29 files changed, 308 insertions(+), 249 deletions(-) create mode 100644 hm-api/src/main/java/com/hmall/api/client/fallback/ItemClientFallbackFactory.java create mode 100644 hm-gateway/src/main/java/com/hmall/gateway/routers/DynamicRouteLoader.java create mode 100644 hm-gateway/src/main/resources/bootstrap.yaml create mode 100644 item-service/src/main/resources/bootstrap.yaml create mode 100644 pay-service/src/main/resources/bootstrap.yaml create mode 100644 trade-service/src/main/resources/bootstrap.yaml create mode 100644 user-service/src/main/resources/bootstrap.yaml diff --git a/cart-service/pom.xml b/cart-service/pom.xml index 23eb9e1..0e412d0 100644 --- a/cart-service/pom.xml +++ b/cart-service/pom.xml @@ -65,6 +65,11 @@ org.springframework.cloud spring-cloud-starter-bootstrap + + + com.alibaba.cloud + spring-cloud-starter-alibaba-sentinel + ${project.artifactId} diff --git a/cart-service/src/main/java/com/hmall/cart/CartApplication.java b/cart-service/src/main/java/com/hmall/cart/CartApplication.java index 3177507..e60e5e4 100644 --- a/cart-service/src/main/java/com/hmall/cart/CartApplication.java +++ b/cart-service/src/main/java/com/hmall/cart/CartApplication.java @@ -1,12 +1,14 @@ package com.hmall.cart; +import com.hmall.api.config.DefaultFeignConfig; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; @MapperScan("com.hmall.cart.mapper") -@EnableFeignClients(basePackages= "com.hmall.api.client") +@EnableFeignClients(basePackages= "com.hmall.api.client",defaultConfiguration = DefaultFeignConfig + .class) @SpringBootApplication public class CartApplication { public static void main(String[] args) { diff --git a/cart-service/src/main/resources/application-local.yaml b/cart-service/src/main/resources/application-local.yaml index e310359..707fb24 100644 --- a/cart-service/src/main/resources/application-local.yaml +++ b/cart-service/src/main/resources/application-local.yaml @@ -1,4 +1,4 @@ hm: db: - host: 124.71.159.195 # 修改为你自己的虚拟机IP地址 + host: localhost # 修改为你自己的虚拟机IP地址 pw: 123456 # 修改为docker中的MySQL密码 \ No newline at end of file diff --git a/cart-service/src/main/resources/application.yaml b/cart-service/src/main/resources/application.yaml index 521993a..67934e9 100644 --- a/cart-service/src/main/resources/application.yaml +++ b/cart-service/src/main/resources/application.yaml @@ -1,8 +1,15 @@ server: port: 8082 + tomcat: + threads: + max: 50 + accept-count: 50 + max-connections: 100 feign: httpclient: enabled: true # 使用 Apache HttpClient(默认关闭) + sentinel: + enabled: true # 开启feign对sentinel的支持 hm: swagger: diff --git a/cart-service/src/main/resources/bootstrap.yaml b/cart-service/src/main/resources/bootstrap.yaml index a6008d1..8cddd17 100644 --- a/cart-service/src/main/resources/bootstrap.yaml +++ b/cart-service/src/main/resources/bootstrap.yaml @@ -5,10 +5,11 @@ spring: active: local cloud: nacos: - server-addr: 124.71.159.195:8848 # nacos地址 + server-addr: localhost:8848 # nacos地址 config: file-extension: yaml # 文件后缀名 shared-configs: # 共享配置 - dataId: shared_jdbc.yaml # 共享mybatis配置 - dataId: shared_log.yaml # 共享日志配置 - - dataId: shared_swagger.yaml # 共享日志配置 \ No newline at end of file + - dataId: shared_swagger.yaml # 共享日志配置 + - dataId: shared_sentinel.yaml #共享sentinel控制台 \ No newline at end of file diff --git a/hm-api/src/main/java/com/hmall/api/client/ItemClient.java b/hm-api/src/main/java/com/hmall/api/client/ItemClient.java index 04feaed..d111d11 100644 --- a/hm-api/src/main/java/com/hmall/api/client/ItemClient.java +++ b/hm-api/src/main/java/com/hmall/api/client/ItemClient.java @@ -1,5 +1,6 @@ package com.hmall.api.client; +import com.hmall.api.client.fallback.ItemClientFallbackFactory; import com.hmall.api.dto.ItemDTO; import com.hmall.api.dto.OrderDetailDTO; import org.springframework.cloud.openfeign.FeignClient; @@ -11,7 +12,7 @@ import org.springframework.web.bind.annotation.RequestParam; import java.util.Collection; import java.util.List; -@FeignClient("item-service") +@FeignClient(value = "item-service",fallbackFactory = ItemClientFallbackFactory.class) public interface ItemClient { @GetMapping("/items") diff --git a/hm-api/src/main/java/com/hmall/api/client/fallback/ItemClientFallbackFactory.java b/hm-api/src/main/java/com/hmall/api/client/fallback/ItemClientFallbackFactory.java new file mode 100644 index 0000000..e5cf301 --- /dev/null +++ b/hm-api/src/main/java/com/hmall/api/client/fallback/ItemClientFallbackFactory.java @@ -0,0 +1,33 @@ +package com.hmall.api.client.fallback; + +import com.hmall.api.client.ItemClient; +import com.hmall.api.dto.ItemDTO; +import com.hmall.api.dto.OrderDetailDTO; +import com.hmall.common.exception.BizIllegalException; +import com.hmall.common.utils.CollUtils; +import lombok.extern.slf4j.Slf4j; +import org.springframework.cloud.openfeign.FallbackFactory; + +import java.util.Collection; +import java.util.List; + +@Slf4j +public class ItemClientFallbackFactory implements FallbackFactory { + @Override + public ItemClient create(Throwable cause) { + return new ItemClient() { + @Override + public List queryItemByIds(Collection ids) { + log.error("远程调用ItemClient#queryItemByIds方法出现异常,参数:{}", ids, cause); + // 查询购物车允许失败,查询失败,返回空集合 + return CollUtils.emptyList(); + } + + @Override + public void deductStock(List items) { + // 库存扣减业务需要触发事务回滚,查询失败,抛出异常 + throw new BizIllegalException(cause); + } + }; + } +} \ No newline at end of file diff --git a/hm-api/src/main/java/com/hmall/api/config/DefaultFeignConfig.java b/hm-api/src/main/java/com/hmall/api/config/DefaultFeignConfig.java index d575a6b..829a2f7 100644 --- a/hm-api/src/main/java/com/hmall/api/config/DefaultFeignConfig.java +++ b/hm-api/src/main/java/com/hmall/api/config/DefaultFeignConfig.java @@ -1,5 +1,6 @@ package com.hmall.api.config; +import com.hmall.api.client.fallback.ItemClientFallbackFactory; import com.hmall.common.utils.UserContext; import feign.RequestInterceptor; import feign.RequestTemplate; @@ -22,4 +23,9 @@ public class DefaultFeignConfig { } }; } + + @Bean + public ItemClientFallbackFactory itemClientFallbackFactory(){ + return new ItemClientFallbackFactory(); + } } diff --git a/hm-gateway/pom.xml b/hm-gateway/pom.xml index f4b3232..76d1de9 100644 --- a/hm-gateway/pom.xml +++ b/hm-gateway/pom.xml @@ -42,7 +42,16 @@ spring-boot-configuration-processor true - + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + + org.springframework.cloud + spring-cloud-starter-bootstrap + ${project.artifactId} diff --git a/hm-gateway/src/main/java/com/hmall/gateway/routers/DynamicRouteLoader.java b/hm-gateway/src/main/java/com/hmall/gateway/routers/DynamicRouteLoader.java new file mode 100644 index 0000000..c920f02 --- /dev/null +++ b/hm-gateway/src/main/java/com/hmall/gateway/routers/DynamicRouteLoader.java @@ -0,0 +1,77 @@ +package com.hmall.gateway.routers; + +import cn.hutool.json.JSONUtil; +import com.alibaba.cloud.nacos.NacosConfigManager; +import com.alibaba.nacos.api.config.listener.Listener; +import com.alibaba.nacos.api.exception.NacosException; +import com.hmall.common.utils.CollUtils; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.cloud.gateway.route.RouteDefinition; +import org.springframework.cloud.gateway.route.RouteDefinitionWriter; +import org.springframework.stereotype.Component; +import reactor.core.publisher.Mono; + +import javax.annotation.PostConstruct; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.concurrent.Executor; + +@Slf4j +@Component +@RequiredArgsConstructor +public class DynamicRouteLoader { + + private final RouteDefinitionWriter writer; + private final NacosConfigManager nacosConfigManager; + + // 路由配置文件的id和分组 + private final String dataId = "gateway-routes.json"; + private final String group = "DEFAULT_GROUP"; + // 保存更新过的路由id + private final Set routeIds = new HashSet<>(); + + @PostConstruct + public void initRouteConfigListener() throws NacosException { + // 1.注册监听器并首次拉取配置 + String configInfo = nacosConfigManager.getConfigService() + .getConfigAndSignListener(dataId, group, 5000, new Listener() { + @Override + public Executor getExecutor() { + return null; + } + + @Override + public void receiveConfigInfo(String configInfo) { + updateConfigInfo(configInfo); + } + }); + // 2.首次启动时,更新一次配置 + updateConfigInfo(configInfo); + } + + private void updateConfigInfo(String configInfo) { + log.debug("监听到路由配置变更,{}", configInfo); + // 1.反序列化 + List routeDefinitions = JSONUtil.toList(configInfo, RouteDefinition.class); + // 2.更新前先清空旧路由 + // 2.1.清除旧路由 + for (String routeId : routeIds) { + writer.delete(Mono.just(routeId)).subscribe(); + } + routeIds.clear(); + // 2.2.判断是否有新的路由要更新 + if (CollUtils.isEmpty(routeDefinitions)) { + // 无新路由配置,直接结束 + return; + } + // 3.更新路由 + routeDefinitions.forEach(routeDefinition -> { + // 3.1.更新路由 + writer.save(Mono.just(routeDefinition)).subscribe(); + // 3.2.记录路由id,方便将来删除 + routeIds.add(routeDefinition.getId()); + }); + } +} diff --git a/hm-gateway/src/main/resources/application.yml b/hm-gateway/src/main/resources/application.yml index fbc0d80..133f2b4 100644 --- a/hm-gateway/src/main/resources/application.yml +++ b/hm-gateway/src/main/resources/application.yml @@ -1,33 +1,5 @@ server: port: 8080 -spring: - application: - name: gateway - cloud: - nacos: - server-addr: 124.71.159.195:8848 - gateway: - routes: - - id: item # 路由规则id,自定义,唯一 - uri: lb://item-service # 路由的目标服务,lb代表负载均衡,会从注册中心拉取服务列表 - predicates: # 路由断言,判断当前请求是否符合当前规则,符合则路由到目标服务 - - Path=/items/**,/search/** # 这里是以请求路径作为判断规则 - - id: cart - uri: lb://cart-service - predicates: - - Path=/carts/** - - id: user - uri: lb://user-service - predicates: - - Path=/users/**,/addresses/** - - id: trade - uri: lb://trade-service - predicates: - - Path=/orders/** - - id: pay - uri: lb://pay-service - predicates: - - Path=/pay-orders/** hm: jwt: location: classpath:hmall.jks diff --git a/hm-gateway/src/main/resources/bootstrap.yaml b/hm-gateway/src/main/resources/bootstrap.yaml new file mode 100644 index 0000000..85b113d --- /dev/null +++ b/hm-gateway/src/main/resources/bootstrap.yaml @@ -0,0 +1,12 @@ +spring: + application: + name: gateway # 服务名称 + profiles: + active: local + cloud: + nacos: + server-addr: localhost:8848 # nacos地址 + config: + file-extension: yaml # 文件后缀名 + shared-configs: # 共享配置 + - dataId: shared_log.yaml # 共享日志配置 \ No newline at end of file diff --git a/item-service/pom.xml b/item-service/pom.xml index fc9045e..8e56a93 100644 --- a/item-service/pom.xml +++ b/item-service/pom.xml @@ -54,6 +54,20 @@ com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + + org.springframework.cloud + spring-cloud-starter-bootstrap + + + com.alibaba.cloud + spring-cloud-starter-alibaba-sentinel + ${project.artifactId} diff --git a/item-service/src/main/java/com/hmall/item/controller/ItemController.java b/item-service/src/main/java/com/hmall/item/controller/ItemController.java index 08949b4..01d02cc 100644 --- a/item-service/src/main/java/com/hmall/item/controller/ItemController.java +++ b/item-service/src/main/java/com/hmall/item/controller/ItemController.java @@ -1,4 +1,5 @@ package com.hmall.item.controller; +import cn.hutool.core.thread.ThreadUtil; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.hmall.api.dto.ItemDTO; import com.hmall.api.dto.OrderDetailDTO; @@ -34,6 +35,8 @@ public class ItemController { @ApiOperation("根据id批量查询商品") @GetMapping public List queryItemByIds(@RequestParam("ids") List ids){ + //模拟线程延迟 + ThreadUtil.sleep(500); return itemService.queryItemByIds(ids); } diff --git a/item-service/src/main/resources/application-local.yaml b/item-service/src/main/resources/application-local.yaml index e310359..707fb24 100644 --- a/item-service/src/main/resources/application-local.yaml +++ b/item-service/src/main/resources/application-local.yaml @@ -1,4 +1,4 @@ hm: db: - host: 124.71.159.195 # 修改为你自己的虚拟机IP地址 + host: localhost # 修改为你自己的虚拟机IP地址 pw: 123456 # 修改为docker中的MySQL密码 \ No newline at end of file diff --git a/item-service/src/main/resources/application.yaml b/item-service/src/main/resources/application.yaml index 1addd78..1d3d033 100644 --- a/item-service/src/main/resources/application.yaml +++ b/item-service/src/main/resources/application.yaml @@ -1,57 +1,12 @@ server: port: 8081 -spring: - application: - name: item-service - cloud: - nacos: - server-addr: 124.71.159.195:8848 # nacos地址 +feign: + httpclient: + enabled: true # 使用 Apache HttpClient(默认关闭) - profiles: - active: local - datasource: - url: jdbc:mysql://${hm.db.host}:3307/hm-item?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai - driver-class-name: com.mysql.cj.jdbc.Driver - username: root - password: ${hm.db.pw} - hikari: - # 比你数据库空闲断连时间再小一点,这里示例用 20 分钟 - max-lifetime: 1200000 # 20 分钟,单位 ms - idle-timeout: 600000 # 10 分钟,长时间不用就回收 - connection-timeout: 30000 # 30 秒 等待可用连接 - minimum-idle: 5 # 保持最少 5 个空闲 - maximum-pool-size: 10 # 池中最大连接数,根据并发量调整 - validation-timeout: 5000 # 验证一个连接可用时的超时时间 - # (可选)强制每次从池里拿连接前跑一下简单检查: - connection-test-query: SELECT 1 -mybatis-plus: - configuration: - default-enum-type-handler: com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandler - global-config: - db-config: - update-strategy: not_null - id-type: auto -logging: - level: - com.hmall: debug - pattern: - dateformat: HH:mm:ss:SSS - file: - path: "logs/${spring.application.name}" -knife4j: - enable: true - openapi: - title: 黑马商城商品管理接口文档 - description: "黑马商城商品管理接口文档" - email: zhangsan@itcast.cn - concat: 宇哥 - url: https://www.itcast.cn - version: v1.0.0 - group: - default: - group-name: default - api-rule: package - api-rule-resources: - - com.hmall.item.controller - -# keytool -genkeypair -alias hmall -keyalg RSA -keypass hmall123 -keystore hmall.jks -storepass hmall123 \ No newline at end of file +hm: + swagger: + title: 商品管理接口文档 + package: com.hmall.item.controller + db: + database: hm-item \ No newline at end of file diff --git a/item-service/src/main/resources/bootstrap.yaml b/item-service/src/main/resources/bootstrap.yaml new file mode 100644 index 0000000..f066f4e --- /dev/null +++ b/item-service/src/main/resources/bootstrap.yaml @@ -0,0 +1,15 @@ +spring: + application: + name: item-service # 服务名称 + profiles: + active: local + cloud: + nacos: + server-addr: localhost:8848 # nacos地址 + config: + file-extension: yaml # 文件后缀名 + shared-configs: # 共享配置 + - dataId: shared_jdbc.yaml # 共享mybatis配置 + - dataId: shared_log.yaml # 共享日志配置 + - dataId: shared_swagger.yaml # 共享日志配置 + - dataId: shared_sentinel.yaml #共享sentinel控制台 \ No newline at end of file diff --git a/pay-service/pom.xml b/pay-service/pom.xml index 18d43ed..57769d2 100644 --- a/pay-service/pom.xml +++ b/pay-service/pom.xml @@ -54,6 +54,20 @@ com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + + org.springframework.cloud + spring-cloud-starter-bootstrap + + + com.alibaba.cloud + spring-cloud-starter-alibaba-sentinel + ${project.artifactId} diff --git a/pay-service/src/main/resources/application-local.yaml b/pay-service/src/main/resources/application-local.yaml index e310359..707fb24 100644 --- a/pay-service/src/main/resources/application-local.yaml +++ b/pay-service/src/main/resources/application-local.yaml @@ -1,4 +1,4 @@ hm: db: - host: 124.71.159.195 # 修改为你自己的虚拟机IP地址 + host: localhost # 修改为你自己的虚拟机IP地址 pw: 123456 # 修改为docker中的MySQL密码 \ No newline at end of file diff --git a/pay-service/src/main/resources/application.yaml b/pay-service/src/main/resources/application.yaml index 2e43187..6fae47d 100644 --- a/pay-service/src/main/resources/application.yaml +++ b/pay-service/src/main/resources/application.yaml @@ -1,61 +1,14 @@ server: port: 8086 -spring: - application: - name: pay-service - cloud: - nacos: - server-addr: 124.71.159.195:8848 # nacos地址 - profiles: - active: local - datasource: - url: jdbc:mysql://${hm.db.host}:3307/hm-pay?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai - driver-class-name: com.mysql.cj.jdbc.Driver - username: root - password: ${hm.db.pw} - hikari: - # 比你数据库空闲断连时间再小一点,这里示例用 20 分钟 - max-lifetime: 1200000 # 20 分钟,单位 ms - idle-timeout: 600000 # 10 分钟,长时间不用就回收 - connection-timeout: 30000 # 30 秒 等待可用连接 - minimum-idle: 5 # 保持最少 5 个空闲 - maximum-pool-size: 10 # 池中最大连接数,根据并发量调整 - validation-timeout: 5000 # 验证一个连接可用时的超时时间 - # (可选)强制每次从池里拿连接前跑一下简单检查: - connection-test-query: SELECT 1 -mybatis-plus: - configuration: - default-enum-type-handler: com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandler - global-config: - db-config: - update-strategy: not_null - id-type: auto -logging: - level: - com.hmall: debug - pattern: - dateformat: HH:mm:ss:SSS - file: - path: "logs/${spring.application.name}" -knife4j: - enable: true - openapi: - title: 黑马商城支付服务接口文档 - description: "黑马商城支付服务接口文档" - email: zhangsan@itcast.cn - concat: 宇哥 - url: https://www.itcast.cn - version: v1.0.0 - group: - default: - group-name: default - api-rule: package - api-rule-resources: - - com.hmall.pay.controller feign: httpclient: enabled: true # 使用 Apache HttpClient(默认关闭) - +hm: + swagger: + title: 支付管理接口文档 + package: com.hmall.pay.controller + db: + database: hm-pay # keytool -genkeypair -alias hmall -keyalg RSA -keypass hmall123 -keystore hmall.jks -storepass hmall123 \ No newline at end of file diff --git a/pay-service/src/main/resources/bootstrap.yaml b/pay-service/src/main/resources/bootstrap.yaml new file mode 100644 index 0000000..9636ce3 --- /dev/null +++ b/pay-service/src/main/resources/bootstrap.yaml @@ -0,0 +1,15 @@ +spring: + application: + name: pay-service # 服务名称 + profiles: + active: local + cloud: + nacos: + server-addr: localhost:8848 # nacos地址 + config: + file-extension: yaml # 文件后缀名 + shared-configs: # 共享配置 + - dataId: shared_jdbc.yaml # 共享mybatis配置 + - dataId: shared_log.yaml # 共享日志配置 + - dataId: shared_swagger.yaml # 共享日志配置 + - dataId: shared_sentinel.yaml #共享sentinel控制台 \ No newline at end of file diff --git a/trade-service/pom.xml b/trade-service/pom.xml index 87258cc..400ef45 100644 --- a/trade-service/pom.xml +++ b/trade-service/pom.xml @@ -54,6 +54,20 @@ com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + + org.springframework.cloud + spring-cloud-starter-bootstrap + + + com.alibaba.cloud + spring-cloud-starter-alibaba-sentinel + ${project.artifactId} diff --git a/trade-service/src/main/resources/application-local.yaml b/trade-service/src/main/resources/application-local.yaml index e310359..707fb24 100644 --- a/trade-service/src/main/resources/application-local.yaml +++ b/trade-service/src/main/resources/application-local.yaml @@ -1,4 +1,4 @@ hm: db: - host: 124.71.159.195 # 修改为你自己的虚拟机IP地址 + host: localhost # 修改为你自己的虚拟机IP地址 pw: 123456 # 修改为docker中的MySQL密码 \ No newline at end of file diff --git a/trade-service/src/main/resources/application.yaml b/trade-service/src/main/resources/application.yaml index 172e6f6..67eefde 100644 --- a/trade-service/src/main/resources/application.yaml +++ b/trade-service/src/main/resources/application.yaml @@ -1,61 +1,15 @@ server: port: 8085 -spring: - application: - name: trade-service - cloud: - nacos: - server-addr: 124.71.159.195:8848 # nacos地址 - profiles: - active: local - datasource: - url: jdbc:mysql://${hm.db.host}:3307/hm-trade?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai - driver-class-name: com.mysql.cj.jdbc.Driver - username: root - password: ${hm.db.pw} - hikari: - # 比你数据库空闲断连时间再小一点,这里示例用 20 分钟 - max-lifetime: 1200000 # 20 分钟,单位 ms - idle-timeout: 600000 # 10 分钟,长时间不用就回收 - connection-timeout: 30000 # 30 秒 等待可用连接 - minimum-idle: 5 # 保持最少 5 个空闲 - maximum-pool-size: 10 # 池中最大连接数,根据并发量调整 - validation-timeout: 5000 # 验证一个连接可用时的超时时间 - # (可选)强制每次从池里拿连接前跑一下简单检查: - connection-test-query: SELECT 1 -mybatis-plus: - configuration: - default-enum-type-handler: com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandler - global-config: - db-config: - update-strategy: not_null - id-type: auto -logging: - level: - com.hmall: debug - pattern: - dateformat: HH:mm:ss:SSS - file: - path: "logs/${spring.application.name}" -knife4j: - enable: true - openapi: - title: 黑马商城交易服务接口文档 - description: "黑马商城交易服务接口文档" - email: zhangsan@itcast.cn - concat: 宇哥 - url: https://www.itcast.cn - version: v1.0.0 - group: - default: - group-name: default - api-rule: package - api-rule-resources: - - com.hmall.trade.controller feign: httpclient: enabled: true # 使用 Apache HttpClient(默认关闭) +hm: + swagger: + title: 交易管理接口文档 + package: com.hmall.trade.controller + db: + database: hm-trade # keytool -genkeypair -alias hmall -keyalg RSA -keypass hmall123 -keystore hmall.jks -storepass hmall123 \ No newline at end of file diff --git a/trade-service/src/main/resources/bootstrap.yaml b/trade-service/src/main/resources/bootstrap.yaml new file mode 100644 index 0000000..50a81df --- /dev/null +++ b/trade-service/src/main/resources/bootstrap.yaml @@ -0,0 +1,15 @@ +spring: + application: + name: trade-service # 服务名称 + profiles: + active: local + cloud: + nacos: + server-addr: localhost:8848 # nacos地址 + config: + file-extension: yaml # 文件后缀名 + shared-configs: # 共享配置 + - dataId: shared_jdbc.yaml # 共享mybatis配置 + - dataId: shared_log.yaml # 共享日志配置 + - dataId: shared_swagger.yaml # 共享日志配置 + - dataId: shared_sentinel.yaml #共享sentinel控制台 \ No newline at end of file diff --git a/user-service/pom.xml b/user-service/pom.xml index c7c5cb8..10d0c23 100644 --- a/user-service/pom.xml +++ b/user-service/pom.xml @@ -54,6 +54,20 @@ com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + + org.springframework.cloud + spring-cloud-starter-bootstrap + + + com.alibaba.cloud + spring-cloud-starter-alibaba-sentinel + ${project.artifactId} diff --git a/user-service/src/main/resources/application-local.yaml b/user-service/src/main/resources/application-local.yaml index e310359..707fb24 100644 --- a/user-service/src/main/resources/application-local.yaml +++ b/user-service/src/main/resources/application-local.yaml @@ -1,4 +1,4 @@ hm: db: - host: 124.71.159.195 # 修改为你自己的虚拟机IP地址 + host: localhost # 修改为你自己的虚拟机IP地址 pw: 123456 # 修改为docker中的MySQL密码 \ No newline at end of file diff --git a/user-service/src/main/resources/application.yaml b/user-service/src/main/resources/application.yaml index fbbcf3b..8c92405 100644 --- a/user-service/src/main/resources/application.yaml +++ b/user-service/src/main/resources/application.yaml @@ -1,63 +1,16 @@ server: port: 8084 -spring: - application: - name: user-service - cloud: - nacos: - server-addr: 124.71.159.195:8848 # nacos地址 - profiles: - active: local - datasource: - url: jdbc:mysql://${hm.db.host}:3307/hm-user?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai - driver-class-name: com.mysql.cj.jdbc.Driver - username: root - password: ${hm.db.pw} - hikari: - # 比你数据库空闲断连时间再小一点,这里示例用 20 分钟 - max-lifetime: 1200000 # 20 分钟,单位 ms - idle-timeout: 600000 # 10 分钟,长时间不用就回收 - connection-timeout: 30000 # 30 秒 等待可用连接 - minimum-idle: 5 # 保持最少 5 个空闲 - maximum-pool-size: 10 # 池中最大连接数,根据并发量调整 - validation-timeout: 5000 # 验证一个连接可用时的超时时间 - # (可选)强制每次从池里拿连接前跑一下简单检查: - connection-test-query: SELECT 1 -mybatis-plus: - configuration: - default-enum-type-handler: com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandler - global-config: - db-config: - update-strategy: not_null - id-type: auto -logging: - level: - com.hmall: debug - pattern: - dateformat: HH:mm:ss:SSS - file: - path: "logs/${spring.application.name}" -knife4j: - enable: true - openapi: - title: 黑马商城用户服务接口文档 - description: "黑马商城用户服务接口文档" - email: zhangsan@itcast.cn - concat: 宇哥 - url: https://www.itcast.cn - version: v1.0.0 - group: - default: - group-name: default - api-rule: package - api-rule-resources: - - com.hmall.user.controller feign: httpclient: enabled: true # 使用 Apache HttpClient(默认关闭) hm: + swagger: + title: 用户管理接口文档 + package: com.hmall.user.controller + db: + database: hm-user jwt: location: classpath:hmall.jks alias: hmall diff --git a/user-service/src/main/resources/bootstrap.yaml b/user-service/src/main/resources/bootstrap.yaml new file mode 100644 index 0000000..1e2f1f1 --- /dev/null +++ b/user-service/src/main/resources/bootstrap.yaml @@ -0,0 +1,15 @@ +spring: + application: + name: user-service # 服务名称 + profiles: + active: local + cloud: + nacos: + server-addr: localhost:8848 # nacos地址 + config: + file-extension: yaml # 文件后缀名 + shared-configs: # 共享配置 + - dataId: shared_jdbc.yaml # 共享mybatis配置 + - dataId: shared_log.yaml # 共享日志配置 + - dataId: shared_swagger.yaml # 共享日志配置 + - dataId: shared_sentinel.yaml #共享sentinel控制台 \ No newline at end of file