mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
add nacos discovery starter support to connect to aliyun
This commit is contained in:
parent
90a7769180
commit
997faff710
@ -20,7 +20,7 @@
|
|||||||
<sentinel.version>1.4.1</sentinel.version>
|
<sentinel.version>1.4.1</sentinel.version>
|
||||||
<oss.version>3.1.0</oss.version>
|
<oss.version>3.1.0</oss.version>
|
||||||
<fescar.version>0.1.3</fescar.version>
|
<fescar.version>0.1.3</fescar.version>
|
||||||
<nacos.client.version>0.8.0</nacos.client.version>
|
<nacos.client.version>0.8.1</nacos.client.version>
|
||||||
<nacos.config.version>0.8.0</nacos.config.version>
|
<nacos.config.version>0.8.0</nacos.config.version>
|
||||||
<acm.version>1.0.8</acm.version>
|
<acm.version>1.0.8</acm.version>
|
||||||
<ans.version>1.0.1</ans.version>
|
<ans.version>1.0.1</ans.version>
|
||||||
|
@ -45,6 +45,10 @@
|
|||||||
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-alicloud-context</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -29,7 +29,10 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-alicloud-context</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -11,10 +11,10 @@ import com.alibaba.cloud.context.edas.EdasChangeOrderConfigurationFactory;
|
|||||||
/**
|
/**
|
||||||
* @author pbting
|
* @author pbting
|
||||||
*/
|
*/
|
||||||
public class NacosParameterInitListener
|
public class NacosConfigParameterInitListener
|
||||||
extends AbstractOnceApplicationListener<ApplicationEnvironmentPreparedEvent> {
|
extends AbstractOnceApplicationListener<ApplicationEnvironmentPreparedEvent> {
|
||||||
private static final Logger log = LoggerFactory
|
private static final Logger log = LoggerFactory
|
||||||
.getLogger(NacosParameterInitListener.class);
|
.getLogger(NacosConfigParameterInitListener.class);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String conditionalOnClass() {
|
protected String conditionalOnClass() {
|
@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2019 the original author or authors.
|
||||||
|
*
|
||||||
|
* 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 org.springframework.cloud.alicloud.context.nacos;
|
||||||
|
|
||||||
|
import com.alibaba.cloud.context.edas.EdasChangeOrderConfiguration;
|
||||||
|
import com.alibaba.cloud.context.edas.EdasChangeOrderConfigurationFactory;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
|
||||||
|
import org.springframework.cloud.alicloud.context.listener.AbstractOnceApplicationListener;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author pbting
|
||||||
|
* @date 2019-02-14 11:12 AM
|
||||||
|
*/
|
||||||
|
public class NacosDiscoveryParameterInitListener
|
||||||
|
extends AbstractOnceApplicationListener<ApplicationEnvironmentPreparedEvent> {
|
||||||
|
private static final Logger log = LoggerFactory
|
||||||
|
.getLogger(NacosDiscoveryParameterInitListener.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String conditionalOnClass() {
|
||||||
|
return "org.springframework.cloud.alibaba.nacos.NacosDiscoveryAutoConfiguration";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void handleEvent(ApplicationEnvironmentPreparedEvent event) {
|
||||||
|
EdasChangeOrderConfiguration edasChangeOrderConfiguration = EdasChangeOrderConfigurationFactory
|
||||||
|
.getEdasChangeOrderConfiguration();
|
||||||
|
|
||||||
|
log.info(
|
||||||
|
"Initialize Nacos Discovery Parameter from edas change order,is edas managed {}.",
|
||||||
|
edasChangeOrderConfiguration.isEdasManaged());
|
||||||
|
|
||||||
|
if (!edasChangeOrderConfiguration.isEdasManaged()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// initialize nacos configuration
|
||||||
|
Properties properties = System.getProperties();
|
||||||
|
|
||||||
|
// step 1: set some properties for spring cloud alibaba nacos discovery
|
||||||
|
properties.setProperty("spring.cloud.nacos.discovery.server-addr", "");
|
||||||
|
properties.setProperty("spring.cloud.nacos.discovery.endpoint",
|
||||||
|
edasChangeOrderConfiguration.getAddressServerDomain());
|
||||||
|
properties.setProperty("spring.cloud.nacos.discovery.namespace",
|
||||||
|
edasChangeOrderConfiguration.getTenantId());
|
||||||
|
properties.setProperty("spring.cloud.nacos.discovery.access-key",
|
||||||
|
edasChangeOrderConfiguration.getDauthAccessKey());
|
||||||
|
properties.setProperty("spring.cloud.nacos.discovery.secret-key",
|
||||||
|
edasChangeOrderConfiguration.getDauthSecretKey());
|
||||||
|
|
||||||
|
// step 2: set these properties for nacos client
|
||||||
|
properties.setProperty("webContext", "/vipserver");
|
||||||
|
properties.setProperty("serverPort", "80");
|
||||||
|
}
|
||||||
|
}
|
@ -10,5 +10,6 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
|||||||
org.springframework.cloud.alicloud.context.sms.SmsContextAutoConfiguration
|
org.springframework.cloud.alicloud.context.sms.SmsContextAutoConfiguration
|
||||||
org.springframework.context.ApplicationListener=\
|
org.springframework.context.ApplicationListener=\
|
||||||
org.springframework.cloud.alicloud.context.ans.AnsContextApplicationListener,\
|
org.springframework.cloud.alicloud.context.ans.AnsContextApplicationListener,\
|
||||||
org.springframework.cloud.alicloud.context.nacos.NacosParameterInitListener,\
|
org.springframework.cloud.alicloud.context.nacos.NacosConfigParameterInitListener,\
|
||||||
|
org.springframework.cloud.alicloud.context.nacos.NacosDiscoveryParameterInitListener,\
|
||||||
org.springframework.cloud.alicloud.context.sentinel.SentinelAliCloudListener
|
org.springframework.cloud.alicloud.context.sentinel.SentinelAliCloudListener
|
@ -16,23 +16,22 @@
|
|||||||
|
|
||||||
package org.springframework.cloud.alicloud.context.nacos;
|
package org.springframework.cloud.alicloud.context.nacos;
|
||||||
|
|
||||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
import com.alibaba.cloud.context.ans.AliCloudAnsInitializer;
|
||||||
|
import com.alibaba.cloud.context.edas.EdasChangeOrderConfigurationFactory;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||||
import org.springframework.cloud.alicloud.context.BaseAliCloudSpringApplication;
|
import org.springframework.cloud.alicloud.context.BaseAliCloudSpringApplication;
|
||||||
import org.springframework.cloud.alicloud.utils.ChangeOrderUtils;
|
import org.springframework.cloud.alicloud.utils.ChangeOrderUtils;
|
||||||
|
|
||||||
import com.alibaba.cloud.context.ans.AliCloudAnsInitializer;
|
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||||
import com.alibaba.cloud.context.edas.EdasChangeOrderConfigurationFactory;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author xiaolongzuo
|
* @author xiaolongzuo
|
||||||
*/
|
*/
|
||||||
@PrepareForTest({ EdasChangeOrderConfigurationFactory.class,
|
@PrepareForTest({ EdasChangeOrderConfigurationFactory.class,
|
||||||
NacosParameterInitListener.class, AliCloudAnsInitializer.class })
|
NacosConfigParameterInitListener.class, AliCloudAnsInitializer.class })
|
||||||
public class NacosParameterInitListenerTests extends BaseAliCloudSpringApplication {
|
public class NacosConfigParameterInitListenerTests extends BaseAliCloudSpringApplication {
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void setUp() {
|
public static void setUp() {
|
@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2018 the original author or authors.
|
||||||
|
*
|
||||||
|
* 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 org.springframework.cloud.alicloud.context.nacos;
|
||||||
|
|
||||||
|
import com.alibaba.cloud.context.ans.AliCloudAnsInitializer;
|
||||||
|
import com.alibaba.cloud.context.edas.EdasChangeOrderConfigurationFactory;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||||
|
import org.springframework.cloud.alicloud.context.BaseAliCloudSpringApplication;
|
||||||
|
import org.springframework.cloud.alicloud.utils.ChangeOrderUtils;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xiaolongzuo
|
||||||
|
*/
|
||||||
|
@PrepareForTest({EdasChangeOrderConfigurationFactory.class,
|
||||||
|
NacosConfigParameterInitListener.class, AliCloudAnsInitializer.class})
|
||||||
|
public class NacosDiscoveryParameterInitListenerTests extends BaseAliCloudSpringApplication {
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void setUp() {
|
||||||
|
ChangeOrderUtils.mockChangeOrder();
|
||||||
|
System.getProperties().setProperty("webContext", "/vipserver");
|
||||||
|
System.getProperties().setProperty("serverPort", "80");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNacosParameterInitListener() {
|
||||||
|
assertThat(System.getProperty("spring.cloud.nacos.config.server-addr"))
|
||||||
|
.isEqualTo("");
|
||||||
|
assertThat(System.getProperty("spring.cloud.nacos.config.endpoint"))
|
||||||
|
.isEqualTo("testDomain");
|
||||||
|
assertThat(System.getProperty("spring.cloud.nacos.config.namespace"))
|
||||||
|
.isEqualTo("testTenantId");
|
||||||
|
assertThat(System.getProperty("spring.cloud.nacos.config.access-key"))
|
||||||
|
.isEqualTo("testAK");
|
||||||
|
assertThat(System.getProperty("spring.cloud.nacos.config.secret-key"))
|
||||||
|
.isEqualTo("testSK");
|
||||||
|
assertThat(System.getProperties().getProperty("webContext")).isEqualTo("/vipserver");
|
||||||
|
assertThat(System.getProperties().getProperty("serverPort")).isEqualTo("80");
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user