mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
commit
7ce2f7a27e
@ -34,9 +34,14 @@ import java.util.List;
|
|||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import com.alibaba.nacos.api.NacosFactory;
|
import com.alibaba.nacos.api.NacosFactory;
|
||||||
import com.alibaba.nacos.api.config.ConfigService;
|
import com.alibaba.nacos.api.config.ConfigService;
|
||||||
@ -47,6 +52,7 @@ import com.alibaba.nacos.api.config.ConfigService;
|
|||||||
* @author leijuan
|
* @author leijuan
|
||||||
* @author xiaojing
|
* @author xiaojing
|
||||||
* @author pbting
|
* @author pbting
|
||||||
|
* @author <a href="mailto:lyuzb@lyuzb.com">lyuzb</a>
|
||||||
*/
|
*/
|
||||||
@ConfigurationProperties(NacosConfigProperties.PREFIX)
|
@ConfigurationProperties(NacosConfigProperties.PREFIX)
|
||||||
public class NacosConfigProperties {
|
public class NacosConfigProperties {
|
||||||
@ -56,6 +62,24 @@ public class NacosConfigProperties {
|
|||||||
private static final Logger log = LoggerFactory
|
private static final Logger log = LoggerFactory
|
||||||
.getLogger(NacosConfigProperties.class);
|
.getLogger(NacosConfigProperties.class);
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private Environment environment;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
this.overrideFromEnv();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void overrideFromEnv() {
|
||||||
|
if (StringUtils.isEmpty(this.getServerAddr())) {
|
||||||
|
String serverAddr = environment.resolvePlaceholders("${spring.cloud.nacos.config.server-addr:}");
|
||||||
|
if(StringUtils.isEmpty(serverAddr)) {
|
||||||
|
serverAddr = environment.resolvePlaceholders("${spring.cloud.nacos.server-addr}");
|
||||||
|
}
|
||||||
|
this.setServerAddr(serverAddr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* nacos config server address.
|
* nacos config server address.
|
||||||
*/
|
*/
|
||||||
|
@ -1,5 +1,16 @@
|
|||||||
{
|
{
|
||||||
"properties": [
|
"properties": [
|
||||||
|
{
|
||||||
|
"name": "spring.cloud.nacos.server-addr",
|
||||||
|
"type": "java.lang.String",
|
||||||
|
"description": "nacos server address."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "spring.cloud.nacos.config.server-addr",
|
||||||
|
"type": "java.lang.String",
|
||||||
|
"defaultValue": "${spring.cloud.nacos.server-addr}",
|
||||||
|
"description": "nacos config server address."
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "spring.cloud.nacos.config.encode",
|
"name": "spring.cloud.nacos.config.encode",
|
||||||
"type": "java.lang.String",
|
"type": "java.lang.String",
|
||||||
|
@ -0,0 +1,41 @@
|
|||||||
|
package com.alibaba.cloud.nacos;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="mailto:lyuzb@lyuzb.com">lyuzb</a>
|
||||||
|
*/
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(classes = NacosConfigPropertiesServerAddressBothLevelTests.TestConfig.class, properties = {
|
||||||
|
"spring.cloud.nacos.config.server-addr=321,321,321,321:8848",
|
||||||
|
"spring.cloud.nacos.server-addr=123.123.123.123:8848"
|
||||||
|
}, webEnvironment = RANDOM_PORT)
|
||||||
|
public class NacosConfigPropertiesServerAddressBothLevelTests {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private NacosConfigProperties properties;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetServerAddr() {
|
||||||
|
assertEquals("NacosConfigProperties server address was wrong","321,321,321,321:8848", properties.getServerAddr());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableAutoConfiguration
|
||||||
|
@ImportAutoConfiguration({ NacosConfigEndpointAutoConfiguration.class,
|
||||||
|
NacosConfigAutoConfiguration.class, NacosConfigBootstrapConfiguration.class })
|
||||||
|
public static class TestConfig {
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
package com.alibaba.cloud.nacos;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="mailto:lyuzb@lyuzb.com">lyuzb</a>
|
||||||
|
*/
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(classes = NacosConfigPropertiesServerAddressTopLevelTests.TestConfig.class, properties = {
|
||||||
|
"spring.cloud.nacos.server-addr=123.123.123.123:8848"
|
||||||
|
}, webEnvironment = RANDOM_PORT)
|
||||||
|
public class NacosConfigPropertiesServerAddressTopLevelTests {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private NacosConfigProperties properties;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetServerAddr() {
|
||||||
|
assertEquals("NacosConfigProperties server address was wrong","123.123.123.123:8848", properties.getServerAddr());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableAutoConfiguration
|
||||||
|
@ImportAutoConfiguration({ NacosConfigEndpointAutoConfiguration.class,
|
||||||
|
NacosConfigAutoConfiguration.class, NacosConfigBootstrapConfiguration.class })
|
||||||
|
public static class TestConfig {
|
||||||
|
}
|
||||||
|
}
|
@ -57,6 +57,7 @@ import com.alibaba.nacos.client.naming.utils.UtilAndComs;
|
|||||||
* @author dungu.zpf
|
* @author dungu.zpf
|
||||||
* @author xiaojing
|
* @author xiaojing
|
||||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||||
|
* @author <a href="mailto:lyuzb@lyuzb.com">lyuzb</a>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ConfigurationProperties("spring.cloud.nacos.discovery")
|
@ConfigurationProperties("spring.cloud.nacos.discovery")
|
||||||
@ -411,8 +412,11 @@ public class NacosDiscoveryProperties {
|
|||||||
public void overrideFromEnv(Environment env) {
|
public void overrideFromEnv(Environment env) {
|
||||||
|
|
||||||
if (StringUtils.isEmpty(this.getServerAddr())) {
|
if (StringUtils.isEmpty(this.getServerAddr())) {
|
||||||
this.setServerAddr(env
|
String serverAddr = env.resolvePlaceholders("${spring.cloud.nacos.discovery.server-addr:}");
|
||||||
.resolvePlaceholders("${spring.cloud.nacos.discovery.server-addr:}"));
|
if(StringUtils.isEmpty(serverAddr)) {
|
||||||
|
serverAddr = env.resolvePlaceholders("${spring.cloud.nacos.server-addr}");
|
||||||
|
}
|
||||||
|
this.setServerAddr(serverAddr);
|
||||||
}
|
}
|
||||||
if (StringUtils.isEmpty(this.getNamespace())) {
|
if (StringUtils.isEmpty(this.getNamespace())) {
|
||||||
this.setNamespace(env
|
this.setNamespace(env
|
||||||
|
@ -1,4 +1,15 @@
|
|||||||
{"properties": [
|
{"properties": [
|
||||||
|
{
|
||||||
|
"name": "spring.cloud.nacos.server-addr",
|
||||||
|
"type": "java.lang.String",
|
||||||
|
"description": "nacos server address."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "spring.cloud.nacos.discovery.server-addr",
|
||||||
|
"type": "java.lang.String",
|
||||||
|
"defaultValue": "${spring.cloud.nacos.server-addr}",
|
||||||
|
"description": "nacos discovery server address."
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "spring.cloud.nacos.discovery.service",
|
"name": "spring.cloud.nacos.discovery.service",
|
||||||
"type": "java.lang.String",
|
"type": "java.lang.String",
|
||||||
|
@ -0,0 +1,45 @@
|
|||||||
|
package com.alibaba.cloud.nacos;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import com.alibaba.cloud.nacos.NacosDiscoveryAutoConfiguration;
|
||||||
|
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
||||||
|
import com.alibaba.cloud.nacos.discovery.NacosDiscoveryClientAutoConfiguration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="mailto:lyuzb@lyuzb.com">lyuzb</a>
|
||||||
|
*/
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(classes = NacosDiscoveryPropertiesServerAddressBothLevelTests.TestConfig.class, properties = {
|
||||||
|
"spring.cloud.nacos.discovery.server-addr=321.321.321.321:8848",
|
||||||
|
"spring.cloud.nacos.server-addr=123.123.123.123:8848"
|
||||||
|
}, webEnvironment = RANDOM_PORT)
|
||||||
|
public class NacosDiscoveryPropertiesServerAddressBothLevelTests {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private NacosDiscoveryProperties properties;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetServerAddr() {
|
||||||
|
assertEquals("NacosDiscoveryProperties server address was wrong","321.321.321.321:8848", properties.getServerAddr());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableAutoConfiguration
|
||||||
|
@ImportAutoConfiguration({ AutoServiceRegistrationConfiguration.class,
|
||||||
|
NacosDiscoveryClientAutoConfiguration.class,
|
||||||
|
NacosDiscoveryAutoConfiguration.class })
|
||||||
|
public static class TestConfig {
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,47 @@
|
|||||||
|
package com.alibaba.cloud.nacos;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import com.alibaba.cloud.nacos.NacosDiscoveryAutoConfiguration;
|
||||||
|
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
||||||
|
import com.alibaba.cloud.nacos.discovery.NacosDiscoveryClientAutoConfiguration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:lyuzb@lyuzb.com">lyuzb</a>
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(classes = NacosDiscoveryPropertiesServerAddressTopLevelTests.TestConfig.class, properties = {
|
||||||
|
"spring.cloud.nacos.server-addr=123.123.123.123:8848"
|
||||||
|
}, webEnvironment = RANDOM_PORT)
|
||||||
|
|
||||||
|
public class NacosDiscoveryPropertiesServerAddressTopLevelTests {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private NacosDiscoveryProperties properties;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetServerAddr() {
|
||||||
|
assertEquals("NacosDiscoveryProperties server address was wrong","123.123.123.123:8848", properties.getServerAddr());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableAutoConfiguration
|
||||||
|
@ImportAutoConfiguration({ AutoServiceRegistrationConfiguration.class,
|
||||||
|
NacosDiscoveryClientAutoConfiguration.class,
|
||||||
|
NacosDiscoveryAutoConfiguration.class })
|
||||||
|
public static class TestConfig {
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user