1.解决冀北阿里云Nacos版本兼容问题

2.解决配置bootstrap.yml兼容问题
This commit is contained in:
wr
2026-04-02 10:40:11 +08:00
parent 7b9fb1628b
commit 1aad1aa86b
42 changed files with 1753 additions and 895 deletions

View File

@@ -1,5 +1,7 @@
package com.njcn.microservice.listener;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import org.slf4j.LoggerFactory;
@@ -34,33 +36,46 @@ public class NacosLogbackConfigLoader implements SpringApplicationRunListener, A
@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("cndl408125hawen", "lanxia201");
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);
String active = environment.getProperty("spring.profiles.active");
if(!"jb".equalsIgnoreCase(active)){
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("cndl408125hawen", "lanxia201");
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,logLevel);
}
}
}
private void applyLogbackConfig(String logbackConfigContent) {
private void applyLogbackConfig(String logbackConfigContent,String logLevel) {
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);
Logger root = context.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
if("debug".equalsIgnoreCase(logLevel)){
root.setLevel(Level.DEBUG);
}
if("info".equalsIgnoreCase(logLevel)){
root.setLevel(Level.INFO);
}
if("error".equalsIgnoreCase(logLevel)){
root.setLevel(Level.ERROR);
}
} catch (Exception e) {
e.printStackTrace();
}

View File

@@ -1,6 +1,6 @@
package com.njcn.mq.template;
import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.alibaba.fastjson.JSON;
import com.njcn.middle.rocket.domain.BaseMessage;
import com.njcn.middle.rocket.template.RocketMQEnhanceTemplate;
import com.njcn.mq.constant.BusinessResource;
@@ -25,7 +25,7 @@ public class BZEventMessageTemplate extends RocketMQEnhanceTemplate {
public SendResult sendMember(Object message, String tag) {
BaseMessage baseMessage = new BaseMessage();
baseMessage.setSource(BusinessResource.WEB_RESOURCE);
baseMessage.setMessageBody(new Gson().toJson(message));
baseMessage.setMessageBody(JSON.toJSONString(message));
return send(BusinessTopic.ASK_RECALL_TOPIC,tag,baseMessage);
}
}

View File

@@ -1,6 +1,6 @@
package com.njcn.mq.template;
import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.google.gson.Gson;
import com.njcn.middle.rocket.domain.BaseMessage;
import com.njcn.middle.rocket.template.RocketMQEnhanceTemplate;
import com.njcn.mq.constant.BusinessResource;

View File

@@ -1,6 +1,6 @@
package com.njcn.mq.template;
import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.google.gson.Gson;
import com.njcn.middle.rocket.domain.BaseMessage;
import com.njcn.middle.rocket.template.RocketMQEnhanceTemplate;
import com.njcn.mq.constant.BusinessResource;

View File

@@ -1,6 +1,6 @@
package com.njcn.mq.template;
import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.google.gson.Gson;
import com.njcn.middle.rocket.domain.BaseMessage;
import com.njcn.middle.rocket.template.RocketMQEnhanceTemplate;
import com.njcn.mq.constant.BusinessResource;