Commit f2c18352 by Nepxion

Nacos配置支持客户自定义

parent 5b397658
...@@ -21,8 +21,8 @@ ...@@ -21,8 +21,8 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-context</artifactId> <artifactId>spring-boot-autoconfigure</artifactId>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>
\ No newline at end of file
...@@ -11,7 +11,9 @@ package com.nepxion.discovery.common.nacos.configuration; ...@@ -11,7 +11,9 @@ package com.nepxion.discovery.common.nacos.configuration;
import java.util.Properties; import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
...@@ -28,11 +30,21 @@ public class NacosAutoConfiguration { ...@@ -28,11 +30,21 @@ public class NacosAutoConfiguration {
private Environment environment; private Environment environment;
@Bean @Bean
@ConditionalOnMissingBean
public ConfigService configService() throws NacosException { public ConfigService configService() throws NacosException {
Properties properties = new Properties();
String url = environment.getProperty(NacosConstant.URL); String url = environment.getProperty(NacosConstant.URL);
if (StringUtils.isNotEmpty(url)) {
properties.put(NacosConstant.SERVER_ADDR, url);
} else {
throw new IllegalArgumentException("Url can't be null or empty");
}
Properties properties = new Properties(); String namespace = environment.getProperty(NacosConstant.NAMESPACE);
properties.put(NacosConstant.URL_KEY, url); if (StringUtils.isNotEmpty(namespace)) {
properties.put(NacosConstant.NAMESPACE, namespace);
}
return NacosFactory.createConfigService(properties); return NacosFactory.createConfigService(properties);
} }
......
...@@ -9,10 +9,12 @@ package com.nepxion.discovery.common.nacos.constant; ...@@ -9,10 +9,12 @@ package com.nepxion.discovery.common.nacos.constant;
* @version 1.0 * @version 1.0
*/ */
public class NacosConstant { import com.alibaba.nacos.api.PropertyKeyConst;
public static final String URL_KEY = "serverAddr";
public class NacosConstant extends PropertyKeyConst {
public static final String URL = "nacos.url"; public static final String URL = "nacos.url";
public static final String TIMEOUT = "nacos.timout"; public static final String TIMEOUT = "nacos.timout";
public static final String NAMESPACE = "nacos.namespace";
public static final long DEFAULT_TIMEOUT = 30000; public static final long DEFAULT_TIMEOUT = 30000;
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment