nacos开启鉴权功能新增部分代码
This commit is contained in:
@@ -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<ApplicationEnvironmentPreparedEvent> {
|
||||||
|
|
||||||
|
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<String> entity = new HttpEntity<>(headers);
|
||||||
|
RestTemplate restTemplate = new RestTemplate();
|
||||||
|
ResponseEntity<String> 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
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
org.springframework.boot.SpringApplicationRunListener=com.njcn.microservice.listener.NacosLogbackConfigLoader
|
||||||
Reference in New Issue
Block a user