diff --git a/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java b/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java index c7ddfae2b20..b43c4a95920 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java @@ -72,19 +72,20 @@ public class NacosConfigService implements ConfigService { private final ConfigFilterChainManager configFilterChainManager; public NacosConfigService(Properties properties) throws NacosException { - NacosClientProperties clientProperties = NacosClientProperties.PROTOTYPE.derive(properties); - + this(NacosClientProperties.PROTOTYPE.derive(properties)); + } + + public NacosConfigService(NacosClientProperties clientProperties) throws NacosException { ValidatorUtils.checkInitParam(clientProperties); - + initNamespace(clientProperties); this.configFilterChainManager = new ConfigFilterChainManager(clientProperties); ServerListManager serverListManager = new ServerListManager(clientProperties); serverListManager.start(); - + this.worker = new ClientWorker(this.configFilterChainManager, serverListManager, clientProperties); // will be deleted in 2.0 later versions agent = new ServerHttpAgent(serverListManager); - } private void initNamespace(NacosClientProperties properties) { diff --git a/client/src/main/java/com/alibaba/nacos/client/logging/log4j2/Log4J2NacosLogging.java b/client/src/main/java/com/alibaba/nacos/client/logging/log4j2/Log4J2NacosLogging.java index c1f2eaf0131..acb42dc890b 100644 --- a/client/src/main/java/com/alibaba/nacos/client/logging/log4j2/Log4J2NacosLogging.java +++ b/client/src/main/java/com/alibaba/nacos/client/logging/log4j2/Log4J2NacosLogging.java @@ -46,6 +46,8 @@ public class Log4J2NacosLogging extends AbstractNacosLogging { private static final String NACOS_LOGGER_PREFIX = "com.alibaba.nacos"; + private static final String NACOS_LOG4J2_PLUGIN_PACKAGE = "com.alibaba.nacos.client.logging.log4j2"; + private final String location = getLocation(NACOS_LOG4J2_LOCATION); @Override @@ -59,6 +61,7 @@ public void loadConfiguration() { // load and start nacos configuration Configuration configuration = loadConfiguration(loggerContext, location); + configuration.getPluginPackages().add(NACOS_LOG4J2_PLUGIN_PACKAGE); configuration.start(); // append loggers and appenders to contextConfiguration diff --git a/client/src/main/java/com/alibaba/nacos/client/logging/log4j2/NacosClientPropertiesLookup.java b/client/src/main/java/com/alibaba/nacos/client/logging/log4j2/NacosClientPropertiesLookup.java new file mode 100644 index 00000000000..47ce9f02601 --- /dev/null +++ b/client/src/main/java/com/alibaba/nacos/client/logging/log4j2/NacosClientPropertiesLookup.java @@ -0,0 +1,38 @@ +/* + * Copyright 1999-2022 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.nacos.client.logging.log4j2; + +import com.alibaba.nacos.client.env.NacosClientProperties; +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.config.plugins.Plugin; +import org.apache.logging.log4j.core.lookup.AbstractLookup; +import org.apache.logging.log4j.core.lookup.StrLookup; + +/** + * support log4j2 read properties from NacosClientProperties. + * for example: + * + * @author onewe + */ +@Plugin(name = "nacosClientProperty", category = StrLookup.CATEGORY) +public class NacosClientPropertiesLookup extends AbstractLookup { + + @Override + public String lookup(LogEvent event, String key) { + return NacosClientProperties.PROTOTYPE.getProperty(key); + } +} diff --git a/client/src/main/java/com/alibaba/nacos/client/logging/logback/NacosClientPropertyAction.java b/client/src/main/java/com/alibaba/nacos/client/logging/logback/NacosClientPropertyAction.java new file mode 100644 index 00000000000..71bb7c9464b --- /dev/null +++ b/client/src/main/java/com/alibaba/nacos/client/logging/logback/NacosClientPropertyAction.java @@ -0,0 +1,59 @@ +/* + * Copyright 1999-2022 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.nacos.client.logging.logback; + +import ch.qos.logback.core.joran.action.Action; +import ch.qos.logback.core.joran.action.ActionUtil; +import ch.qos.logback.core.joran.spi.ActionException; +import ch.qos.logback.core.joran.spi.InterpretationContext; +import ch.qos.logback.core.util.OptionHelper; +import com.alibaba.nacos.client.env.NacosClientProperties; +import org.xml.sax.Attributes; + +/** + * support logback read properties from NacosClientProperties. just like springProperty. + * for example: + * + * @author onewe + */ +class NacosClientPropertyAction extends Action { + + private static final String DEFAULT_VALUE_ATTRIBUTE = "defaultValue"; + + private static final String SOURCE_ATTRIBUTE = "source"; + + @Override + public void begin(InterpretationContext ic, String elementName, Attributes attributes) throws ActionException { + String name = attributes.getValue(NAME_ATTRIBUTE); + String source = attributes.getValue(SOURCE_ATTRIBUTE); + ActionUtil.Scope scope = ActionUtil.stringToScope(attributes.getValue(SCOPE_ATTRIBUTE)); + String defaultValue = attributes.getValue(DEFAULT_VALUE_ATTRIBUTE); + if (OptionHelper.isEmpty(name)) { + addError("The \"name\" and \"source\" attributes of must be set"); + } + ActionUtil.setProperty(ic, name, getValue(source, defaultValue), scope); + } + + @Override + public void end(InterpretationContext ic, String name) throws ActionException { + + } + + private String getValue(String source, String defaultValue) { + return NacosClientProperties.PROTOTYPE.getProperty(source, defaultValue); + } +} diff --git a/client/src/main/java/com/alibaba/nacos/client/logging/logback/NacosJoranConfigurator.java b/client/src/main/java/com/alibaba/nacos/client/logging/logback/NacosJoranConfigurator.java index 9fd4a4d93bc..aa171679656 100644 --- a/client/src/main/java/com/alibaba/nacos/client/logging/logback/NacosJoranConfigurator.java +++ b/client/src/main/java/com/alibaba/nacos/client/logging/logback/NacosJoranConfigurator.java @@ -18,7 +18,9 @@ import ch.qos.logback.classic.joran.JoranConfigurator; import ch.qos.logback.core.joran.event.SaxEvent; +import ch.qos.logback.core.joran.spi.ElementSelector; import ch.qos.logback.core.joran.spi.JoranException; +import ch.qos.logback.core.joran.spi.RuleStore; import java.io.IOException; import java.io.InputStream; @@ -43,6 +45,12 @@ public class NacosJoranConfigurator extends JoranConfigurator { public void registerSafeConfiguration(List eventList) { } + @Override + public void addInstanceRules(RuleStore rs) { + super.addInstanceRules(rs); + rs.addRule(new ElementSelector("configuration/nacosClientProperty"), new NacosClientPropertyAction()); + } + /** * ensure that Nacos configuration does not affect user configuration scanning url. * diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingMaintainService.java b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingMaintainService.java index 28324323264..8c8ccaad4a2 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingMaintainService.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingMaintainService.java @@ -69,8 +69,11 @@ public NacosNamingMaintainService(String serverList) throws NacosException { } public NacosNamingMaintainService(Properties properties) throws NacosException { - NacosClientProperties clientProperties = NacosClientProperties.PROTOTYPE.derive(properties); - init(clientProperties); + this(NacosClientProperties.PROTOTYPE.derive(properties)); + } + + public NacosNamingMaintainService(NacosClientProperties properties) throws NacosException { + init(properties); } private void init(NacosClientProperties properties) throws NacosException { diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java index 1e08f794d2d..c44bed0f001 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java @@ -82,8 +82,11 @@ public NacosNamingService(String serverList) throws NacosException { } public NacosNamingService(Properties properties) throws NacosException { - final NacosClientProperties clientProperties = NacosClientProperties.PROTOTYPE.derive(properties); - init(clientProperties); + this(NacosClientProperties.PROTOTYPE.derive(properties)); + } + + public NacosNamingService(NacosClientProperties properties) throws NacosException { + init(properties); } private void init(NacosClientProperties properties) throws NacosException { diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/remote/http/NamingHttpClientManager.java b/client/src/main/java/com/alibaba/nacos/client/naming/remote/http/NamingHttpClientManager.java index bec98d790a5..1648583b2b7 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/remote/http/NamingHttpClientManager.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/remote/http/NamingHttpClientManager.java @@ -17,6 +17,7 @@ package com.alibaba.nacos.client.naming.remote.http; import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.client.env.NacosClientProperties; import com.alibaba.nacos.common.http.AbstractHttpClientFactory; import com.alibaba.nacos.common.http.HttpClientBeanHolder; import com.alibaba.nacos.common.http.HttpClientConfig; @@ -38,12 +39,12 @@ */ public class NamingHttpClientManager implements Closeable { - private static final int READ_TIME_OUT_MILLIS = Integer - .getInteger("com.alibaba.nacos.client.naming.rtimeout", 50000); + private static final int READ_TIME_OUT_MILLIS = NacosClientProperties.PROTOTYPE + .getInteger("com.alibaba.nacos.client.naming.rtimeout"); - private static final int CON_TIME_OUT_MILLIS = Integer.getInteger("com.alibaba.nacos.client.naming.ctimeout", 3000); + private static final int CON_TIME_OUT_MILLIS = NacosClientProperties.PROTOTYPE.getInteger("com.alibaba.nacos.client.naming.ctimeout"); - private static final boolean ENABLE_HTTPS = Boolean.getBoolean(TlsSystemConfig.TLS_ENABLE); + private static final boolean ENABLE_HTTPS = NacosClientProperties.PROTOTYPE.getBoolean(TlsSystemConfig.TLS_ENABLE); private static final int MAX_REDIRECTS = 5; diff --git a/client/src/main/resources/nacos-log4j2.xml b/client/src/main/resources/nacos-log4j2.xml index 08241ecd3d7..f8df24ab6b5 100644 --- a/client/src/main/resources/nacos-log4j2.xml +++ b/client/src/main/resources/nacos-log4j2.xml @@ -17,59 +17,59 @@ - + %d{yyyy-MM-dd HH:mm:ss.SSS} %p [%-5t:%c{2}] %m%n - + - - + + - + - + %d{yyyy-MM-dd HH:mm:ss.SSS} %p [%-5t:%c{2}] %m%n - + - - + + - + - + %d{yyyy-MM-dd HH:mm:ss.SSS} %p [%-5t:%c{2}] %m%n - + - - + + - + @@ -77,22 +77,22 @@ - - - - diff --git a/client/src/main/resources/nacos-logback.xml b/client/src/main/resources/nacos-logback.xml index e0d9126098f..1b883b6436c 100644 --- a/client/src/main/resources/nacos-logback.xml +++ b/client/src/main/resources/nacos-logback.xml @@ -17,16 +17,21 @@ + + + + + - ${JM.LOG.PATH}/nacos/config.log + ${logPath}/nacos/config.log - ${JM.LOG.PATH}/nacos/config.log.%i - ${JM.LOG.RETAIN.COUNT:-7} + ${logPath}/nacos/config.log.%i + ${logRetainCount} - ${JM.LOG.FILE.SIZE:-10MB} + ${logFileSize} @@ -35,15 +40,15 @@ - ${JM.LOG.PATH}/nacos/naming.log + ${logPath}/nacos/naming.log - ${JM.LOG.PATH}/nacos/naming.log.%i - ${JM.LOG.RETAIN.COUNT:-7} + ${logPath}/nacos/naming.log.%i + ${logRetainCount} - ${JM.LOG.FILE.SIZE:-10MB} + ${logFileSize} @@ -52,15 +57,15 @@ - ${JM.LOG.PATH}/nacos/remote.log + ${logPath}/nacos/remote.log - ${JM.LOG.PATH}/nacos/remote.log.%i - ${JM.LOG.RETAIN.COUNT:-7} + ${logPath}/nacos/remote.log.%i + ${logRetainCount} - ${JM.LOG.FILE.SIZE:-10MB} + ${logFileSize} @@ -69,28 +74,28 @@ - - - - -