Просмотр исходного кода

content 引入 spring-cloud-zookeeper-config 依赖实现动态的服务配置

reghao 11 месяцев назад
Родитель
Сommit
273417d214

+ 9 - 0
content/content-service/src/main/java/cn/reghao/tnb/content/app/vod/controller/ContentController.java

@@ -5,9 +5,11 @@ import cn.reghao.jutil.web.WebResult;
 import cn.reghao.tnb.content.api.iface.AdminSiteService;
 import cn.reghao.tnb.content.app.vod.model.vo.UserContentData;
 import cn.reghao.tnb.content.app.vod.service.ContentService;
+import cn.reghao.tnb.content.app.vod.service.LogService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.MediaType;
 import org.springframework.web.bind.annotation.*;
 
@@ -44,4 +46,11 @@ public class ContentController {
         UserContentData userContentData = contentService.getUserContentData(userId1);
         return WebResult.success(userContentData);
     }
+
+    @Autowired
+    LogService logService;
+    @GetMapping("/log")
+    public String getLogConfig() {
+        return WebResult.success(logService.getLogProperties());
+    }
 }

+ 51 - 0
content/content-service/src/main/java/cn/reghao/tnb/content/app/vod/service/LogService.java

@@ -0,0 +1,51 @@
+package cn.reghao.tnb.content.app.vod.service;
+
+import cn.reghao.tnb.common.prop.LogProperties;
+import org.springframework.cloud.context.config.annotation.RefreshScope;
+import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.event.EventListener;
+import org.springframework.core.env.Environment;
+import org.springframework.stereotype.Service;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * @author reghao
+ * @date 2025-04-18 14:45:36
+ */
+@RefreshScope
+@Service
+public class LogService {
+    private final LogProperties logProperties;
+
+    public LogService(LogProperties logProperties) {
+        this.logProperties = logProperties;
+    }
+
+    public Map<String, Object> getLogProperties() {
+        boolean enabled = logProperties.getEnabled();
+        String wsUrl = logProperties.getWsUrl();
+        String token = logProperties.getToken();
+
+        Map<String, Object> map = new HashMap<>();
+        map.put("enabled", enabled);
+        map.put("wsUrl", wsUrl);
+        map.put("token", token);
+        return map;
+    }
+
+    @EventListener
+    public void envListener(EnvironmentChangeEvent event) {
+        ApplicationContext context = (ApplicationContext) event.getSource();
+        Environment environment = context.getEnvironment();
+        Set<String> changedKeys = event.getKeys();
+        System.out.println("changed keys -> " + changedKeys);
+        for (String key : changedKeys) {
+            String value = environment.getProperty(key);
+            System.out.printf("new value %s -> %s", key, value);
+        }
+    }
+}

+ 53 - 0
content/content-service/src/main/resources/bootstrap.yml

@@ -0,0 +1,53 @@
+dubbo:
+  scan:
+    base-packages: cn.reghao.tnb.content.app.vod.rpc,cn.reghao.tnb.content.app.mall.rpc
+  protocol:
+    name: dubbo
+    port: 6105
+server:
+  port: 6005
+  tomcat:
+    max-http-form-post-size: 4MB
+spring:
+  application:
+    name: content-service
+  profiles:
+    active: @profile.active@
+  datasource:
+    driver-class-name: com.mysql.cj.jdbc.Driver
+    type: com.zaxxer.hikari.HikariDataSource
+    hikari:
+      minimum-idle: 5
+      maximum-pool-size: 10
+      auto-commit: true
+      idle-timeout: 30000
+      pool-name: EvaluationHikariCP
+      max-lifetime: 1800000
+      connection-timeout: 30000
+      connection-test-query: SELECT 1
+  cloud:
+    zookeeper:
+      connect-string: localhost:2181
+      config:
+        enabled: true
+        root: config
+        default-context: ${spring.application.name}
+        profile-separator: ':'
+        watcher:
+          enabled: true
+mybatis:
+  configuration:
+    map-underscore-to-camel-case: true
+  mapper-locations: classpath*:mapper/**.xml
+  type-aliases-package: cn.reghao.tnb.content.app.model.po
+management:
+  endpoints:
+    web:
+      base-path: /api/content/actuator
+      exposure:
+        include: "*"
+    health:
+      show-details: always
+  metrics:
+    tags:
+      application: ${spring.application.name}