diff --git a/pqs-common/common-microservice/src/main/java/com/njcn/microservice/listener/NacosLogbackConfigLoader.java b/pqs-common/common-microservice/src/main/java/com/njcn/microservice/listener/NacosLogbackConfigLoader.java new file mode 100644 index 000000000..159e0590b --- /dev/null +++ b/pqs-common/common-microservice/src/main/java/com/njcn/microservice/listener/NacosLogbackConfigLoader.java @@ -0,0 +1,76 @@ +package com.njcn.microservice.listener; + +import ch.qos.logback.classic.LoggerContext; +import ch.qos.logback.classic.joran.JoranConfigurator; +import org.slf4j.LoggerFactory; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.SpringApplicationRunListener; +import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; +import org.springframework.context.ApplicationListener; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestTemplate; + +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; + +public class NacosLogbackConfigLoader implements SpringApplicationRunListener, ApplicationListener { + + private final SpringApplication application; + private final String[] args; + + public NacosLogbackConfigLoader(SpringApplication application, String[] args) { + this.application = application; + this.args = args; + } + + @Override + public void starting() { + // Empty implementation + } + + @Override + public void environmentPrepared(ConfigurableEnvironment environment) { + String serverAddr = environment.getProperty("spring.cloud.nacos.config.server-addr"); + String namespace = environment.getProperty("spring.cloud.nacos.config.namespace"); + String projectName = environment.getProperty("microservice.ename"); + String logLevel = environment.getProperty("logging.level.root"); + String url = String.format("http://%s/nacos/v1/cs/configs?tenant=%s&group=DEFAULT_GROUP&dataId=logback.xml", serverAddr, namespace); + HttpHeaders headers = new HttpHeaders(); + headers.set("njcnDnzlcn870299", "PqsadminDNZL001"); + HttpEntity entity = new HttpEntity<>(headers); + RestTemplate restTemplate = new RestTemplate(); + ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); + if (response.getStatusCode().is2xxSuccessful()) { + String logbackConfigContent = response.getBody(); + //替换项目名称 + logbackConfigContent = logbackConfigContent.replace("projectDefaultName",projectName); + logbackConfigContent = logbackConfigContent.replace("logDefaultLevel",logLevel); + // 应用获取到的Logback配置 + applyLogbackConfig(logbackConfigContent); + } + } + + private void applyLogbackConfig(String logbackConfigContent) { + LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); + JoranConfigurator configurator = new JoranConfigurator(); + configurator.setContext(context); + context.reset(); + try (ByteArrayInputStream inputStream = new ByteArrayInputStream(logbackConfigContent.getBytes(StandardCharsets.UTF_8))) { + configurator.doConfigure(inputStream); + } catch (Exception e) { + e.printStackTrace(); + } + } + @Override + public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { + // Empty implementation + } + + + + +} diff --git a/pqs-common/common-microservice/src/main/resources/META-INF/spring.factories b/pqs-common/common-microservice/src/main/resources/META-INF/spring.factories new file mode 100644 index 000000000..2f9625497 --- /dev/null +++ b/pqs-common/common-microservice/src/main/resources/META-INF/spring.factories @@ -0,0 +1 @@ +org.springframework.boot.SpringApplicationRunListener=com.njcn.microservice.listener.NacosLogbackConfigLoader