1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

nacos discovery support register service to ANS

This commit is contained in:
得少 2019-02-18 12:49:31 +08:00
parent 3ac295747b
commit 92d559c7fb
13 changed files with 156 additions and 24 deletions

View File

@ -18,7 +18,7 @@
<properties>
<sentinel.version>1.4.0</sentinel.version>
<oss.version>3.1.0</oss.version>
<nacos.version>0.6.2</nacos.version>
<nacos.version>0.8.1</nacos.version>
<fescar.version>0.1.3</fescar.version>
<acm.version>1.0.8</acm.version>
<ans.version>1.0.1</ans.version>
@ -173,7 +173,7 @@
<version>${fescar.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.fescar</groupId>
<groupId>com.alibaba.fescar</groupId>
<artifactId>fescar-common</artifactId>
<version>${fescar.version}</version>
</dependency>

View File

@ -45,6 +45,10 @@
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alicloud-context</artifactId>
</dependency>
</dependencies>
<build>

View File

@ -32,7 +32,7 @@ public class ConsumerApplication {
SpringApplication.run(ConsumerApplication.class, args);
}
@FeignClient(name = "service-provider", fallback = EchoServiceFallback.class, configuration = FeignConfiguration.class)
@FeignClient(name = "service-provider-1X", fallback = EchoServiceFallback.class, configuration = FeignConfiguration.class)
public interface EchoService {
@RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
String echo(@PathVariable("str") String str);

View File

@ -3,11 +3,7 @@ package org.springframework.cloud.alibaba.cloud.examples;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.alibaba.cloud.examples.ConsumerApplication.EchoService;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
/**
@ -26,7 +22,7 @@ public class TestController {
@RequestMapping(value = "/echo-rest/{str}", method = RequestMethod.GET)
public String rest(@PathVariable String str) {
return restTemplate.getForObject("http://service-provider/echo/" + str,
return restTemplate.getForObject("http://service-provider-1X/echo/" + str,
String.class);
}

View File

@ -1,12 +1,9 @@
spring.application.name=service-consumer
spring.application.name=service-consumer-1X
server.port=18083
management.security.enabled=false
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
feign.sentinel.enabled=true
spring.cloud.sentinel.transport.dashboard=localhost:8080
spring.cloud.sentinel.eager=true
spring.cloud.sentinel.datasource.ds1.file.file=classpath: flowrule.json
spring.cloud.sentinel.datasource.ds1.file.data-type=json

View File

@ -1,6 +1,6 @@
[
{
"resource": "GET:http://service-provider/echo/{str}",
"resource": "GET:http://service-provider-1X/echo/{str}",
"controlBehavior": 0,
"count": 1,
"grade": 1,

View File

@ -30,6 +30,10 @@
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alicloud-context</artifactId>
</dependency>
</dependencies>
<build>

View File

@ -1,4 +1,4 @@
server.port=18082
spring.application.name=service-provider
spring.application.name=service-provider-1X
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
management.security.enabled=false

View File

@ -11,10 +11,10 @@ import com.alibaba.cloud.context.edas.EdasChangeOrderConfigurationFactory;
/**
* @author pbting
*/
public class NacosParameterInitListener
public class NacosConfigParameterInitListener
extends AbstractOnceApplicationListener<ApplicationEnvironmentPreparedEvent> {
private static final Logger log = LoggerFactory
.getLogger(NacosParameterInitListener.class);
.getLogger(NacosConfigParameterInitListener.class);
@Override
protected String conditionalOnClass() {

View File

@ -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");
}
}

View File

@ -10,5 +10,6 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.alicloud.context.sms.SmsContextAutoConfiguration
org.springframework.context.ApplicationListener=\
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

View File

@ -16,23 +16,22 @@
package org.springframework.cloud.alicloud.context.nacos;
import static org.assertj.core.api.Assertions.assertThat;
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 com.alibaba.cloud.context.ans.AliCloudAnsInitializer;
import com.alibaba.cloud.context.edas.EdasChangeOrderConfigurationFactory;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author xiaolongzuo
*/
@PrepareForTest({ EdasChangeOrderConfigurationFactory.class,
NacosParameterInitListener.class, AliCloudAnsInitializer.class })
public class NacosParameterInitListenerTests extends BaseAliCloudSpringApplication {
NacosConfigParameterInitListener.class, AliCloudAnsInitializer.class })
public class NacosConfigParameterInitListenerTests extends BaseAliCloudSpringApplication {
@BeforeClass
public static void setUp() {

View File

@ -0,0 +1,60 @@
/*
* 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.Assertions.assertThat;
/**
* @author xiaolongzuo
*/
@PrepareForTest({ EdasChangeOrderConfigurationFactory.class,
NacosDiscoveryParameterInitListener.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");
}
}