commit
707dd54d09
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.alibaba.dubbo.registry;
|
||||
|
||||
import com.alibaba.dubbo.common.Constants;
|
||||
import com.alibaba.dubbo.common.URL;
|
||||
import com.alibaba.dubbo.common.utils.NetUtils;
|
||||
|
||||
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.serviceregistry.Registration;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
/**
|
||||
* Abstract {@link RegistrationFactory} implementation
|
||||
* <p>
|
||||
*
|
||||
* @param <T> The subclass of {@link Registration}
|
||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||
*/
|
||||
public abstract class AbstractRegistrationFactory<T extends Registration> implements RegistrationFactory<T> {
|
||||
|
||||
protected ServiceInstance createServiceInstance(String serviceName, URL url) {
|
||||
// Append default category if absent
|
||||
String category = url.getParameter(Constants.CATEGORY_KEY, Constants.DEFAULT_CATEGORY);
|
||||
URL newURL = url.addParameter(Constants.CATEGORY_KEY, category);
|
||||
newURL = newURL.addParameter(Constants.PROTOCOL_KEY, url.getProtocol());
|
||||
String ip = NetUtils.getLocalHost();
|
||||
int port = newURL.getParameter(Constants.BIND_PORT_KEY, url.getPort());
|
||||
DefaultServiceInstance serviceInstance = new DefaultServiceInstance(url.toIdentityString(), serviceName, ip, port, false);
|
||||
serviceInstance.getMetadata().putAll(new LinkedHashMap<>(newURL.getParameters()));
|
||||
return serviceInstance;
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.alibaba.dubbo.registry;
|
||||
|
||||
import com.alibaba.dubbo.common.URL;
|
||||
|
||||
import org.springframework.cloud.client.serviceregistry.Registration;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
/**
|
||||
* Default {@link RegistrationFactory}
|
||||
*
|
||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||
*/
|
||||
public class DefaultRegistrationFactory extends AbstractRegistrationFactory<Registration> {
|
||||
|
||||
@Override
|
||||
public Registration create(String serviceName, URL url, ApplicationContext applicationContext) {
|
||||
return new DelegatingRegistration(createServiceInstance(serviceName, url));
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.alibaba.dubbo.registry;
|
||||
|
||||
import com.alibaba.dubbo.common.URL;
|
||||
|
||||
import org.springframework.cloud.client.serviceregistry.Registration;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
|
||||
/**
|
||||
* {@link Registration} Factory to createServiceInstance a instance of {@link Registration}
|
||||
*
|
||||
* @param <T> The subclass of {@link Registration}
|
||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||
*/
|
||||
public interface RegistrationFactory<T extends Registration> {
|
||||
|
||||
/**
|
||||
* Create a instance of {@link T}
|
||||
*
|
||||
* @param serviceName The service name of Dubbo service interface
|
||||
* @param url The Dubbo's URL
|
||||
* @param applicationContext {@link ApplicationContext}
|
||||
* @return a instance of {@link T}
|
||||
*/
|
||||
T create(String serviceName, URL url, ApplicationContext applicationContext);
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.alibaba.dubbo.registry.apache.zookeeper;
|
||||
|
||||
import com.alibaba.dubbo.common.URL;
|
||||
|
||||
import org.springframework.cloud.alibaba.dubbo.registry.AbstractRegistrationFactory;
|
||||
import org.springframework.cloud.alibaba.dubbo.registry.RegistrationFactory;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.zookeeper.discovery.ZookeeperInstance;
|
||||
import org.springframework.cloud.zookeeper.serviceregistry.ServiceInstanceRegistration;
|
||||
import org.springframework.cloud.zookeeper.serviceregistry.ZookeeperRegistration;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
/**
|
||||
* Zookeeper {@link RegistrationFactory}
|
||||
*
|
||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||
*/
|
||||
public class ZookeeperRegistrationFactory extends AbstractRegistrationFactory<ZookeeperRegistration> {
|
||||
|
||||
@Override
|
||||
public ZookeeperRegistration create(String serviceName, URL url, ApplicationContext applicationContext) {
|
||||
|
||||
ServiceInstance serviceInstance = createServiceInstance(serviceName, url);
|
||||
|
||||
ZookeeperInstance zookeeperInstance = new ZookeeperInstance(serviceInstance.getInstanceId(),
|
||||
serviceInstance.getServiceId(), serviceInstance.getMetadata());
|
||||
|
||||
ZookeeperRegistration registration = ServiceInstanceRegistration
|
||||
.builder()
|
||||
.address(serviceInstance.getHost())
|
||||
.name(serviceInstance.getServiceId())
|
||||
.payload(zookeeperInstance)
|
||||
.port(serviceInstance.getPort())
|
||||
.build();
|
||||
|
||||
return registration;
|
||||
}
|
||||
}
|
@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.alibaba.dubbo.registry.hashicorp.consul;
|
||||
|
||||
import com.alibaba.dubbo.common.URL;
|
||||
|
||||
import com.ecwid.consul.v1.agent.model.NewService;
|
||||
import org.springframework.cloud.alibaba.dubbo.registry.AbstractRegistrationFactory;
|
||||
import org.springframework.cloud.alibaba.dubbo.registry.RegistrationFactory;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties;
|
||||
import org.springframework.cloud.consul.discovery.ConsulServerUtils;
|
||||
import org.springframework.cloud.consul.serviceregistry.ConsulRegistration;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* {@link ConsulRegistration} {@link RegistrationFactory} implementation
|
||||
*
|
||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||
*/
|
||||
public class ConsulRegistrationFactory extends AbstractRegistrationFactory<ConsulRegistration> {
|
||||
|
||||
@Override
|
||||
public ConsulRegistration create(String serviceName, URL url, ApplicationContext applicationContext) {
|
||||
ServiceInstance serviceInstance = createServiceInstance(serviceName, url);
|
||||
|
||||
Map<String, String> metadata = getMetadata(serviceInstance);
|
||||
List<String> tags = createTags(metadata);
|
||||
|
||||
NewService service = new NewService();
|
||||
service.setId(serviceInstance.getInstanceId());
|
||||
service.setName(serviceInstance.getServiceId());
|
||||
service.setAddress(serviceInstance.getHost());
|
||||
service.setPort(serviceInstance.getPort());
|
||||
service.setMeta(metadata);
|
||||
service.setTags(tags);
|
||||
|
||||
ConsulDiscoveryProperties properties = applicationContext.getBean(ConsulDiscoveryProperties.class);
|
||||
|
||||
ConsulRegistration registration = new ConsulRegistration(service, properties);
|
||||
return registration;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param metadata
|
||||
* @return
|
||||
* @see ConsulServerUtils#getMetadata(java.util.List)
|
||||
*/
|
||||
private List<String> createTags(Map<String, String> metadata) {
|
||||
List<String> tags = new LinkedList<>();
|
||||
for (Map.Entry<String, String> entry : metadata.entrySet()) {
|
||||
String tag = entry.getKey() + "=" + entry.getValue();
|
||||
tags.add(tag);
|
||||
|
||||
}
|
||||
return tags;
|
||||
}
|
||||
|
||||
private Map<String, String> getMetadata(ServiceInstance serviceInstance) {
|
||||
Map<String, String> metadata = serviceInstance.getMetadata();
|
||||
Set<String> removedKeys = new LinkedHashSet<>();
|
||||
for (String key : metadata.keySet()) {
|
||||
if (key.contains(".")) {
|
||||
removedKeys.add(key);
|
||||
}
|
||||
}
|
||||
for (String removedKey : removedKeys) {
|
||||
metadata.remove(removedKey);
|
||||
}
|
||||
return metadata;
|
||||
}
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.alibaba.dubbo.registry.netflix.eureka;
|
||||
|
||||
import com.alibaba.dubbo.common.URL;
|
||||
|
||||
import com.netflix.appinfo.HealthCheckHandler;
|
||||
import com.netflix.discovery.EurekaClientConfig;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.cloud.alibaba.dubbo.registry.AbstractRegistrationFactory;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.commons.util.InetUtils;
|
||||
import org.springframework.cloud.netflix.eureka.CloudEurekaInstanceConfig;
|
||||
import org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean;
|
||||
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaRegistration;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
/**
|
||||
* {@link EurekaRegistration} Factory
|
||||
*
|
||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||
*/
|
||||
public class EurekaRegistrationFactory extends AbstractRegistrationFactory<EurekaRegistration> {
|
||||
|
||||
@Override
|
||||
public EurekaRegistration create(String serviceName, URL url, ApplicationContext applicationContext) {
|
||||
ServiceInstance serviceInstance = createServiceInstance(serviceName, url);
|
||||
CloudEurekaInstanceConfig cloudEurekaInstanceConfig = applicationContext.getBean(CloudEurekaInstanceConfig.class);
|
||||
ObjectProvider<HealthCheckHandler> healthCheckHandler = applicationContext.getBeanProvider(HealthCheckHandler.class);
|
||||
EurekaClientConfig eurekaClientConfig = applicationContext.getBean(EurekaClientConfig.class);
|
||||
InetUtils inetUtils = applicationContext.getBean(InetUtils.class);
|
||||
EurekaInstanceConfigBean eurekaInstanceConfigBean = new EurekaInstanceConfigBean(inetUtils);
|
||||
BeanUtils.copyProperties(cloudEurekaInstanceConfig, eurekaInstanceConfigBean);
|
||||
String serviceId = serviceInstance.getServiceId();
|
||||
eurekaInstanceConfigBean.setInstanceId(serviceInstance.getInstanceId());
|
||||
eurekaInstanceConfigBean.setVirtualHostName(serviceId);
|
||||
eurekaInstanceConfigBean.setSecureVirtualHostName(serviceId);
|
||||
eurekaInstanceConfigBean.setAppname(serviceId);
|
||||
eurekaInstanceConfigBean.setHostname(serviceInstance.getHost());
|
||||
eurekaInstanceConfigBean.setMetadataMap(serviceInstance.getMetadata());
|
||||
|
||||
return EurekaRegistration.builder(eurekaInstanceConfigBean)
|
||||
.with(healthCheckHandler)
|
||||
.with(eurekaClientConfig, applicationContext)
|
||||
.build();
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
spring.application.name=sca-nacos-config
|
||||
spring.application.name=nacos-config-example
|
||||
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
|
||||
spring.cloud.nacos.config.shared-data-ids=base-common.properties,common.properties
|
||||
spring.cloud.nacos.config.refreshable-dataids=common.properties
|
@ -0,0 +1,182 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>spring-cloud-alibaba-examples</artifactId>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<version>0.2.2.BUILD-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dubbo-examples</artifactId>
|
||||
<name>Spring Cloud Alibaba Dubbo Examples</name>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
<module>spring-cloud-dubbo-sample-api</module>
|
||||
<module>spring-cloud-dubbo-provider-sample</module>
|
||||
<module>spring-cloud-dubbo-consumer-sample</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
<dubbo.version>2.6.5</dubbo.version>
|
||||
<dubbo-spring-boot.version>0.2.1.RELEASE</dubbo-spring-boot.version>
|
||||
<dubbo-registry-nacos.version>0.0.2</dubbo-registry-nacos.version>
|
||||
<spring-cloud-zookeeper.version>2.1.0.RELEASE</spring-cloud-zookeeper.version>
|
||||
<spring-cloud-consul.version>2.1.0.RELEASE</spring-cloud-consul.version>
|
||||
<curator.version>4.0.1</curator.version>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- Spring Boot dependencies -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-dependencies</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Apache Dubbo dependencies-->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>dubbo-dependencies-bom</artifactId>
|
||||
<version>${dubbo.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-actuator-autoconfigure</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Cloud dependencies -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-commons</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-context</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Dubbo Spring Cloud Starter -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-dubbo</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<profiles>
|
||||
|
||||
<!-- Nacos -->
|
||||
<profile>
|
||||
<id>nacos</id>
|
||||
<activation>
|
||||
<activeByDefault>true</activeByDefault>
|
||||
</activation>
|
||||
<dependencies>
|
||||
<!-- Nacos Service Discovery -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</profile>
|
||||
|
||||
<profile>
|
||||
<id>eureka</id>
|
||||
<dependencies>
|
||||
<!-- Eureka Service Discovery -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</profile>
|
||||
|
||||
<!-- Zookeeper -->
|
||||
<profile>
|
||||
<id>zookeeper</id>
|
||||
<dependencies>
|
||||
<!-- Zookeeper Service Discovery -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
|
||||
<version>${spring-cloud-zookeeper.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.apache.zookeeper</groupId>
|
||||
<artifactId>zookeeper</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.zookeeper</groupId>
|
||||
<artifactId>zookeeper</artifactId>
|
||||
<version>3.4.12</version>
|
||||
<optional>true</optional>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.curator</groupId>
|
||||
<artifactId>curator-framework</artifactId>
|
||||
<version>${curator.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</profile>
|
||||
|
||||
<profile>
|
||||
<id>consul</id>
|
||||
<dependencies>
|
||||
<!-- Spring Cloud Consul -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
|
||||
<version>${spring-cloud-consul.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</profile>
|
||||
|
||||
</profiles>
|
||||
|
||||
|
||||
</project>
|
@ -0,0 +1,38 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>spring-cloud-alibaba-dubbo-examples</artifactId>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<version>0.2.2.BUILD-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dubbo-consumer-sample</artifactId>
|
||||
<name>Spring Cloud Dubbo Consumer Sample</name>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Sample API -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dubbo-sample-api</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Cloud Open Feign -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
30
spring-cloud-alibaba-dubbo/src/test/java/org/springframework/cloud/alibaba/dubbo/bootstrap/DubboSpringCloudBootstrap.java → spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-consumer-sample/src/main/java/org/springframework/cloud/alibaba/dubbo/bootstrap/DubboSpringCloudConsumerBootstrap.java
30
spring-cloud-alibaba-dubbo/src/test/java/org/springframework/cloud/alibaba/dubbo/bootstrap/DubboSpringCloudBootstrap.java → spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-consumer-sample/src/main/java/org/springframework/cloud/alibaba/dubbo/bootstrap/DubboSpringCloudConsumerBootstrap.java
@ -0,0 +1,5 @@
|
||||
dubbo:
|
||||
registry:
|
||||
address: spring-cloud://nacos
|
||||
server:
|
||||
port: 7070
|
@ -0,0 +1,74 @@
|
||||
spring:
|
||||
application:
|
||||
name: spring-cloud-alibaba-dubbo-consumer
|
||||
main:
|
||||
allow-bean-definition-overriding: true
|
||||
|
||||
|
||||
# default disable all
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
enabled: false
|
||||
register-enabled: false
|
||||
zookeeper:
|
||||
enabled: false
|
||||
consul:
|
||||
enabled: false
|
||||
|
||||
eureka:
|
||||
client:
|
||||
enabled: false
|
||||
|
||||
ribbon:
|
||||
nacos:
|
||||
enabled: false
|
||||
|
||||
provider:
|
||||
application:
|
||||
name: spring-cloud-alibaba-dubbo-provider
|
||||
|
||||
---
|
||||
spring:
|
||||
profiles: nacos
|
||||
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
enabled: true
|
||||
register-enabled: true
|
||||
server-addr: 127.0.0.1:8848
|
||||
|
||||
ribbon:
|
||||
nacos:
|
||||
enabled: true
|
||||
|
||||
---
|
||||
spring:
|
||||
profiles: eureka
|
||||
|
||||
eureka:
|
||||
client:
|
||||
enabled: true
|
||||
service-url:
|
||||
defaultZone: http://127.0.0.1:9090/eureka/
|
||||
|
||||
|
||||
---
|
||||
spring:
|
||||
profiles: zookeeper
|
||||
cloud:
|
||||
zookeeper:
|
||||
enabled: true
|
||||
connect-string: 127.0.0.1:2181
|
||||
|
||||
|
||||
---
|
||||
spring:
|
||||
profiles: consul
|
||||
|
||||
cloud:
|
||||
consul:
|
||||
enabled: true
|
||||
host: 127.0.0.1
|
||||
port: 8500
|
@ -0,0 +1,65 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>spring-cloud-alibaba-dubbo-examples</artifactId>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<version>0.2.2.BUILD-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dubbo-provider-sample</artifactId>
|
||||
<name>Spring Cloud Dubbo Provider Sample</name>
|
||||
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Sample API -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dubbo-sample-api</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- REST support dependencies -->
|
||||
<dependency>
|
||||
<groupId>org.jboss.resteasy</groupId>
|
||||
<artifactId>resteasy-jaxrs</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jboss.resteasy</groupId>
|
||||
<artifactId>resteasy-client</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jboss.resteasy</groupId>
|
||||
<artifactId>resteasy-netty4</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jboss.resteasy</groupId>
|
||||
<artifactId>resteasy-jackson-provider</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jboss.resteasy</groupId>
|
||||
<artifactId>resteasy-jaxb-provider</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.alibaba.dubbo.bootstrap;
|
||||
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
|
||||
/**
|
||||
* Dubbo Spring Cloud Provider Bootstrap
|
||||
*/
|
||||
@EnableDiscoveryClient
|
||||
@EnableAutoConfiguration
|
||||
public class DubboSpringCloudProviderBootstrap {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder(DubboSpringCloudProviderBootstrap.class)
|
||||
.profiles("nacos")
|
||||
.run(args);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
8
spring-cloud-alibaba-dubbo/src/test/java/org/springframework/cloud/alibaba/dubbo/service/StandardRestService.java → spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-provider-sample/src/main/java/org/springframework/cloud/alibaba/dubbo/service/StandardRestService.java
8
spring-cloud-alibaba-dubbo/src/test/java/org/springframework/cloud/alibaba/dubbo/service/StandardRestService.java → spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-provider-sample/src/main/java/org/springframework/cloud/alibaba/dubbo/service/StandardRestService.java
@ -0,0 +1,20 @@
|
||||
dubbo:
|
||||
scan:
|
||||
base-packages: org.springframework.cloud.alibaba.dubbo.service
|
||||
protocols:
|
||||
dubbo:
|
||||
name: dubbo
|
||||
port: 12345
|
||||
rest:
|
||||
name: rest
|
||||
port: 8081
|
||||
server: netty
|
||||
registry:
|
||||
address: spring-cloud://nacos
|
||||
|
||||
feign:
|
||||
hystrix:
|
||||
enabled: true
|
||||
|
||||
server:
|
||||
port: 8080
|
@ -0,0 +1,63 @@
|
||||
spring:
|
||||
application:
|
||||
name: spring-cloud-alibaba-dubbo-provider
|
||||
main:
|
||||
allow-bean-definition-overriding: true
|
||||
|
||||
# default disable all
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
enabled: false
|
||||
register-enabled: false
|
||||
zookeeper:
|
||||
enabled: false
|
||||
consul:
|
||||
enabled: false
|
||||
|
||||
eureka:
|
||||
client:
|
||||
enabled: false
|
||||
|
||||
|
||||
---
|
||||
spring:
|
||||
profiles: nacos
|
||||
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
enabled: true
|
||||
register-enabled: true
|
||||
server-addr: 127.0.0.1:8848
|
||||
|
||||
|
||||
---
|
||||
spring:
|
||||
profiles: eureka
|
||||
|
||||
eureka:
|
||||
client:
|
||||
enabled: true
|
||||
service-url:
|
||||
defaultZone: http://127.0.0.1:9090/eureka/
|
||||
|
||||
|
||||
---
|
||||
spring:
|
||||
profiles: zookeeper
|
||||
cloud:
|
||||
zookeeper:
|
||||
enabled: true
|
||||
connect-string: 127.0.0.1:2181
|
||||
|
||||
|
||||
---
|
||||
spring:
|
||||
profiles: consul
|
||||
|
||||
cloud:
|
||||
consul:
|
||||
enabled: true
|
||||
host: 127.0.0.1
|
||||
port: 8500
|
@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>spring-cloud-alibaba-dubbo-examples</artifactId>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<version>0.2.2.BUILD-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dubbo-sample-api</artifactId>
|
||||
<name>Spring Cloud Dubbo Sample API</name>
|
||||
|
||||
</project>
|
@ -1,43 +0,0 @@
|
||||
/*
|
||||
* 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.alibaba.nacos;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.cloud.alibaba.nacos.endpoint.NacosConfigEndpoint;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author pbting
|
||||
* @date 2019-01-17 2:25 PM
|
||||
*/
|
||||
public class EndpointTests extends NacosPowerMockitBaseTests {
|
||||
|
||||
@Test
|
||||
public void nacosConfigEndpoint() {
|
||||
|
||||
NacosConfigEndpoint nacosConfigEndpoint = super.context
|
||||
.getBean(NacosConfigEndpoint.class);
|
||||
assertThat(nacosConfigEndpoint != null).isEqualTo(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void endpointInvoke() {
|
||||
NacosConfigEndpoint nacosConfigEndpoint = this.context
|
||||
.getBean(NacosConfigEndpoint.class);
|
||||
assertThat(nacosConfigEndpoint.invoke() != null).isEqualTo(true);
|
||||
}
|
||||
}
|
@ -1,76 +0,0 @@
|
||||
/*
|
||||
* 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.alibaba.nacos;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.cloud.alibaba.nacos.client.NacosPropertySourceLocator;
|
||||
import org.springframework.cloud.alibaba.nacos.refresh.NacosRefreshProperties;
|
||||
import org.springframework.core.env.CompositePropertySource;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
* @author pbting
|
||||
*/
|
||||
public class NacosConfigAutoConfigurationTests extends NacosPowerMockitBaseTests {
|
||||
@Test
|
||||
public void testNacosConfigProperties() {
|
||||
|
||||
NacosConfigProperties nacosConfigProperties = context.getParent()
|
||||
.getBean(NacosConfigProperties.class);
|
||||
assertThat(nacosConfigProperties.getFileExtension()).isEqualTo("properties");
|
||||
assertThat(nacosConfigProperties.getPrefix() == null).isEqualTo(true);
|
||||
assertThat(nacosConfigProperties.getNamespace() == null).isEqualTo(true);
|
||||
assertThat(nacosConfigProperties.getName()).isEqualTo("sca-nacos-config");
|
||||
assertThat(nacosConfigProperties.getServerAddr()).isEqualTo("127.0.0.1:8848");
|
||||
assertThat(nacosConfigProperties.getEncode()).isEqualTo("utf-8");
|
||||
assertThat(nacosConfigProperties.getActiveProfiles())
|
||||
.isEqualTo(new String[] { "develop" });
|
||||
assertThat(nacosConfigProperties.getSharedDataids())
|
||||
.isEqualTo("base-common.properties,common.properties");
|
||||
assertThat(nacosConfigProperties.getRefreshableDataids())
|
||||
.isEqualTo("common.properties");
|
||||
assertThat(nacosConfigProperties.getExtConfig().size()).isEqualTo(3);
|
||||
assertThat(nacosConfigProperties.getExtConfig().get(0).getDataId())
|
||||
.isEqualTo("ext00.yaml");
|
||||
assertThat(nacosConfigProperties.getExtConfig().get(1).getGroup())
|
||||
.isEqualTo("EXT01_GROUP");
|
||||
assertThat(nacosConfigProperties.getExtConfig().get(1).isRefresh())
|
||||
.isEqualTo(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nacosPropertySourceLocator() {
|
||||
NacosPropertySourceLocator nacosPropertySourceLocator = this.context
|
||||
.getBean(NacosPropertySourceLocator.class);
|
||||
PropertySource propertySource = nacosPropertySourceLocator
|
||||
.locate(this.context.getEnvironment());
|
||||
assertThat(propertySource instanceof CompositePropertySource).isEqualTo(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNacosRefreshProperties() {
|
||||
|
||||
NacosRefreshProperties nacosRefreshProperties = this.context
|
||||
.getBean(NacosRefreshProperties.class);
|
||||
assertThat(nacosRefreshProperties.isEnabled()).isEqualTo(true);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,67 +0,0 @@
|
||||
/*
|
||||
* 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.alibaba.nacos;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.actuate.health.Health;
|
||||
import org.springframework.cloud.alibaba.nacos.endpoint.NacosConfigHealthIndicator;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author pbting
|
||||
* @date 2019-01-17 2:58 PM
|
||||
*/
|
||||
public class NacosConfigHealthIndicatorTests extends NacosPowerMockitBaseTests {
|
||||
|
||||
@Test
|
||||
public void nacosConfigHealthIndicatorInstance() {
|
||||
NacosConfigHealthIndicator nacosConfigHealthIndicator = this.context
|
||||
.getBean(NacosConfigHealthIndicator.class);
|
||||
|
||||
assertThat(nacosConfigHealthIndicator != null).isEqualTo(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHealthCheck() {
|
||||
|
||||
NacosConfigHealthIndicator nacosConfigHealthIndicator = this.context
|
||||
.getBean(NacosConfigHealthIndicator.class);
|
||||
|
||||
Health.Builder builder = Health.up();
|
||||
|
||||
Method method = ReflectionUtils.findMethod(NacosConfigHealthIndicator.class,
|
||||
"doHealthCheck", Health.Builder.class);
|
||||
ReflectionUtils.makeAccessible(method);
|
||||
assertThat(method != null).isEqualTo(true);
|
||||
|
||||
try {
|
||||
method.invoke(nacosConfigHealthIndicator, builder);
|
||||
assertThat(builder != null).isEqualTo(true);
|
||||
}
|
||||
catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,157 @@
|
||||
/*
|
||||
* 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.alibaba.nacos;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.nacos.client.config.NacosConfigService;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.api.support.MethodProxy;
|
||||
import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
|
||||
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.alibaba.nacos.client.NacosPropertySourceLocator;
|
||||
import org.springframework.cloud.alibaba.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
|
||||
import org.springframework.cloud.alibaba.nacos.refresh.NacosRefreshHistory;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PowerMockIgnore("javax.management.*")
|
||||
@PowerMockRunnerDelegate(SpringRunner.class)
|
||||
@PrepareForTest({ NacosConfigService.class })
|
||||
@SpringBootTest(classes = NacosConfigurationExtConfigTests.TestConfig.class, properties = {
|
||||
"spring.application.name=myTestService1", "spring.profiles.active=dev,test",
|
||||
"spring.cloud.nacos.config.server-addr=127.0.0.1:8848",
|
||||
"spring.cloud.nacos.config.encode=utf-8",
|
||||
"spring.cloud.nacos.config.timeout=1000",
|
||||
"spring.cloud.nacos.config.file-extension=properties",
|
||||
|
||||
"spring.cloud.nacos.config.ext-config[0].data-id=ext-config-common01.properties",
|
||||
|
||||
"spring.cloud.nacos.config.ext-config[1].data-id=ext-config-common02.properties",
|
||||
"spring.cloud.nacos.config.ext-config[1].group=GLOBAL_GROUP",
|
||||
|
||||
"spring.cloud.nacos.config.shared-dataids=common1.properties,common2.properties",
|
||||
|
||||
"spring.cloud.nacos.config.accessKey=test-accessKey",
|
||||
"spring.cloud.nacos.config.secretKey=test-secretKey" }, webEnvironment = NONE)
|
||||
public class NacosConfigurationExtConfigTests {
|
||||
|
||||
static {
|
||||
|
||||
try {
|
||||
// when(any(ConfigService.class).getConfig(eq("test-name.properties"),
|
||||
// eq("test-group"), any())).thenReturn("user.name=hello");
|
||||
|
||||
Method method = PowerMockito.method(NacosConfigService.class, "getConfig",
|
||||
String.class, String.class, long.class);
|
||||
MethodProxy.proxy(method, new InvocationHandler() {
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args)
|
||||
throws Throwable {
|
||||
|
||||
if ("test-name.properties".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "user.name=hello\nuser.age=12";
|
||||
}
|
||||
|
||||
if ("test-name-dev.properties".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "user.name=dev";
|
||||
}
|
||||
|
||||
if ("ext-config-common01.properties".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "test-ext-config1=config1\ntest-ext-config2=config1";
|
||||
}
|
||||
if ("ext-config-common02.properties".equals(args[0])
|
||||
&& "GLOBAL_GROUP".equals(args[1])) {
|
||||
return "test-ext-config2=config2";
|
||||
}
|
||||
|
||||
if ("common1.properties".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "test-common1=common1\ntest-common2=common1";
|
||||
}
|
||||
|
||||
if ("common2.properties".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "test-common2=common2";
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
ignore.printStackTrace();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Autowired
|
||||
private NacosPropertySourceLocator locator;
|
||||
|
||||
@Autowired
|
||||
private NacosConfigProperties properties;
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
|
||||
assertNotNull("NacosPropertySourceLocator was not created", locator);
|
||||
assertNotNull("NacosConfigProperties was not created", properties);
|
||||
|
||||
Assert.assertEquals(environment.getProperty("test-ext-config1"), "config1");
|
||||
Assert.assertEquals(environment.getProperty("test-ext-config2"), "config2");
|
||||
Assert.assertEquals(environment.getProperty("test-common1"), "common1");
|
||||
Assert.assertEquals(environment.getProperty("test-common2"), "common2");
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ImportAutoConfiguration({ NacosConfigEndpointAutoConfiguration.class,
|
||||
NacosConfigAutoConfiguration.class, NacosConfigBootstrapConfiguration.class })
|
||||
public static class TestConfig {
|
||||
}
|
||||
}
|
@ -0,0 +1,268 @@
|
||||
/*
|
||||
* 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.alibaba.nacos;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.nacos.client.config.NacosConfigService;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.api.support.MethodProxy;
|
||||
import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
|
||||
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.alibaba.nacos.client.NacosPropertySourceLocator;
|
||||
import org.springframework.cloud.alibaba.nacos.endpoint.NacosConfigEndpoint;
|
||||
import org.springframework.cloud.alibaba.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
|
||||
import org.springframework.cloud.alibaba.nacos.refresh.NacosRefreshHistory;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PowerMockIgnore("javax.management.*")
|
||||
@PowerMockRunnerDelegate(SpringRunner.class)
|
||||
@PrepareForTest({ NacosConfigService.class })
|
||||
@SpringBootTest(classes = NacosConfigurationTests.TestConfig.class, properties = {
|
||||
"spring.application.name=myTestService1", "spring.profiles.active=dev,test",
|
||||
"spring.cloud.nacos.config.server-addr=127.0.0.1:8848",
|
||||
"spring.cloud.nacos.config.endpoint=test-endpoint",
|
||||
"spring.cloud.nacos.config.namespace=test-namespace",
|
||||
"spring.cloud.nacos.config.encode=utf-8",
|
||||
"spring.cloud.nacos.config.timeout=1000",
|
||||
"spring.cloud.nacos.config.group=test-group",
|
||||
"spring.cloud.nacos.config.name=test-name",
|
||||
"spring.cloud.nacos.config.cluster-name=test-cluster",
|
||||
"spring.cloud.nacos.config.file-extension=properties",
|
||||
"spring.cloud.nacos.config.contextPath=test-contextpath",
|
||||
|
||||
"spring.cloud.nacos.config.ext-config[0].data-id=ext-config-common01.properties",
|
||||
|
||||
"spring.cloud.nacos.config.ext-config[1].data-id=ext-config-common02.properties",
|
||||
"spring.cloud.nacos.config.ext-config[1].group=GLOBAL_GROUP",
|
||||
|
||||
"spring.cloud.nacos.config.shared-dataids=common1.properties,common2.properties",
|
||||
|
||||
"spring.cloud.nacos.config.accessKey=test-accessKey",
|
||||
"spring.cloud.nacos.config.secretKey=test-secretKey" }, webEnvironment = NONE)
|
||||
public class NacosConfigurationTests {
|
||||
|
||||
static {
|
||||
|
||||
try {
|
||||
// when(any(ConfigService.class).getConfig(eq("test-name.properties"),
|
||||
// eq("test-group"), any())).thenReturn("user.name=hello");
|
||||
|
||||
Method method = PowerMockito.method(NacosConfigService.class, "getConfig",
|
||||
String.class, String.class, long.class);
|
||||
MethodProxy.proxy(method, new InvocationHandler() {
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args)
|
||||
throws Throwable {
|
||||
|
||||
if ("test-name.properties".equals(args[0])
|
||||
&& "test-group".equals(args[1])) {
|
||||
return "user.name=hello\nuser.age=12";
|
||||
}
|
||||
|
||||
if ("test-name-dev.properties".equals(args[0])
|
||||
&& "test-group".equals(args[1])) {
|
||||
return "user.name=dev";
|
||||
}
|
||||
|
||||
if ("ext-config-common01.properties".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "test-ext-config1=config1\ntest-ext-config2=config1";
|
||||
}
|
||||
if ("ext-config-common02.properties".equals(args[0])
|
||||
&& "GLOBAL_GROUP".equals(args[1])) {
|
||||
return "test-ext-config2=config2";
|
||||
}
|
||||
|
||||
if ("common1.properties".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "test-common1=common1\ntest-common2=common1";
|
||||
}
|
||||
|
||||
if ("common2.properties".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "test-common2=common2";
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
ignore.printStackTrace();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Autowired
|
||||
private NacosPropertySourceLocator locator;
|
||||
|
||||
@Autowired
|
||||
private NacosConfigProperties properties;
|
||||
|
||||
@Autowired
|
||||
private NacosRefreshHistory refreshHistory;
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
|
||||
assertNotNull("NacosPropertySourceLocator was not created", locator);
|
||||
assertNotNull("NacosConfigProperties was not created", properties);
|
||||
|
||||
checkoutNacosConfigServerAddr();
|
||||
checkoutNacosConfigEndpoint();
|
||||
checkoutNacosConfigNamespace();
|
||||
checkoutNacosConfigClusterName();
|
||||
checkoutNacosConfigAccessKey();
|
||||
checkoutNacosConfigSecrectKey();
|
||||
checkoutNacosConfigName();
|
||||
checkoutNacosConfigGroup();
|
||||
checkoutNacosConfigContextPath();
|
||||
checkoutNacosConfigFileExtension();
|
||||
checkoutNacosConfigTimeout();
|
||||
checkoutNacosConfigEncode();
|
||||
checkoutNacosConfigProfiles();
|
||||
checkoutNacosConfigExtConfig();
|
||||
|
||||
checkoutEndpoint();
|
||||
|
||||
Assert.assertEquals(environment.getProperty("user.name"), "dev");
|
||||
Assert.assertEquals(environment.getProperty("user.age"), "12");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigServerAddr() {
|
||||
assertEquals("NacosConfigProperties server address is wrong", "127.0.0.1:8848",
|
||||
properties.getServerAddr());
|
||||
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigEndpoint() {
|
||||
assertEquals("NacosConfigProperties endpoint is wrong", "test-endpoint",
|
||||
properties.getEndpoint());
|
||||
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigNamespace() {
|
||||
assertEquals("NacosConfigProperties namespace is wrong", "test-namespace",
|
||||
properties.getNamespace());
|
||||
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigClusterName() {
|
||||
assertEquals("NacosConfigProperties' cluster is wrong", "test-cluster",
|
||||
properties.getClusterName());
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigAccessKey() {
|
||||
assertEquals("NacosConfigProperties' is access key is wrong", "test-accessKey",
|
||||
properties.getAccessKey());
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigSecrectKey() {
|
||||
assertEquals("NacosConfigProperties' is secret key is wrong", "test-secretKey",
|
||||
properties.getSecretKey());
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigContextPath() {
|
||||
assertEquals("NacosConfigProperties' context path is wrong", "test-contextpath",
|
||||
properties.getContextPath());
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigName() {
|
||||
assertEquals("NacosConfigProperties' name is wrong", "test-name",
|
||||
properties.getName());
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigGroup() {
|
||||
assertEquals("NacosConfigProperties' group is wrong", "test-group",
|
||||
properties.getGroup());
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigFileExtension() {
|
||||
assertEquals("NacosConfigProperties' file extension is wrong", "properties",
|
||||
properties.getFileExtension());
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigTimeout() {
|
||||
assertEquals("NacosConfigProperties' timeout is wrong", 1000,
|
||||
properties.getTimeout());
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigEncode() {
|
||||
assertEquals("NacosConfigProperties' encode is wrong", "utf-8",
|
||||
properties.getEncode());
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigExtConfig() {
|
||||
assertEquals("NacosConfigProperties' ext config is wrong",
|
||||
"ext-config-common01.properties",
|
||||
properties.getExtConfig().get(0).getDataId());
|
||||
assertEquals("NacosConfigProperties' ext config is wrong",
|
||||
"ext-config-common02.properties",
|
||||
properties.getExtConfig().get(1).getDataId());
|
||||
assertEquals("NacosConfigProperties' ext config is wrong", "GLOBAL_GROUP",
|
||||
properties.getExtConfig().get(1).getGroup());
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigProfiles() {
|
||||
assertEquals("NacosConfigProperties' profiles is wrong",
|
||||
new String[] { "dev", "test" }, properties.getActiveProfiles());
|
||||
}
|
||||
|
||||
private void checkoutEndpoint() throws Exception {
|
||||
NacosConfigEndpoint nacosConfigEndpoint = new NacosConfigEndpoint(properties,
|
||||
refreshHistory);
|
||||
Map<String, Object> map = nacosConfigEndpoint.invoke();
|
||||
assertEquals(map.get("NacosConfigProperties"), properties);
|
||||
assertEquals(map.get("RefreshHistory"), refreshHistory.getRecords());
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ImportAutoConfiguration({ NacosConfigEndpointAutoConfiguration.class,
|
||||
NacosConfigAutoConfiguration.class, NacosConfigBootstrapConfiguration.class })
|
||||
public static class TestConfig {
|
||||
}
|
||||
}
|
@ -0,0 +1,98 @@
|
||||
/*
|
||||
* 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.alibaba.nacos;
|
||||
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import com.alibaba.nacos.client.config.NacosConfigService;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.api.support.MethodProxy;
|
||||
import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
|
||||
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.alibaba.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PowerMockIgnore("javax.management.*")
|
||||
@PowerMockRunnerDelegate(SpringRunner.class)
|
||||
@PrepareForTest({ NacosConfigService.class })
|
||||
@SpringBootTest(classes = NacosFileExtensionTest.TestConfig.class, properties = {
|
||||
"spring.application.name=test-name",
|
||||
"spring.cloud.nacos.config.server-addr=127.0.0.1:8848",
|
||||
"spring.cloud.nacos.config.file-extension=yaml" }, webEnvironment = NONE)
|
||||
public class NacosFileExtensionTest {
|
||||
|
||||
static {
|
||||
|
||||
try {
|
||||
Method method = PowerMockito.method(NacosConfigService.class, "getConfig",
|
||||
String.class, String.class, long.class);
|
||||
MethodProxy.proxy(method, new InvocationHandler() {
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args)
|
||||
throws Throwable {
|
||||
if ("test-name.yaml".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "user:\n name: hello\n age: 12";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
ignore.printStackTrace();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
|
||||
Assert.assertEquals(environment.getProperty("user.name"), "hello");
|
||||
Assert.assertEquals(environment.getProperty("user.age"), "12");
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ImportAutoConfiguration({ NacosConfigEndpointAutoConfiguration.class,
|
||||
NacosConfigAutoConfiguration.class, NacosConfigBootstrapConfiguration.class })
|
||||
public static class TestConfig {
|
||||
}
|
||||
}
|
@ -1,177 +0,0 @@
|
||||
/*
|
||||
* 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.alibaba.nacos;
|
||||
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.api.support.MethodProxy;
|
||||
import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.alibaba.nacos.client.NacosPropertySource;
|
||||
import org.springframework.cloud.alibaba.nacos.client.NacosPropertySourceBuilder;
|
||||
import org.springframework.cloud.alibaba.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
|
||||
import org.springframework.cloud.context.refresh.ContextRefresher;
|
||||
import org.springframework.cloud.context.scope.refresh.RefreshScope;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @author pbting
|
||||
* @date 2019-01-17 8:54 PM
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PowerMockRunnerDelegate(SpringRunner.class)
|
||||
@PowerMockIgnore({ "javax.management.*", "javax.net.ssl.*" })
|
||||
@PrepareForTest({ NacosPropertySourceBuilder.class })
|
||||
@SpringBootTest(classes = { NacosConfigBootstrapConfiguration.class,
|
||||
NacosConfigEndpointAutoConfiguration.class, NacosConfigAutoConfiguration.class,
|
||||
NacosPowerMockitBaseTests.TestConfiguration.class }, properties = {
|
||||
"spring.application.name=sca-nacos-config",
|
||||
"spring.cloud.nacos.config.server-addr=127.0.0.1:8848",
|
||||
"spring.cloud.nacos.config.name=sca-nacos-config",
|
||||
// "spring.cloud.nacos.config.refresh.enabled=false",
|
||||
"spring.cloud.nacos.config.encode=utf-8",
|
||||
"spring.cloud.nacos.config.shared-data-ids=base-common.properties,common.properties",
|
||||
"spring.cloud.nacos.config.refreshable-dataids=common.properties",
|
||||
"spring.cloud.nacos.config.ext-config[0].data-id=ext00.yaml",
|
||||
"spring.cloud.nacos.config.ext-config[1].data-id=ext01.yml",
|
||||
"spring.cloud.nacos.config.ext-config[1].group=EXT01_GROUP",
|
||||
"spring.cloud.nacos.config.ext-config[1].refresh=true",
|
||||
"spring.cloud.nacos.config.ext-config[2].data-id=ext02.yaml",
|
||||
"spring.profiles.active=develop", "server.port=19090" })
|
||||
public class NacosPowerMockitBaseTests {
|
||||
|
||||
private final static List<String> DATAIDS = Arrays.asList("common.properties",
|
||||
"base-common.properties", "ext00.yaml", "ext01.yml", "ext02.yaml",
|
||||
"sca-nacos-config.properties", "sca-nacos-config-develop.properties");
|
||||
|
||||
private final static HashMap<String, Properties> VALUES = new HashMap<>();
|
||||
|
||||
@Autowired
|
||||
protected ApplicationContext context;
|
||||
|
||||
static {
|
||||
initDataIds();
|
||||
try {
|
||||
final Constructor constructor = ReflectionUtils.accessibleConstructor(
|
||||
NacosPropertySource.class, String.class, String.class, Map.class,
|
||||
Date.class, boolean.class);
|
||||
Method method = PowerMockito.method(NacosPropertySourceBuilder.class, "build",
|
||||
String.class, String.class, String.class, boolean.class);
|
||||
MethodProxy.proxy(method, new InvocationHandler() {
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args)
|
||||
throws Throwable {
|
||||
Properties properties = VALUES.get(args[0].toString());
|
||||
if (properties == null) {
|
||||
properties = new Properties();
|
||||
properties.put("user.name", args[0].toString());
|
||||
}
|
||||
Object instance = constructor.newInstance(args[1].toString(),
|
||||
args[0].toString(), properties, new Date(), args[3]);
|
||||
return instance;
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static void initDataIds() {
|
||||
DATAIDS.forEach(dataId -> {
|
||||
String realpath = "/" + dataId;
|
||||
ClassPathResource classPathResource = new ClassPathResource(realpath);
|
||||
if (realpath.endsWith("properties")) {
|
||||
Properties properties = new Properties();
|
||||
try {
|
||||
properties.load(classPathResource.getInputStream());
|
||||
VALUES.put(dataId, properties);
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
if (realpath.endsWith("yaml") || realpath.endsWith("yml")) {
|
||||
YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
|
||||
yamlFactory.setResources(classPathResource);
|
||||
try {
|
||||
VALUES.put(dataId, yamlFactory.getObject());
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public NacosPropertySourceBuilder nacosPropertySourceBuilderInstance() {
|
||||
NacosConfigProperties nacosConfigProperties = this.context
|
||||
.getBean(NacosConfigProperties.class);
|
||||
|
||||
ConfigService configService = nacosConfigProperties.configServiceInstance();
|
||||
long timeout = nacosConfigProperties.getTimeout();
|
||||
NacosPropertySourceBuilder nacosPropertySourceBuilder = new NacosPropertySourceBuilder(
|
||||
configService, timeout);
|
||||
return nacosPropertySourceBuilder;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@AutoConfigureBefore(NacosConfigAutoConfiguration.class)
|
||||
static class TestConfiguration {
|
||||
|
||||
@Autowired
|
||||
ConfigurableApplicationContext context;
|
||||
|
||||
@Bean
|
||||
ContextRefresher contextRefresher() {
|
||||
RefreshScope refreshScope = new RefreshScope();
|
||||
refreshScope.setApplicationContext(context);
|
||||
return new ContextRefresher(context, refreshScope);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAppContext() {
|
||||
System.err.println(this.context);
|
||||
}
|
||||
}
|
@ -1,190 +0,0 @@
|
||||
/*
|
||||
* 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.alibaba.nacos;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.powermock.api.support.MethodProxy;
|
||||
import org.springframework.cloud.alibaba.nacos.client.NacosPropertySource;
|
||||
import org.springframework.cloud.alibaba.nacos.client.NacosPropertySourceBuilder;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author pbting
|
||||
* @date 2019-01-17 11:49 AM
|
||||
*/
|
||||
public class NacosPropertySourceBuilderTests extends NacosPowerMockitBaseTests {
|
||||
|
||||
@Test
|
||||
public void nacosPropertySourceBuilder() {
|
||||
|
||||
assertThat(nacosPropertySourceBuilderInstance() != null).isEqualTo(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getConfigByProperties() {
|
||||
try {
|
||||
final HashMap<String, String> value = new HashMap<>();
|
||||
value.put("dev.mode", "local-mock");
|
||||
|
||||
final Constructor constructor = ReflectionUtils.accessibleConstructor(
|
||||
NacosPropertySource.class, String.class, String.class, Map.class,
|
||||
Date.class, boolean.class);
|
||||
|
||||
NacosPropertySourceBuilder nacosPropertySourceBuilder = nacosPropertySourceBuilderInstance();
|
||||
|
||||
Method method = ReflectionUtils.findMethod(NacosPropertySourceBuilder.class,
|
||||
"build", String.class, String.class, String.class, boolean.class);
|
||||
ReflectionUtils.makeAccessible(method);
|
||||
assertThat(method != null).isEqualTo(true);
|
||||
MethodProxy.proxy(method, new InvocationHandler() {
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args)
|
||||
throws Throwable {
|
||||
Object instance = constructor.newInstance(args[1].toString(),
|
||||
args[0].toString(), value, new Date(), args[3]);
|
||||
return instance;
|
||||
}
|
||||
});
|
||||
|
||||
Object result = method.invoke(nacosPropertySourceBuilder,
|
||||
"mock-nacos-config.properties", "DEFAULT_GROUP", "properties", true);
|
||||
assertThat(result != null).isEqualTo(true);
|
||||
assertThat(result instanceof NacosPropertySource).isEqualTo(true);
|
||||
NacosPropertySource nacosPropertySource = (NacosPropertySource) result;
|
||||
assertThat(nacosPropertySource.getProperty("dev.mode"))
|
||||
.isEqualTo("local-mock");
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getConfigByYaml() {
|
||||
|
||||
try {
|
||||
//
|
||||
final HashMap<String, String> value = new HashMap<>();
|
||||
value.put("mock-ext-config", "mock-ext-config-value");
|
||||
|
||||
final Constructor constructor = ReflectionUtils.accessibleConstructor(
|
||||
NacosPropertySource.class, String.class, String.class, Map.class,
|
||||
Date.class, boolean.class);
|
||||
|
||||
Method method = ReflectionUtils.findMethod(NacosPropertySourceBuilder.class,
|
||||
"build", String.class, String.class, String.class, boolean.class);
|
||||
ReflectionUtils.makeAccessible(method);
|
||||
assertThat(method != null).isEqualTo(true);
|
||||
|
||||
MethodProxy.proxy(method, new InvocationHandler() {
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args)
|
||||
throws Throwable {
|
||||
Object instance = constructor.newInstance(args[1].toString(),
|
||||
args[0].toString(), value, new Date(), args[3]);
|
||||
return instance;
|
||||
}
|
||||
});
|
||||
|
||||
NacosPropertySourceBuilder nacosPropertySourceBuilder = nacosPropertySourceBuilderInstance();
|
||||
Object result = method.invoke(nacosPropertySourceBuilder, "ext-config.yaml",
|
||||
"DEFAULT_GROUP", "yaml", true);
|
||||
assertThat(result != null).isEqualTo(true);
|
||||
assertThat(result instanceof NacosPropertySource).isEqualTo(true);
|
||||
NacosPropertySource nacosPropertySource = (NacosPropertySource) result;
|
||||
assertThat(nacosPropertySource.getProperty("mock-ext-config"))
|
||||
.isEqualTo("mock-ext-config-value");
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getConfigByYml() {
|
||||
try {
|
||||
//
|
||||
final HashMap<String, String> value = new HashMap<>();
|
||||
value.put("mock-ext-config-yml", "mock-ext-config-yml-value");
|
||||
|
||||
final Constructor constructor = ReflectionUtils.accessibleConstructor(
|
||||
NacosPropertySource.class, String.class, String.class, Map.class,
|
||||
Date.class, boolean.class);
|
||||
|
||||
Method method = ReflectionUtils.findMethod(NacosPropertySourceBuilder.class,
|
||||
"build", String.class, String.class, String.class, boolean.class);
|
||||
ReflectionUtils.makeAccessible(method);
|
||||
assertThat(method != null).isEqualTo(true);
|
||||
|
||||
MethodProxy.proxy(method, new InvocationHandler() {
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args)
|
||||
throws Throwable {
|
||||
Object instance = constructor.newInstance(args[1].toString(),
|
||||
args[0].toString(), value, new Date(), args[3]);
|
||||
return instance;
|
||||
}
|
||||
});
|
||||
|
||||
NacosPropertySourceBuilder nacosPropertySourceBuilder = nacosPropertySourceBuilderInstance();
|
||||
Object result = method.invoke(nacosPropertySourceBuilder, "ext-config.yml",
|
||||
"DEFAULT_GROUP", "yml", true);
|
||||
assertThat(result != null).isEqualTo(true);
|
||||
assertThat(result instanceof NacosPropertySource).isEqualTo(true);
|
||||
NacosPropertySource nacosPropertySource = (NacosPropertySource) result;
|
||||
assertThat(nacosPropertySource.getProperty("mock-ext-config-yml"))
|
||||
.isEqualTo("mock-ext-config-yml-value");
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getEmpty() {
|
||||
NacosPropertySourceBuilder nacosPropertySourceBuilder = nacosPropertySourceBuilderInstance();
|
||||
|
||||
Method method = ReflectionUtils.findMethod(NacosPropertySourceBuilder.class,
|
||||
"build", String.class, String.class, String.class, boolean.class);
|
||||
ReflectionUtils.makeAccessible(method);
|
||||
assertThat(method != null).isEqualTo(true);
|
||||
|
||||
try {
|
||||
Object result = method.invoke(nacosPropertySourceBuilder, "nacos-empty.yml",
|
||||
"DEFAULT_GROUP", "yml", true);
|
||||
assertThat(result != null).isEqualTo(true);
|
||||
assertThat(result instanceof NacosPropertySource).isEqualTo(true);
|
||||
NacosPropertySource nacosPropertySource = (NacosPropertySource) result;
|
||||
assertThat(nacosPropertySource.getProperty("address")).isEqualTo(null);
|
||||
}
|
||||
catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,91 +0,0 @@
|
||||
/*
|
||||
* 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.alibaba.nacos;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author pbting
|
||||
* @date 2019-01-17 11:46 AM
|
||||
*/
|
||||
public class NacosSharedAndExtConfigTests extends NacosPowerMockitBaseTests {
|
||||
private final static Logger log = LoggerFactory
|
||||
.getLogger(NacosSharedAndExtConfigTests.class);
|
||||
|
||||
@Test
|
||||
public void testSharedConfigPriority() {
|
||||
String userName = this.context.getEnvironment().getProperty("user.address");
|
||||
assertThat(userName).isEqualTo("zhejiang-ningbo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSharedConfigRefresh() {
|
||||
|
||||
while (true) {
|
||||
// ContextRefresher contextRefresher = this.context
|
||||
// .getBean(ContextRefresher.class);
|
||||
// contextRefresher.refresh();
|
||||
String userName = this.context.getEnvironment().getProperty("user.address");
|
||||
try {
|
||||
assertThat(userName).isEqualTo("zhejiang-ningbo");
|
||||
TimeUnit.SECONDS.sleep(1);
|
||||
log.info("user name is {}", userName);
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// 真实测试时将这里 注释掉
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExtConfigPriority() {
|
||||
String extKey = this.context.getEnvironment().getProperty("ext.key");
|
||||
assertThat(extKey).isEqualTo("ext.value02");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExtOtherGroup() {
|
||||
String userExt = this.context.getEnvironment().getProperty("user.ext");
|
||||
assertThat(userExt).isEqualTo("EXT01_GROUP-value");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExtRefresh() {
|
||||
while (true) {
|
||||
// ContextRefresher contextRefresher = this.context
|
||||
// .getBean(ContextRefresher.class);
|
||||
// contextRefresher.refresh();
|
||||
String userExt = this.context.getEnvironment().getProperty("user.ext");
|
||||
try {
|
||||
assertThat(userExt).isEqualTo("EXT01_GROUP-value");
|
||||
TimeUnit.SECONDS.sleep(1);
|
||||
log.info("user name is {}", userExt);
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,141 @@
|
||||
/*
|
||||
* 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.alibaba.nacos.endpoint;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.nacos.client.config.NacosConfigService;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.api.support.MethodProxy;
|
||||
import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.actuate.health.Health.Builder;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.alibaba.nacos.NacosConfigAutoConfiguration;
|
||||
import org.springframework.cloud.alibaba.nacos.NacosConfigBootstrapConfiguration;
|
||||
import org.springframework.cloud.alibaba.nacos.NacosConfigProperties;
|
||||
import org.springframework.cloud.alibaba.nacos.refresh.NacosRefreshHistory;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PowerMockIgnore("javax.management.*")
|
||||
@PowerMockRunnerDelegate(SpringRunner.class)
|
||||
@PrepareForTest({ NacosConfigService.class })
|
||||
@SpringBootTest(classes = NacosConfigEndpointTests.TestConfig.class, properties = {
|
||||
"spring.application.name=test-name",
|
||||
"spring.cloud.nacos.config.server-addr=127.0.0.1:8848",
|
||||
"spring.cloud.nacos.config.file-extension=properties" }, webEnvironment = NONE)
|
||||
public class NacosConfigEndpointTests {
|
||||
|
||||
static {
|
||||
|
||||
try {
|
||||
|
||||
Method method = PowerMockito.method(NacosConfigService.class, "getConfig",
|
||||
String.class, String.class, long.class);
|
||||
MethodProxy.proxy(method, new InvocationHandler() {
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args)
|
||||
throws Throwable {
|
||||
|
||||
if ("test-name.properties".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "user.name=hello\nuser.age=12";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
ignore.printStackTrace();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private NacosConfigProperties properties;
|
||||
|
||||
@Autowired
|
||||
private NacosRefreshHistory refreshHistory;
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
|
||||
checkoutEndpoint();
|
||||
checkoutAcmHealthIndicator();
|
||||
|
||||
}
|
||||
|
||||
private void checkoutAcmHealthIndicator() {
|
||||
try {
|
||||
Builder builder = new Builder();
|
||||
|
||||
NacosConfigHealthIndicator healthIndicator = new NacosConfigHealthIndicator(
|
||||
properties, properties.configServiceInstance());
|
||||
healthIndicator.doHealthCheck(builder);
|
||||
|
||||
Builder builder1 = new Builder();
|
||||
List<String> dataIds = new ArrayList<>();
|
||||
dataIds.add("test-name.properties");
|
||||
builder1.up().withDetail("dataIds", dataIds);
|
||||
|
||||
Assert.assertTrue(builder.build().equals(builder1.build()));
|
||||
|
||||
}
|
||||
catch (Exception ignoreE) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void checkoutEndpoint() throws Exception {
|
||||
NacosConfigEndpoint endpoint = new NacosConfigEndpoint(properties,
|
||||
refreshHistory);
|
||||
Map<String, Object> map = endpoint.invoke();
|
||||
assertEquals(map.get("NacosConfigProperties"), properties);
|
||||
assertEquals(map.get("RefreshHistory"), refreshHistory.getRecords());
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ImportAutoConfiguration({ NacosConfigEndpointAutoConfiguration.class,
|
||||
NacosConfigAutoConfiguration.class, NacosConfigBootstrapConfiguration.class })
|
||||
public static class TestConfig {
|
||||
}
|
||||
}
|
@ -1,2 +0,0 @@
|
||||
user.name=base-common-value
|
||||
user.address=zhejiang-hangzhou
|
@ -1,2 +0,0 @@
|
||||
user.name=common-value
|
||||
user.address=zhejiang-ningbo
|
@ -1,4 +0,0 @@
|
||||
user:
|
||||
name: ext-00-value
|
||||
ext:
|
||||
key: ext.value00
|
@ -1,5 +0,0 @@
|
||||
user:
|
||||
name: ext-01-value
|
||||
ext: EXT01_GROUP-value
|
||||
ext:
|
||||
key: ext.value01
|
@ -1,5 +0,0 @@
|
||||
user:
|
||||
name: ext-02-value
|
||||
ext:
|
||||
key: ext.value02
|
||||
app-local-common: update app local shared cguration for Nacos
|
@ -1 +0,0 @@
|
||||
user.name=sca-nacos-config-value-develop
|
@ -1,2 +0,0 @@
|
||||
user.name=sca-nacos-config-value
|
||||
dev.mode=local
|
@ -1,40 +0,0 @@
|
||||
/*
|
||||
* 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.acm.diagnostics.analyzer;
|
||||
|
||||
import org.springframework.boot.diagnostics.AbstractFailureAnalyzer;
|
||||
import org.springframework.boot.diagnostics.FailureAnalysis;
|
||||
|
||||
/**
|
||||
* A {@code FailureAnalyzer} that performs analysis of failures caused by a
|
||||
* {@code DiamondConnectionFailureException}.
|
||||
*
|
||||
* @author juven.xuxb, 07/11/2016.
|
||||
*/
|
||||
public class DiamondConnectionFailureAnalyzer
|
||||
extends AbstractFailureAnalyzer<DiamondConnectionFailureException> {
|
||||
|
||||
@Override
|
||||
protected FailureAnalysis analyze(Throwable rootFailure,
|
||||
DiamondConnectionFailureException cause) {
|
||||
return new FailureAnalysis(
|
||||
"Application failed to connect to Diamond, unable to access http://"
|
||||
+ cause.getDomain() + ":" + cause.getPort()
|
||||
+ "/diamond-server/diamond",
|
||||
"config the right endpoint", cause);
|
||||
}
|
||||
}
|
@ -1,52 +0,0 @@
|
||||
/*
|
||||
* 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.acm.diagnostics.analyzer;
|
||||
|
||||
/**
|
||||
* A {@code DiamondConnectionFailureException} is thrown when the application fails to
|
||||
* connect to Diamond Server.
|
||||
*
|
||||
* @author juven.xuxb, 07/11/2016.
|
||||
*/
|
||||
public class DiamondConnectionFailureException extends RuntimeException {
|
||||
|
||||
private final String domain;
|
||||
|
||||
private final String port;
|
||||
|
||||
public DiamondConnectionFailureException(String domain, String port, String message) {
|
||||
super(message);
|
||||
this.domain = domain;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public DiamondConnectionFailureException(String domain, String port, String message,
|
||||
Throwable cause) {
|
||||
super(message, cause);
|
||||
this.domain = domain;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
String getDomain() {
|
||||
return domain;
|
||||
}
|
||||
|
||||
String getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,195 @@
|
||||
/*
|
||||
* 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.acm;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import com.alibaba.edas.acm.ConfigService;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.api.support.MethodProxy;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
|
||||
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.alicloud.acm.bootstrap.AcmPropertySourceLocator;
|
||||
import org.springframework.cloud.alicloud.acm.endpoint.AcmEndpointAutoConfiguration;
|
||||
import org.springframework.cloud.alicloud.context.acm.AcmContextBootstrapConfiguration;
|
||||
import org.springframework.cloud.alicloud.context.acm.AcmIntegrationProperties;
|
||||
import org.springframework.cloud.alicloud.context.acm.AcmProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PowerMockRunnerDelegate(SpringRunner.class)
|
||||
@PrepareForTest({ ConfigService.class })
|
||||
@SpringBootTest(classes = AcmConfigurationTests.TestConfig.class, properties = {
|
||||
"spring.application.name=test-name", "spring.profiles.active=dev,test",
|
||||
"spring.cloud.alicloud.acm.server-list=127.0.0.1",
|
||||
"spring.cloud.alicloud.acm.server-port=8848",
|
||||
"spring.cloud.alicloud.acm.endpoint=test-endpoint",
|
||||
"spring.cloud.alicloud.acm.namespace=test-namespace",
|
||||
"spring.cloud.alicloud.acm.timeout=1000",
|
||||
"spring.cloud.alicloud.acm.group=test-group",
|
||||
"spring.cloud.alicloud.acm.refresh-enabled=false",
|
||||
"spring.cloud.alicloud.acm.file-extension=properties" }, webEnvironment = NONE)
|
||||
public class AcmConfigurationTests {
|
||||
|
||||
static {
|
||||
|
||||
try {
|
||||
|
||||
Method method = PowerMockito.method(ConfigService.class, "getConfig",
|
||||
String.class, String.class, long.class);
|
||||
MethodProxy.proxy(method, new InvocationHandler() {
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args)
|
||||
throws Throwable {
|
||||
|
||||
if ("test-name.properties".equals(args[0])
|
||||
&& "test-group".equals(args[1])) {
|
||||
return "user.name=hello\nuser.age=12";
|
||||
}
|
||||
|
||||
if ("test-name-dev.properties".equals(args[0])
|
||||
&& "test-group".equals(args[1])) {
|
||||
return "user.name=dev";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
ignore.printStackTrace();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Autowired
|
||||
private AcmPropertySourceLocator locator;
|
||||
|
||||
@Autowired
|
||||
private AcmIntegrationProperties integrationProperties;
|
||||
|
||||
@Autowired
|
||||
private AcmProperties properties;
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
|
||||
assertNotNull("AcmPropertySourceLocator was not created", locator);
|
||||
assertNotNull("AcmProperties was not created", properties);
|
||||
assertNotNull("AcmIntegrationProperties was not created", integrationProperties);
|
||||
|
||||
checkoutAcmServerAddr();
|
||||
checkoutAcmServerPort();
|
||||
checkoutAcmEndpoint();
|
||||
checkoutAcmNamespace();
|
||||
checkoutAcmGroup();
|
||||
checkoutAcmFileExtension();
|
||||
checkoutAcmTimeout();
|
||||
checkoutAcmProfiles();
|
||||
checkoutAcmRefreshEnabled();
|
||||
checkoutDataLoad();
|
||||
checkoutProfileDataLoad();
|
||||
}
|
||||
|
||||
private void checkoutAcmServerAddr() {
|
||||
assertEquals("AcmProperties server address is wrong", "127.0.0.1",
|
||||
properties.getServerList());
|
||||
|
||||
}
|
||||
|
||||
private void checkoutAcmServerPort() {
|
||||
assertEquals("AcmProperties server port is wrong", "8848",
|
||||
properties.getServerPort());
|
||||
|
||||
}
|
||||
|
||||
private void checkoutAcmEndpoint() {
|
||||
assertEquals("AcmProperties endpoint is wrong", "test-endpoint",
|
||||
properties.getEndpoint());
|
||||
|
||||
}
|
||||
|
||||
private void checkoutAcmNamespace() {
|
||||
assertEquals("AcmProperties namespace is wrong", "test-namespace",
|
||||
properties.getNamespace());
|
||||
|
||||
}
|
||||
|
||||
private void checkoutAcmGroup() {
|
||||
assertEquals("AcmProperties' group is wrong", "test-group",
|
||||
properties.getGroup());
|
||||
}
|
||||
|
||||
private void checkoutAcmFileExtension() {
|
||||
assertEquals("AcmProperties' file extension is wrong", "properties",
|
||||
properties.getFileExtension());
|
||||
}
|
||||
|
||||
private void checkoutAcmTimeout() {
|
||||
assertEquals("AcmProperties' timeout is wrong", 1000, properties.getTimeout());
|
||||
}
|
||||
|
||||
private void checkoutAcmRefreshEnabled() {
|
||||
assertEquals("AcmProperties' refresh enabled is wrong", false,
|
||||
properties.isRefreshEnabled());
|
||||
}
|
||||
|
||||
private void checkoutAcmProfiles() {
|
||||
assertArrayEquals("AcmProperties' profiles is wrong",
|
||||
new String[] { "dev", "test" },
|
||||
integrationProperties.getActiveProfiles());
|
||||
}
|
||||
|
||||
private void checkoutDataLoad() {
|
||||
Assert.assertEquals(environment.getProperty("user.age"), "12");
|
||||
}
|
||||
|
||||
private void checkoutProfileDataLoad() {
|
||||
Assert.assertEquals(environment.getProperty("user.name"), "dev");
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ImportAutoConfiguration({ AcmEndpointAutoConfiguration.class,
|
||||
AcmAutoConfiguration.class, AcmContextBootstrapConfiguration.class })
|
||||
public static class TestConfig {
|
||||
}
|
||||
}
|
@ -0,0 +1,98 @@
|
||||
/*
|
||||
* 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.acm;
|
||||
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.api.support.MethodProxy;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
|
||||
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.alicloud.acm.endpoint.AcmEndpointAutoConfiguration;
|
||||
import org.springframework.cloud.alicloud.context.acm.AcmContextBootstrapConfiguration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.alibaba.edas.acm.ConfigService;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PowerMockRunnerDelegate(SpringRunner.class)
|
||||
@PrepareForTest({ ConfigService.class })
|
||||
@SpringBootTest(classes = AcmFileExtensionTest.TestConfig.class, properties = {
|
||||
"spring.application.name=test-name",
|
||||
"spring.cloud.alicloud.acm.server-list=127.0.0.1",
|
||||
"spring.cloud.alicloud.acm.server-port=8080",
|
||||
"spring.cloud.alicloud.acm.file-extension=yaml" }, webEnvironment = NONE)
|
||||
public class AcmFileExtensionTest {
|
||||
|
||||
static {
|
||||
|
||||
try {
|
||||
Method method = PowerMockito.method(ConfigService.class, "getConfig",
|
||||
String.class, String.class, long.class);
|
||||
MethodProxy.proxy(method, new InvocationHandler() {
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args)
|
||||
throws Throwable {
|
||||
if ("test-name.yaml".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "user:\n name: hello\n age: 12";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
ignore.printStackTrace();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
|
||||
Assert.assertEquals(environment.getProperty("user.name"), "hello");
|
||||
Assert.assertEquals(environment.getProperty("user.age"), "12");
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ImportAutoConfiguration({ AcmEndpointAutoConfiguration.class,
|
||||
AcmAutoConfiguration.class, AcmContextBootstrapConfiguration.class })
|
||||
public static class TestConfig {
|
||||
}
|
||||
}
|
@ -0,0 +1,106 @@
|
||||
/*
|
||||
* 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.acm;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import com.alibaba.edas.acm.ConfigService;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.api.support.MethodProxy;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
|
||||
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.alicloud.acm.endpoint.AcmEndpointAutoConfiguration;
|
||||
import org.springframework.cloud.alicloud.context.acm.AcmContextBootstrapConfiguration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PowerMockRunnerDelegate(SpringRunner.class)
|
||||
@PrepareForTest({ ConfigService.class })
|
||||
@SpringBootTest(classes = AcmGroupConfigurationTest.TestConfig.class, properties = {
|
||||
"spring.application.name=test-name", "spring.application.group=com.test.hello",
|
||||
"spring.cloud.alicloud.acm.server-list=127.0.0.1",
|
||||
"spring.cloud.alicloud.acm.server-port=8080",
|
||||
"spring.cloud.alicloud.acm.timeout=1000",
|
||||
"spring.cloud.alicloud.acm.group=test-group" }, webEnvironment = NONE)
|
||||
public class AcmGroupConfigurationTest {
|
||||
|
||||
static {
|
||||
|
||||
try {
|
||||
Method method = PowerMockito.method(ConfigService.class, "getConfig",
|
||||
String.class, String.class, long.class);
|
||||
MethodProxy.proxy(method, new InvocationHandler() {
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args)
|
||||
throws Throwable {
|
||||
if ("com.test:application.properties".equals(args[0])
|
||||
&& "test-group".equals(args[1])) {
|
||||
return "com.test.value=com.test\ntest.priority=1";
|
||||
}
|
||||
if ("com.test.hello:application.properties".equals(args[0])
|
||||
&& "test-group".equals(args[1])) {
|
||||
return "com.test.hello.value=com.test.hello\ntest.priority=2";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
ignore.printStackTrace();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
|
||||
Assert.assertEquals(environment.getProperty("com.test.value"), "com.test");
|
||||
Assert.assertEquals(environment.getProperty("test.priority"), "2");
|
||||
Assert.assertEquals(environment.getProperty("com.test.hello.value"),
|
||||
"com.test.hello");
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ImportAutoConfiguration({ AcmEndpointAutoConfiguration.class,
|
||||
AcmAutoConfiguration.class, AcmContextBootstrapConfiguration.class })
|
||||
public static class TestConfig {
|
||||
}
|
||||
}
|
@ -0,0 +1,148 @@
|
||||
/*
|
||||
* 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.acm.endpoint;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.api.support.MethodProxy;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.actuate.health.Health.Builder;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.alicloud.acm.AcmAutoConfiguration;
|
||||
import org.springframework.cloud.alicloud.acm.AcmPropertySourceRepository;
|
||||
import org.springframework.cloud.alicloud.acm.refresh.AcmRefreshHistory;
|
||||
import org.springframework.cloud.alicloud.context.acm.AcmContextBootstrapConfiguration;
|
||||
import org.springframework.cloud.alicloud.context.acm.AcmProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.alibaba.edas.acm.ConfigService;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PowerMockRunnerDelegate(SpringRunner.class)
|
||||
@PrepareForTest({ ConfigService.class })
|
||||
@SpringBootTest(classes = AcmEndpointTests.TestConfig.class, properties = {
|
||||
"spring.application.name=test-name",
|
||||
"spring.cloud.alicloud.acm.server-list=127.0.0.1",
|
||||
"spring.cloud.alicloud.acm.server-port=8848",
|
||||
"spring.cloud.alicloud.acm.file-extension=properties" }, webEnvironment = NONE)
|
||||
public class AcmEndpointTests {
|
||||
|
||||
static {
|
||||
|
||||
try {
|
||||
|
||||
Method method = PowerMockito.method(ConfigService.class, "getConfig",
|
||||
String.class, String.class, long.class);
|
||||
MethodProxy.proxy(method, new InvocationHandler() {
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args)
|
||||
throws Throwable {
|
||||
|
||||
if ("test-name.properties".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "user.name=hello\nuser.age=12";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
ignore.printStackTrace();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private AcmProperties properties;
|
||||
|
||||
@Autowired
|
||||
private AcmRefreshHistory refreshHistory;
|
||||
|
||||
@Autowired
|
||||
private AcmPropertySourceRepository propertySourceRepository;
|
||||
|
||||
@Autowired
|
||||
private AcmPropertySourceRepository acmPropertySourceRepository;
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
|
||||
checkoutEndpoint();
|
||||
checkoutAcmHealthIndicator();
|
||||
|
||||
}
|
||||
|
||||
private void checkoutAcmHealthIndicator() {
|
||||
try {
|
||||
Builder builder = new Builder();
|
||||
|
||||
AcmHealthIndicator healthIndicator = new AcmHealthIndicator(properties,
|
||||
acmPropertySourceRepository);
|
||||
healthIndicator.doHealthCheck(builder);
|
||||
|
||||
Builder builder1 = new Builder();
|
||||
List<String> dataIds = new ArrayList<>();
|
||||
dataIds.add("test-name.properties");
|
||||
builder1.up().withDetail("dataIds", dataIds);
|
||||
|
||||
Assert.assertTrue(builder.build().equals(builder1.build()));
|
||||
|
||||
}
|
||||
catch (Exception ignoreE) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void checkoutEndpoint() throws Exception {
|
||||
AcmEndpoint acmEndpoint = new AcmEndpoint(properties, refreshHistory,
|
||||
propertySourceRepository);
|
||||
Map<String, Object> map = acmEndpoint.invoke();
|
||||
assertEquals(map.get("config"), properties);
|
||||
assertEquals(((Map) map.get("runtime")).get("refreshHistory"),
|
||||
refreshHistory.getRecords());
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ImportAutoConfiguration({ AcmEndpointAutoConfiguration.class,
|
||||
AcmAutoConfiguration.class, AcmContextBootstrapConfiguration.class })
|
||||
public static class TestConfig {
|
||||
}
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
package org.springframework.cloud.alicloud.ans.migrate;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
||||
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
@Endpoint(id = "migrate")
|
||||
public class MigrateEndpoint {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(MigrateEndpoint.class);
|
||||
|
||||
public MigrateEndpoint() {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ans endpoint
|
||||
*/
|
||||
@ReadOperation
|
||||
public Map<String, ConcurrentMap<String, ServerWrapper>> invoke() {
|
||||
|
||||
Map<String, ConcurrentMap<String, ServerWrapper>> result = ServerListInvocationHandler
|
||||
.getServerRegistry();
|
||||
|
||||
log.info("migrate server list :" + result);
|
||||
return result;
|
||||
}
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package org.springframework.cloud.alicloud.ans.migrate;
|
||||
|
||||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
|
||||
/**
|
||||
* @author pbting
|
||||
*/
|
||||
@ConditionalOnWebApplication
|
||||
@ConditionalOnClass(value = Endpoint.class)
|
||||
@Conditional(MigrateOnConditionClass.class)
|
||||
public class MigrateEndpointAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public MigrateEndpoint ansEndpoint() {
|
||||
return new MigrateEndpoint();
|
||||
}
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
package org.springframework.cloud.alicloud.ans.migrate;
|
||||
|
||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||
import org.springframework.context.annotation.Condition;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* @author pbting
|
||||
*/
|
||||
public abstract class MigrateOnCondition implements Condition, BeanClassLoaderAware {
|
||||
|
||||
final String[] conditionOnClass = new String[] {
|
||||
"org.springframework.cloud.consul.serviceregistry.ConsulAutoServiceRegistration",
|
||||
"org.springframework.cloud.netflix.eureka.serviceregistry.EurekaAutoServiceRegistration" };
|
||||
|
||||
ClassLoader classLoader;
|
||||
|
||||
@Override
|
||||
public void setBeanClassLoader(ClassLoader classLoader) {
|
||||
this.classLoader = classLoader;
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract boolean matches(ConditionContext context,
|
||||
AnnotatedTypeMetadata metadata);
|
||||
|
||||
boolean isPresent(String className, ClassLoader classLoader) {
|
||||
if (classLoader == null) {
|
||||
classLoader = ClassUtils.getDefaultClassLoader();
|
||||
}
|
||||
|
||||
try {
|
||||
forName(className, classLoader);
|
||||
return true;
|
||||
}
|
||||
catch (Throwable throwable) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Class<?> forName(String className, ClassLoader classLoader)
|
||||
throws ClassNotFoundException {
|
||||
return classLoader != null ? classLoader.loadClass(className)
|
||||
: Class.forName(className);
|
||||
}
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
package org.springframework.cloud.alicloud.ans.migrate;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
|
||||
/**
|
||||
* @author pbting
|
||||
*/
|
||||
public class MigrateOnConditionClass extends MigrateOnCondition {
|
||||
|
||||
private static final Logger log = LoggerFactory
|
||||
.getLogger(MigrateOnConditionClass.class);
|
||||
|
||||
@Override
|
||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
boolean result = isPresent(conditionOnClass[0], classLoader)
|
||||
|| isPresent(conditionOnClass[1], classLoader);
|
||||
log.info("the result of matcher is " + result);
|
||||
return result;
|
||||
}
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
package org.springframework.cloud.alicloud.ans.migrate;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
|
||||
/**
|
||||
* @author pbting
|
||||
*/
|
||||
public class MigrateOnConditionMissingClass extends MigrateOnConditionClass {
|
||||
private static final Logger log = LoggerFactory
|
||||
.getLogger(MigrateOnConditionMissingClass.class);
|
||||
|
||||
@Override
|
||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
boolean result = !super.matches(context, metadata);
|
||||
log.info(" the result of matcher is " + result);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
@ -1,97 +0,0 @@
|
||||
package org.springframework.cloud.alicloud.ans.migrate;
|
||||
|
||||
import com.netflix.client.config.IClientConfig;
|
||||
import com.netflix.loadbalancer.ILoadBalancer;
|
||||
import com.netflix.loadbalancer.Server;
|
||||
import com.netflix.loadbalancer.ServerList;
|
||||
import org.aopalliance.aop.Advice;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.aop.AfterReturningAdvice;
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentSkipListSet;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* @author pbting
|
||||
*/
|
||||
final class MigrateProxyManager {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(MigrateProxyManager.class);
|
||||
private final static AtomicBoolean IS_PROXY = new AtomicBoolean(true);
|
||||
|
||||
private final static Set<String> SERVICES_ID = new ConcurrentSkipListSet<>();
|
||||
|
||||
private static Object springProxyFactory(Object target, ClassLoader classLoader,
|
||||
List<Advice> adviceList, Class... interfaces) {
|
||||
final ProxyFactory proxyFactory = new ProxyFactory(interfaces);
|
||||
proxyFactory.setTarget(target);
|
||||
adviceList.forEach(advice -> proxyFactory.addAdvice(advice));
|
||||
return proxyFactory.getProxy(classLoader);
|
||||
}
|
||||
|
||||
static Object newServerListProxy(Object bean, ClassLoader classLoader,
|
||||
IClientConfig clientConfig) {
|
||||
bean = springProxyFactory(bean, classLoader,
|
||||
Arrays.asList(new ServerListInvocationHandler(clientConfig)),
|
||||
new Class[] { ServerList.class });
|
||||
log.info("[service id]" + clientConfig.getClientName()
|
||||
+ " new a ServerList proxy instance for spring cloud netflix to spring cloud alibaba ");
|
||||
collectServiceId(clientConfig.getClientName());
|
||||
return bean;
|
||||
}
|
||||
|
||||
static Object newLoadBalancerProxy(Object bean, ClassLoader classLoader,
|
||||
final IClientConfig clientConfig) {
|
||||
|
||||
bean = springProxyFactory(bean, classLoader,
|
||||
Arrays.asList(new AfterReturningAdvice() {
|
||||
private final IClientConfig iclientConfig = clientConfig;
|
||||
|
||||
@Override
|
||||
public void afterReturning(Object returnValue, Method method,
|
||||
Object[] args, Object target) {
|
||||
String methodName = method.getName();
|
||||
if ("chooseServer".equals(methodName)) {
|
||||
String serviceId = iclientConfig.getClientName();
|
||||
Server server = (Server) returnValue;
|
||||
server = ServerListInvocationHandler
|
||||
.checkAndGetServiceServer(serviceId, server);
|
||||
ServerListInvocationHandler.incrementCallService(serviceId,
|
||||
server);
|
||||
}
|
||||
}
|
||||
}), new Class[] { ILoadBalancer.class });
|
||||
log.info("[service id]" + clientConfig.getClientName()
|
||||
+ " new a ILoadBalancer proxy instance for spring cloud netflix to spring cloud alibaba ");
|
||||
return bean;
|
||||
}
|
||||
|
||||
static void migrateProxyClose() {
|
||||
IS_PROXY.set(false);
|
||||
}
|
||||
|
||||
static void migrateProxyUp() {
|
||||
IS_PROXY.set(true);
|
||||
}
|
||||
|
||||
static boolean isMigrateProxy() {
|
||||
|
||||
return IS_PROXY.get();
|
||||
}
|
||||
|
||||
static void collectServiceId(String serviceId) {
|
||||
SERVICES_ID.add(serviceId);
|
||||
}
|
||||
|
||||
static Set<String> getServicesId() {
|
||||
|
||||
return Collections.unmodifiableSet(SERVICES_ID);
|
||||
}
|
||||
}
|
@ -1,78 +0,0 @@
|
||||
package org.springframework.cloud.alicloud.ans.migrate;
|
||||
|
||||
import com.netflix.loadbalancer.ILoadBalancer;
|
||||
import org.springframework.cloud.context.named.NamedContextFactory;
|
||||
import org.springframework.cloud.endpoint.event.RefreshEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author pbting
|
||||
*/
|
||||
@Component
|
||||
public class MigrateRefreshEventListener implements ApplicationListener<RefreshEvent> {
|
||||
|
||||
private final static int CHECK_INTERVAL = 1;
|
||||
|
||||
private final static String MIGRATE_SWITCH = "spring.cloud.alicloud.migrate.ans.switch";
|
||||
|
||||
private volatile String lastScaMigrateAnsSwitchValue = "true";
|
||||
|
||||
private Environment environment;
|
||||
|
||||
private NamedContextFactory namedContextFactory;
|
||||
|
||||
public MigrateRefreshEventListener(Environment environment,
|
||||
NamedContextFactory namedContextFactory) {
|
||||
this.environment = environment;
|
||||
this.namedContextFactory = namedContextFactory;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void initTimerCheck() {
|
||||
Executors.newSingleThreadScheduledExecutor().scheduleWithFixedDelay(
|
||||
() -> onApplicationEvent(null), CHECK_INTERVAL, CHECK_INTERVAL,
|
||||
TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(RefreshEvent event) {
|
||||
String value = environment.getProperty(MIGRATE_SWITCH, "true");
|
||||
// check 1: check the value
|
||||
if (value.equals(lastScaMigrateAnsSwitchValue)) {
|
||||
return;
|
||||
}
|
||||
|
||||
updateLastScaMigrateAnsResetValue(value);
|
||||
|
||||
// step 1: migrate up
|
||||
if ("true".equals(value)) {
|
||||
MigrateProxyManager.migrateProxyUp();
|
||||
serviceIdContextInit();
|
||||
return;
|
||||
}
|
||||
|
||||
// step 2: migrate close
|
||||
if ("false".equals(value)) {
|
||||
MigrateProxyManager.migrateProxyClose();
|
||||
serviceIdContextInit();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private void serviceIdContextInit() {
|
||||
namedContextFactory.destroy();
|
||||
// initializer each spring context for service id
|
||||
MigrateProxyManager.getServicesId().forEach(serviceId -> namedContextFactory
|
||||
.getInstance(serviceId, ILoadBalancer.class));
|
||||
}
|
||||
|
||||
private synchronized void updateLastScaMigrateAnsResetValue(String value) {
|
||||
this.lastScaMigrateAnsSwitchValue = value;
|
||||
}
|
||||
}
|
@ -1,52 +0,0 @@
|
||||
package org.springframework.cloud.alicloud.ans.migrate;
|
||||
|
||||
import com.netflix.client.config.IClientConfig;
|
||||
import com.netflix.loadbalancer.ILoadBalancer;
|
||||
import com.netflix.loadbalancer.ServerList;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
|
||||
public class MigrateRibbonBeanPostProcessor
|
||||
implements BeanPostProcessor, BeanClassLoaderAware {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(MigrateOnCondition.class);
|
||||
|
||||
private ClassLoader classLoader;
|
||||
private IClientConfig clientConfig;
|
||||
|
||||
public MigrateRibbonBeanPostProcessor(IClientConfig clientConfig) {
|
||||
this.clientConfig = clientConfig;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName)
|
||||
throws BeansException {
|
||||
|
||||
// step 1 : check the bean whether proxy or not
|
||||
if (!MigrateProxyManager.isMigrateProxy()) {
|
||||
log.info("Migrate proxy is Close.");
|
||||
return bean;
|
||||
}
|
||||
|
||||
// step 2 : proxy the designated bean
|
||||
if (bean instanceof ServerList) {
|
||||
bean = MigrateProxyManager.newServerListProxy(bean, classLoader,
|
||||
clientConfig);
|
||||
}
|
||||
|
||||
if (bean instanceof ILoadBalancer) {
|
||||
bean = MigrateProxyManager.newLoadBalancerProxy(bean, classLoader,
|
||||
clientConfig);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBeanClassLoader(ClassLoader classLoader) {
|
||||
this.classLoader = classLoader;
|
||||
}
|
||||
|
||||
}
|
@ -1,52 +0,0 @@
|
||||
package org.springframework.cloud.alicloud.ans.migrate;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.web.context.WebServerInitializedEvent;
|
||||
import org.springframework.cloud.alicloud.ans.registry.AnsRegistration;
|
||||
import org.springframework.cloud.alicloud.ans.registry.AnsServiceRegistry;
|
||||
import org.springframework.cloud.alicloud.context.ans.AnsProperties;
|
||||
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* @author pbting
|
||||
*/
|
||||
@Component
|
||||
public class MigrateServiceRegistry {
|
||||
|
||||
private static final Logger log = LoggerFactory
|
||||
.getLogger(MigrateServiceRegistry.class);
|
||||
|
||||
private AtomicBoolean running = new AtomicBoolean(false);
|
||||
|
||||
private ServiceRegistry serviceRegistry;
|
||||
private AnsRegistration ansRegistration;
|
||||
|
||||
public MigrateServiceRegistry(AnsProperties ansProperties,
|
||||
ApplicationContext context) {
|
||||
this.ansRegistration = new AnsRegistration(ansProperties, context);
|
||||
this.ansRegistration.init();
|
||||
this.serviceRegistry = new AnsServiceRegistry();
|
||||
}
|
||||
|
||||
@EventListener(WebServerInitializedEvent.class)
|
||||
public void onApplicationEvent(WebServerInitializedEvent event) {
|
||||
int serverPort = event.getWebServer().getPort();
|
||||
this.ansRegistration.setPort(serverPort);
|
||||
log.info("[ Migrate ] change the port to " + serverPort);
|
||||
if (!this.running.get()) {
|
||||
long s = System.currentTimeMillis();
|
||||
log.info("[Migrate] start to registry server to ANS");
|
||||
this.serviceRegistry.register(this.ansRegistration);
|
||||
log.info("[migrate] end to registry server to ANS cost time with "
|
||||
+ (System.currentTimeMillis() - s) + " ms.");
|
||||
this.running.set(true);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
package org.springframework.cloud.alicloud.ans.migrate;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.alicloud.ans.ConditionalOnAnsEnabled;
|
||||
import org.springframework.cloud.alicloud.context.ans.AnsProperties;
|
||||
import org.springframework.cloud.context.named.NamedContextFactory;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
/**
|
||||
* @author pbting
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties
|
||||
@Conditional(MigrateOnConditionClass.class)
|
||||
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true)
|
||||
@ConditionalOnAnsEnabled
|
||||
public class MigrationAutoconfiguration {
|
||||
|
||||
@Bean
|
||||
public MigrateServiceRegistry migrationManger(AnsProperties ansProperties,
|
||||
ApplicationContext applicationContext) {
|
||||
|
||||
return new MigrateServiceRegistry(ansProperties, applicationContext);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MigrateRefreshEventListener migrateRefreshEventListener(
|
||||
Environment environment,
|
||||
@Qualifier(value = "springClientFactory") NamedContextFactory namedContextFactory) {
|
||||
|
||||
return new MigrateRefreshEventListener(environment, namedContextFactory);
|
||||
}
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
package org.springframework.cloud.alicloud.ans.migrate;
|
||||
|
||||
import com.netflix.loadbalancer.Server;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
/**
|
||||
* @author pbting
|
||||
*/
|
||||
public class ServerWrapper {
|
||||
|
||||
private Server server;
|
||||
private AtomicLong callCount;
|
||||
|
||||
public ServerWrapper() {
|
||||
}
|
||||
|
||||
public ServerWrapper(Server server, AtomicLong callCount) {
|
||||
this.server = server;
|
||||
this.callCount = callCount;
|
||||
}
|
||||
|
||||
public Server getServer() {
|
||||
return server;
|
||||
}
|
||||
|
||||
public void setServer(Server server) {
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
public AtomicLong getCallCount() {
|
||||
return callCount;
|
||||
}
|
||||
|
||||
public void setCallCount(AtomicLong callCount) {
|
||||
this.callCount = callCount;
|
||||
}
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
package org.springframework.cloud.alicloud.ans.ribbon;
|
||||
|
||||
import com.netflix.client.config.IClientConfig;
|
||||
import org.springframework.cloud.alicloud.ans.migrate.MigrateOnConditionClass;
|
||||
import org.springframework.cloud.alicloud.ans.migrate.MigrateRibbonBeanPostProcessor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@Conditional(MigrateOnConditionClass.class)
|
||||
public class MigrateRibbonCofiguration {
|
||||
|
||||
@Bean
|
||||
public MigrateRibbonBeanPostProcessor migrateBeanPostProcessor(IClientConfig clientConfig) {
|
||||
|
||||
return new MigrateRibbonBeanPostProcessor(clientConfig);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue