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

Polish alibaba/spring-cloud-alibaba/#1222 : Combining the code-based and starter modules

This commit is contained in:
mercyblitz
2020-03-06 13:40:22 +08:00
parent 5f10d99e57
commit 155ea9418f
499 changed files with 1185 additions and 1772 deletions

View File

@@ -0,0 +1,145 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba</artifactId>
<version>2.2.1.BUILD-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-alicloud-context</artifactId>
<name>Spring Cloud AliCloud Context</name>
<dependencies>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-edas</artifactId>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>alicloud-context</artifactId>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba.edas</groupId>
<artifactId>schedulerX-client</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba.ans</groupId>
<artifactId>ans-sdk</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba.edas.acm</groupId>
<artifactId>acm-sdk</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.cloud.commons.util.InetUtilsProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author xiaolongzuo
*/
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties({ AliCloudProperties.class, InetUtilsProperties.class })
public class AliCloudContextAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public InetUtils inetUtils(InetUtilsProperties inetUtilsProperties) {
return new InetUtils(inetUtilsProperties);
}
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context;
import com.alibaba.cloud.context.AliCloudConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @author xiaolongzuo
*/
@ConfigurationProperties("spring.cloud.alicloud")
public class AliCloudProperties implements AliCloudConfiguration {
/**
* alibaba cloud access key.
*/
private String accessKey;
/**
* alibaba cloud secret key.
*/
private String secretKey;
@Override
public String getAccessKey() {
return accessKey;
}
public void setAccessKey(String accessKey) {
this.accessKey = accessKey;
}
@Override
public String getSecretKey() {
return secretKey;
}
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}
}

View File

@@ -0,0 +1,71 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context;
/**
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
public final class Constants {
private Constants() {
throw new AssertionError("Must not instantiate constant utility class");
}
public final class Sentinel {
/**
* prefix of sentinel.
*/
public static final String PROPERTY_PREFIX = "spring.cloud.sentinel";
/**
* nacos access key.
*/
public static final String NACOS_DATASOURCE_AK = PROPERTY_PREFIX
+ ".nacos.config.access-key";
/**
* nacos secret key.
*/
public static final String NACOS_DATASOURCE_SK = PROPERTY_PREFIX
+ ".nacos.config.secret-key";
/**
* nacos namespace.
*/
public static final String NACOS_DATASOURCE_NAMESPACE = PROPERTY_PREFIX
+ ".nacos.config.namespace";
/**
* nacos endpoint.
*/
public static final String NACOS_DATASOURCE_ENDPOINT = PROPERTY_PREFIX
+ ".nacos.config.endpoint";
/**
* nacos project name.
*/
public static final String PROJECT_NAME = PROPERTY_PREFIX
+ ".nacos.config.project-name";
private Sentinel() {
throw new AssertionError("Must not instantiate constant utility class");
}
}
}

View File

@@ -0,0 +1,77 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context.acm;
import javax.annotation.PostConstruct;
import com.alibaba.alicloud.context.AliCloudProperties;
import com.alibaba.alicloud.context.edas.EdasContextAutoConfiguration;
import com.alibaba.alicloud.context.edas.EdasProperties;
import com.alibaba.cloud.context.acm.AliCloudAcmInitializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* @author xiaolongzuo
*/
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(AcmProperties.class)
@ConditionalOnClass(name = "com.alibaba.alicloud.acm.AcmAutoConfiguration")
@ImportAutoConfiguration(EdasContextAutoConfiguration.class)
public class AcmContextBootstrapConfiguration {
@Autowired
private AcmProperties acmProperties;
@Autowired
private EdasProperties edasProperties;
@Autowired
private AliCloudProperties aliCloudProperties;
@Autowired
private Environment environment;
@PostConstruct
public void initAcmProperties() {
AliCloudAcmInitializer.initialize(aliCloudProperties, edasProperties,
acmProperties);
}
@Bean
public AcmIntegrationProperties acmIntegrationProperties() {
AcmIntegrationProperties acmIntegrationProperties = new AcmIntegrationProperties();
String applicationName = environment.getProperty("spring.application.name");
String applicationGroup = environment.getProperty("spring.application.group");
Assert.isTrue(!StringUtils.isEmpty(applicationName),
"'spring.application.name' must be configured in bootstrap.properties or bootstrap.yml/yaml...");
acmIntegrationProperties.setApplicationName(applicationName);
acmIntegrationProperties.setApplicationGroup(applicationGroup);
acmIntegrationProperties.setActiveProfiles(environment.getActiveProfiles());
acmIntegrationProperties.setAcmProperties(acmProperties);
return acmIntegrationProperties;
}
}

View File

@@ -0,0 +1,103 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context.acm;
import java.util.ArrayList;
import java.util.List;
import org.springframework.util.StringUtils;
/**
* @author xiaolongzuo
*/
public class AcmIntegrationProperties {
private String applicationName;
private String applicationGroup;
private String[] activeProfiles = new String[0];
private AcmProperties acmProperties;
public String getApplicationConfigurationDataIdWithoutGroup() {
return applicationName + "." + acmProperties.getFileExtension();
}
public List<String> getGroupConfigurationDataIds() {
List<String> groupConfigurationDataIds = new ArrayList<>();
if (StringUtils.isEmpty(applicationGroup)) {
return groupConfigurationDataIds;
}
String[] parts = applicationGroup.split("\\.");
for (int i = 1; i < parts.length; i++) {
StringBuilder subGroup = new StringBuilder(parts[0]);
for (int j = 1; j <= i; j++) {
subGroup.append(".").append(parts[j]);
}
groupConfigurationDataIds
.add(subGroup + ":application." + acmProperties.getFileExtension());
}
return groupConfigurationDataIds;
}
public List<String> getApplicationConfigurationDataIds() {
List<String> applicationConfigurationDataIds = new ArrayList<>();
if (!StringUtils.isEmpty(applicationGroup)) {
applicationConfigurationDataIds.add(applicationGroup + ":" + applicationName
+ "." + acmProperties.getFileExtension());
for (String profile : activeProfiles) {
applicationConfigurationDataIds
.add(applicationGroup + ":" + applicationName + "-" + profile
+ "." + acmProperties.getFileExtension());
}
}
applicationConfigurationDataIds
.add(applicationName + "." + acmProperties.getFileExtension());
for (String profile : activeProfiles) {
applicationConfigurationDataIds.add(applicationName + "-" + profile + "."
+ acmProperties.getFileExtension());
}
return applicationConfigurationDataIds;
}
public void setApplicationName(String applicationName) {
this.applicationName = applicationName;
}
public void setApplicationGroup(String applicationGroup) {
this.applicationGroup = applicationGroup;
}
public void setActiveProfiles(String[] activeProfiles) {
this.activeProfiles = activeProfiles;
}
public String[] getActiveProfiles() {
return activeProfiles;
}
public void setAcmProperties(AcmProperties acmProperties) {
this.acmProperties = acmProperties;
}
public AcmProperties getAcmProperties() {
return acmProperties;
}
}

View File

@@ -0,0 +1,157 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context.acm;
import com.alibaba.cloud.context.AliCloudServerMode;
import com.alibaba.cloud.context.acm.AcmConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* acm properties.
*
* @author leijuan
* @author xiaolongzuo
*/
@ConfigurationProperties(prefix = "spring.cloud.alicloud.acm")
public class AcmProperties implements AcmConfiguration {
private AliCloudServerMode serverMode = AliCloudServerMode.LOCAL;
private String serverList = "127.0.0.1";
private String serverPort = "8080";
/**
* diamond group.
*/
private String group = "DEFAULT_GROUP";
/**
* timeout to get configuration.
*/
private int timeout = 3000;
/**
* the AliYun endpoint for ACM.
*/
private String endpoint;
/**
* ACM namespace.
*/
private String namespace;
/**
* name of ram role granted to ECS.
*/
private String ramRoleName;
private String fileExtension = "properties";
private boolean refreshEnabled = true;
public String getFileExtension() {
return fileExtension;
}
public void setFileExtension(String fileExtension) {
this.fileExtension = fileExtension;
}
@Override
public String getServerList() {
return serverList;
}
public void setServerList(String serverList) {
this.serverList = serverList;
}
@Override
public String getServerPort() {
return serverPort;
}
public void setServerPort(String serverPort) {
this.serverPort = serverPort;
}
@Override
public boolean isRefreshEnabled() {
return refreshEnabled;
}
public void setRefreshEnabled(boolean refreshEnabled) {
this.refreshEnabled = refreshEnabled;
}
@Override
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
@Override
public int getTimeout() {
return timeout;
}
public void setTimeout(int timeout) {
this.timeout = timeout;
}
@Override
public String getEndpoint() {
return endpoint;
}
public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}
@Override
public String getNamespace() {
return namespace;
}
public void setNamespace(String namespace) {
this.namespace = namespace;
}
@Override
public String getRamRoleName() {
return ramRoleName;
}
public void setRamRoleName(String ramRoleName) {
this.ramRoleName = ramRoleName;
}
@Override
public AliCloudServerMode getServerMode() {
return serverMode;
}
public void setServerMode(AliCloudServerMode serverMode) {
this.serverMode = serverMode;
}
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context.ans;
import com.alibaba.alicloud.context.AliCloudProperties;
import com.alibaba.alicloud.context.edas.EdasProperties;
import com.alibaba.alicloud.context.listener.AbstractOnceApplicationListener;
import com.alibaba.cloud.context.ans.AliCloudAnsInitializer;
import com.alibaba.cloud.context.edas.AliCloudEdasSdk;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.ContextRefreshedEvent;
/**
* Init {@link com.alibaba.ans.core.NamingService} properties.
*
* @author xiaolongzuo
*/
public class AnsContextApplicationListener
extends AbstractOnceApplicationListener<ContextRefreshedEvent> {
@Override
protected String conditionalOnClass() {
return "com.alibaba.alicloud.ans.AnsAutoConfiguration";
}
@Override
public void handleEvent(ContextRefreshedEvent event) {
ApplicationContext applicationContext = event.getApplicationContext();
AliCloudProperties aliCloudProperties = applicationContext
.getBean(AliCloudProperties.class);
EdasProperties edasProperties = applicationContext.getBean(EdasProperties.class);
AnsProperties ansProperties = applicationContext.getBean(AnsProperties.class);
AliCloudEdasSdk aliCloudEdasSdk = applicationContext
.getBean(AliCloudEdasSdk.class);
AliCloudAnsInitializer.initialize(aliCloudProperties, edasProperties,
ansProperties, aliCloudEdasSdk);
}
}

View File

@@ -0,0 +1,35 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context.ans;
import com.alibaba.alicloud.context.edas.EdasContextAutoConfiguration;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* @author xiaolongzuo
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(name = "com.alibaba.alicloud.ans.AnsAutoConfiguration")
@EnableConfigurationProperties(AnsProperties.class)
@ImportAutoConfiguration(EdasContextAutoConfiguration.class)
public class AnsContextAutoConfiguration {
}

View File

@@ -0,0 +1,344 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context.ans;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.PostConstruct;
import com.alibaba.cloud.context.AliCloudServerMode;
import com.alibaba.cloud.context.ans.AnsConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.util.StringUtils;
/**
* @author xiaolongzuo
*/
@ConfigurationProperties("spring.cloud.alicloud.ans")
public class AnsProperties implements AnsConfiguration {
/**
* Server side modethe default is LOCAL.
*/
private AliCloudServerMode serverMode = AliCloudServerMode.LOCAL;
/**
* Server list.
*/
private String serverList = "127.0.0.1";
/**
* Server port.
*/
private String serverPort = "8080";
/**
* Service namesdefault value is ${spring.cloud.alicloud.ans.doms}. When not
* configured, use ${spring.application.name}.
*/
@Value("${spring.cloud.alicloud.ans.client-domains:${spring.application.name:}}")
private String clientDomains;
/**
* The weight of the registration service, obtained from the configuration
* ${spring.cloud.alicloud.ans.weight}, the default is 1.
*/
private float clientWeight = 1;
/**
* When there are multiple doms and need to correspond to different weights, configure
* them by spring.cloud.alicloud.ans.weight.dom1=weight1.
*/
private Map<String, Float> clientWeights = new HashMap<String, Float>();
/**
* The token of the registration service, obtained from
* ${spring.cloud.alicloud.ans.token}.
*/
private String clientToken;
/**
* When there are multiple doms and need to correspond to different tokens, configure
* them by spring.cloud.alicloud.ans.tokens.dom1=token1.
*/
private Map<String, String> clientTokens = new HashMap<String, String>();
/**
* Configure which cluster to register with, obtained from
* ${spring.cloud.alicloud.ans.cluster}, defaults to DEFAULT.
*/
private String clientCluster = "DEFAULT";
/**
* Temporarily not supported, reserved fields.
*/
private Map<String, String> clientMetadata = new HashMap<>();
/**
* Registration is turned on by default, and registration can be turned off by the
* configuration of spring.cloud.alicloud.ans.register-enabled=false.
*/
private boolean registerEnabled = true;
/**
* The ip of the service you want to publish, obtained from
* ${spring.cloud.alicloud.ans.client-ip}.
*/
private String clientIp;
/**
* Configure which NIC the ip of the service you want to publish is obtained from.
*/
private String clientInterfaceName;
/**
* The port of the service you want to publish.
*/
private int clientPort = -1;
/**
* The environment isolation configuration under the tenant, the services in the same
* environment of the same tenant can discover each other.
*/
@Value("${spring.cloud.alicloud.ans.env:${env.id:DEFAULT}}")
private String env;
/**
* Whether to register as https, configured by ${spring.cloud.alicloud.ans.secure},
* default is false.
*/
private boolean secure = false;
@Autowired
private InetUtils inetUtils;
private Map<String, String> tags = new HashMap<>();
@PostConstruct
public void init() throws SocketException {
// Marked as spring cloud application
tags.put("ANS_SERVICE_TYPE", "SPRING_CLOUD");
if (StringUtils.isEmpty(clientIp)) {
if (StringUtils.isEmpty(clientInterfaceName)) {
clientIp = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
}
else {
NetworkInterface networkInterface = NetworkInterface
.getByName(clientInterfaceName);
if (null == networkInterface) {
throw new RuntimeException(
"no such network interface " + clientInterfaceName);
}
Enumeration<InetAddress> inetAddress = networkInterface
.getInetAddresses();
while (inetAddress.hasMoreElements()) {
InetAddress currentAddress = inetAddress.nextElement();
if (currentAddress instanceof Inet4Address
&& !currentAddress.isLoopbackAddress()) {
clientIp = currentAddress.getHostAddress();
break;
}
}
if (StringUtils.isEmpty(clientIp)) {
throw new RuntimeException(
"cannot find available ip from network interface "
+ clientInterfaceName);
}
}
}
}
@Override
public String getServerPort() {
return serverPort;
}
public void setServerPort(String serverPort) {
this.serverPort = serverPort;
}
@Override
public String getServerList() {
return serverList;
}
public void setServerList(String serverList) {
this.serverList = serverList;
}
@Override
public boolean isRegisterEnabled() {
return registerEnabled;
}
public void setRegisterEnabled(boolean registerEnabled) {
this.registerEnabled = registerEnabled;
}
@Override
public boolean isSecure() {
return secure;
}
public void setSecure(boolean secure) {
this.secure = secure;
}
@Override
public String getEnv() {
return env;
}
public void setEnv(String env) {
this.env = env;
}
@Override
public Map<String, String> getTags() {
return tags;
}
public void setTags(Map<String, String> tags) {
this.tags = tags;
}
@Override
public AliCloudServerMode getServerMode() {
return serverMode;
}
public void setServerMode(AliCloudServerMode serverMode) {
this.serverMode = serverMode;
}
@Override
public String getClientDomains() {
return clientDomains;
}
public void setClientDomains(String clientDomains) {
this.clientDomains = clientDomains;
}
@Override
public float getClientWeight() {
return clientWeight;
}
public void setClientWeight(float clientWeight) {
this.clientWeight = clientWeight;
}
@Override
public Map<String, Float> getClientWeights() {
return clientWeights;
}
public void setClientWeights(Map<String, Float> clientWeights) {
this.clientWeights = clientWeights;
}
@Override
public String getClientToken() {
return clientToken;
}
public void setClientToken(String clientToken) {
this.clientToken = clientToken;
}
@Override
public Map<String, String> getClientTokens() {
return clientTokens;
}
public void setClientTokens(Map<String, String> clientTokens) {
this.clientTokens = clientTokens;
}
@Override
public String getClientCluster() {
return clientCluster;
}
public void setClientCluster(String clientCluster) {
this.clientCluster = clientCluster;
}
@Override
public Map<String, String> getClientMetadata() {
return clientMetadata;
}
public void setClientMetadata(Map<String, String> clientMetadata) {
this.clientMetadata = clientMetadata;
}
@Override
public String getClientIp() {
return clientIp;
}
public void setClientIp(String clientIp) {
this.clientIp = clientIp;
}
@Override
public String getClientInterfaceName() {
return clientInterfaceName;
}
public void setClientInterfaceName(String clientInterfaceName) {
this.clientInterfaceName = clientInterfaceName;
}
@Override
public int getClientPort() {
return clientPort;
}
public void setClientPort(int clientPort) {
this.clientPort = clientPort;
}
@Override
public String toString() {
return "AnsProperties{" + "doms='" + clientDomains + '\'' + ", weight="
+ clientWeight + ", weights=" + clientWeights + ", token='" + clientToken
+ '\'' + ", tokens=" + clientTokens + ", cluster='" + clientCluster + '\''
+ ", metadata=" + clientMetadata + ", registerEnabled=" + registerEnabled
+ ", ip='" + clientIp + '\'' + ", interfaceName='" + clientInterfaceName
+ '\'' + ", port=" + clientPort + ", env='" + env + '\'' + ", secure="
+ secure + ", tags=" + tags + '}';
}
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context.edas;
import com.alibaba.alicloud.context.AliCloudContextAutoConfiguration;
import com.alibaba.alicloud.context.AliCloudProperties;
import com.alibaba.cloud.context.edas.AliCloudEdasSdk;
import com.alibaba.cloud.context.edas.AliCloudEdasSdkFactory;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author xiaolongzuo
*/
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(EdasProperties.class)
@ImportAutoConfiguration(AliCloudContextAutoConfiguration.class)
public class EdasContextAutoConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnClass(name = "com.aliyuncs.edas.model.v20170801.GetSecureTokenRequest")
public AliCloudEdasSdk aliCloudEdasSdk(AliCloudProperties aliCloudProperties,
EdasProperties edasProperties) {
return AliCloudEdasSdkFactory.getDefaultAliCloudEdasSdk(aliCloudProperties,
edasProperties.getRegionId());
}
}

View File

@@ -0,0 +1,88 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context.edas;
import com.alibaba.cloud.context.edas.EdasConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @author xiaolongzuo
*/
@ConfigurationProperties("spring.cloud.alicloud.edas")
public class EdasProperties implements EdasConfiguration {
private static final String DEFAULT_APPLICATION_NAME = "";
/**
* edas application name.
*/
@Value("${spring.application.name:${spring.cloud.alicloud.edas.application.name:}}")
private String applicationName;
/**
* edas namespace.
*/
private String namespace;
/**
* whether or not connect edas.
*/
private boolean enabled;
@Override
public String getRegionId() {
if (namespace == null) {
return null;
}
return namespace.contains(":") ? namespace.split(":")[0] : namespace;
}
@Override
public boolean isApplicationNameValid() {
return !DEFAULT_APPLICATION_NAME.equals(applicationName);
}
@Override
public String getApplicationName() {
return applicationName;
}
public void setApplicationName(String applicationName) {
this.applicationName = applicationName;
}
@Override
public String getNamespace() {
return namespace;
}
public void setNamespace(String namespace) {
this.namespace = namespace;
}
@Override
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}

View File

@@ -0,0 +1,83 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context.listener;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ApplicationContextEvent;
/**
* @author xiaolongzuo
*/
public abstract class AbstractOnceApplicationListener<T extends ApplicationEvent>
implements ApplicationListener<T> {
private static final String BOOTSTRAP_CONFIG_NAME_VALUE = "bootstrap";
private static final String BOOTSTRAP_CONFIG_NAME_KEY = "spring.config.name";
private static ConcurrentHashMap<Class<?>, AtomicBoolean> lockMap = new ConcurrentHashMap<>();
@Override
public void onApplicationEvent(T event) {
if (event instanceof ApplicationContextEvent) {
ApplicationContext applicationContext = ((ApplicationContextEvent) event)
.getApplicationContext();
// skip bootstrap context or super parent context.
if (BOOTSTRAP_CONFIG_NAME_VALUE.equals(applicationContext.getEnvironment()
.getProperty(BOOTSTRAP_CONFIG_NAME_KEY))) {
return;
}
}
Class<?> clazz = getClass();
lockMap.putIfAbsent(clazz, new AtomicBoolean(false));
AtomicBoolean handled = lockMap.get(clazz);
// only execute once.
if (!handled.compareAndSet(false, true)) {
return;
}
if (conditionalOnClass() != null) {
try {
Class.forName(conditionalOnClass());
}
catch (ClassNotFoundException e) {
// ignored
return;
}
}
handleEvent(event);
}
/**
* handle event.
* @param event event
*/
protected abstract void handleEvent(T event);
/**
* condition on class.
* @return class name
*/
protected String conditionalOnClass() {
return null;
}
}

View File

@@ -0,0 +1,73 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context.nacos;
import com.alibaba.alicloud.context.listener.AbstractOnceApplicationListener;
import com.alibaba.cloud.context.edas.EdasChangeOrderConfiguration;
import com.alibaba.cloud.context.edas.EdasChangeOrderConfigurationFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
/**
* @author pbting
*/
public class NacosConfigParameterInitListener
extends AbstractOnceApplicationListener<ApplicationEnvironmentPreparedEvent> {
private static final Logger log = LoggerFactory
.getLogger(NacosConfigParameterInitListener.class);
@Override
protected String conditionalOnClass() {
return "com.alibaba.cloud.nacos.NacosConfigAutoConfiguration";
}
@Override
protected void handleEvent(ApplicationEnvironmentPreparedEvent event) {
preparedNacosConfiguration();
}
private void preparedNacosConfiguration() {
EdasChangeOrderConfiguration edasChangeOrderConfiguration = EdasChangeOrderConfigurationFactory
.getEdasChangeOrderConfiguration();
if (log.isDebugEnabled()) {
log.debug("Initialize Nacos Config Parameter ,is managed {}.",
edasChangeOrderConfiguration.isEdasManaged());
}
if (!edasChangeOrderConfiguration.isEdasManaged()) {
return;
}
System.getProperties().setProperty("spring.cloud.nacos.config.server-mode",
"EDAS");
// initialize nacos configuration
System.getProperties().setProperty("spring.cloud.nacos.config.server-addr", "");
System.getProperties().setProperty("spring.cloud.nacos.config.endpoint",
edasChangeOrderConfiguration.getAddressServerDomain());
System.getProperties().setProperty("spring.cloud.nacos.config.namespace",
edasChangeOrderConfiguration.getTenantId());
System.getProperties().setProperty("spring.cloud.nacos.config.access-key",
edasChangeOrderConfiguration.getDauthAccessKey());
System.getProperties().setProperty("spring.cloud.nacos.config.secret-key",
edasChangeOrderConfiguration.getDauthSecretKey());
}
}

View File

@@ -0,0 +1,76 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context.nacos;
import java.util.Properties;
import com.alibaba.alicloud.context.listener.AbstractOnceApplicationListener;
import com.alibaba.cloud.context.edas.EdasChangeOrderConfiguration;
import com.alibaba.cloud.context.edas.EdasChangeOrderConfigurationFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
/**
* @author pbting
* @date 2019-02-14 11:12 AM
*/
public class NacosDiscoveryParameterInitListener
extends AbstractOnceApplicationListener<ApplicationEnvironmentPreparedEvent> {
private static final Logger log = LoggerFactory
.getLogger(NacosDiscoveryParameterInitListener.class);
@Override
protected String conditionalOnClass() {
return "com.alibaba.cloud.nacos.NacosDiscoveryAutoConfiguration";
}
@Override
protected void handleEvent(ApplicationEnvironmentPreparedEvent event) {
EdasChangeOrderConfiguration edasChangeOrderConfiguration = EdasChangeOrderConfigurationFactory
.getEdasChangeOrderConfiguration();
if (log.isDebugEnabled()) {
log.debug("Initialize Nacos Discovery Parameter ,is managed {}.",
edasChangeOrderConfiguration.isEdasManaged());
}
if (!edasChangeOrderConfiguration.isEdasManaged()) {
return;
}
// initialize nacos configuration
Properties properties = System.getProperties();
properties.setProperty("spring.cloud.nacos.discovery.server-mode", "EDAS");
// step 1: set some properties for spring cloud alibaba nacos discovery
properties.setProperty("spring.cloud.nacos.discovery.server-addr", "");
properties.setProperty("spring.cloud.nacos.discovery.endpoint",
edasChangeOrderConfiguration.getAddressServerDomain());
properties.setProperty("spring.cloud.nacos.discovery.namespace",
edasChangeOrderConfiguration.getTenantId());
properties.setProperty("spring.cloud.nacos.discovery.access-key",
edasChangeOrderConfiguration.getDauthAccessKey());
properties.setProperty("spring.cloud.nacos.discovery.secret-key",
edasChangeOrderConfiguration.getDauthSecretKey());
// step 2: set these properties for nacos client
properties.setProperty("nacos.naming.web.context", "/vipserver");
properties.setProperty("nacos.naming.exposed.port", "80");
}
}

View File

@@ -0,0 +1,82 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context.oss;
import com.alibaba.alicloud.context.AliCloudContextAutoConfiguration;
import com.alibaba.alicloud.context.AliCloudProperties;
import com.alibaba.cloud.context.AliCloudAuthorizationMode;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* OSS Auto {@link Configuration}.
*
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
* @author xiaolongzuo
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(name = "com.alibaba.alicloud.oss.OssAutoConfiguration")
@ConditionalOnProperty(name = "spring.cloud.alicloud.oss.enabled", matchIfMissing = true)
@EnableConfigurationProperties(OssProperties.class)
@ImportAutoConfiguration(AliCloudContextAutoConfiguration.class)
public class OssContextAutoConfiguration {
@ConditionalOnMissingBean
@Bean
public OSS ossClient(AliCloudProperties aliCloudProperties,
OssProperties ossProperties) {
if (ossProperties.getAuthorizationMode() == AliCloudAuthorizationMode.AK_SK) {
Assert.isTrue(!StringUtils.isEmpty(ossProperties.getEndpoint()),
"Oss endpoint can't be empty.");
Assert.isTrue(!StringUtils.isEmpty(aliCloudProperties.getAccessKey()),
"${spring.cloud.alicloud.access-key} can't be empty.");
Assert.isTrue(!StringUtils.isEmpty(aliCloudProperties.getSecretKey()),
"${spring.cloud.alicloud.secret-key} can't be empty.");
return new OSSClientBuilder().build(ossProperties.getEndpoint(),
aliCloudProperties.getAccessKey(), aliCloudProperties.getSecretKey(),
ossProperties.getConfig());
}
else if (ossProperties.getAuthorizationMode() == AliCloudAuthorizationMode.STS) {
Assert.isTrue(!StringUtils.isEmpty(ossProperties.getEndpoint()),
"Oss endpoint can't be empty.");
Assert.isTrue(!StringUtils.isEmpty(ossProperties.getSts().getAccessKey()),
"Access key can't be empty.");
Assert.isTrue(!StringUtils.isEmpty(ossProperties.getSts().getSecretKey()),
"Secret key can't be empty.");
Assert.isTrue(!StringUtils.isEmpty(ossProperties.getSts().getSecurityToken()),
"Security Token can't be empty.");
return new OSSClientBuilder().build(ossProperties.getEndpoint(),
ossProperties.getSts().getAccessKey(),
ossProperties.getSts().getSecretKey(),
ossProperties.getSts().getSecurityToken(), ossProperties.getConfig());
}
else {
throw new IllegalArgumentException("Unknown auth mode.");
}
}
}

View File

@@ -0,0 +1,129 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context.oss;
import com.alibaba.cloud.context.AliCloudAuthorizationMode;
import com.aliyun.oss.ClientBuilderConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* {@link ConfigurationProperties} for configuring OSS.
*
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
* @author xiaolongzuo
*/
@ConfigurationProperties("spring.cloud.alicloud.oss")
public class OssProperties {
/**
* Authorization Mode, please see <a href=
* "https://help.aliyun.com/document_detail/32010.html?spm=a2c4g.11186623.6.659.29f145dc3KOwTh">oss
* docs</a>.
*/
@Value("${spring.cloud.alicloud.oss.authorization-mode:AK_SK}")
private AliCloudAuthorizationMode authorizationMode;
/**
* Endpoint, please see <a href=
* "https://help.aliyun.com/document_detail/32010.html?spm=a2c4g.11186623.6.659.29f145dc3KOwTh">oss
* docs</a>.
*/
private String endpoint;
/**
* Sts token, please see <a href=
* "https://help.aliyun.com/document_detail/32010.html?spm=a2c4g.11186623.6.659.29f145dc3KOwTh">oss
* docs</a>.
*/
private StsToken sts;
/**
* Client Configuration, please see <a href=
* "https://help.aliyun.com/document_detail/32010.html?spm=a2c4g.11186623.6.659.29f145dc3KOwTh">oss
* docs</a>.
*/
private ClientBuilderConfiguration config;
public AliCloudAuthorizationMode getAuthorizationMode() {
return authorizationMode;
}
public void setAuthorizationMode(AliCloudAuthorizationMode authorizationMode) {
this.authorizationMode = authorizationMode;
}
public ClientBuilderConfiguration getConfig() {
return config;
}
public void setConfig(ClientBuilderConfiguration config) {
this.config = config;
}
public String getEndpoint() {
return endpoint;
}
public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}
public StsToken getSts() {
return sts;
}
public void setSts(StsToken sts) {
this.sts = sts;
}
public static class StsToken {
private String accessKey;
private String secretKey;
private String securityToken;
public String getAccessKey() {
return accessKey;
}
public void setAccessKey(String accessKey) {
this.accessKey = accessKey;
}
public String getSecretKey() {
return secretKey;
}
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}
public String getSecurityToken() {
return securityToken;
}
public void setSecurityToken(String securityToken) {
this.securityToken = securityToken;
}
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context.scx;
import com.alibaba.alicloud.context.AliCloudProperties;
import com.alibaba.alicloud.context.edas.EdasContextAutoConfiguration;
import com.alibaba.alicloud.context.edas.EdasProperties;
import com.alibaba.cloud.context.edas.AliCloudEdasSdk;
import com.alibaba.cloud.context.scx.AliCloudScxInitializer;
import com.alibaba.edas.schedulerx.SchedulerXClient;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author xiaolongzuo
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(name = "com.alibaba.alicloud.scx.ScxAutoConfiguration")
@ConditionalOnProperty(name = "spring.cloud.alicloud.scx.enabled", matchIfMissing = true)
@EnableConfigurationProperties(ScxProperties.class)
@ImportAutoConfiguration(EdasContextAutoConfiguration.class)
public class ScxContextAutoConfiguration {
@Bean(initMethod = "init")
@ConditionalOnMissingBean
public SchedulerXClient schedulerXClient(AliCloudProperties aliCloudProperties,
EdasProperties edasProperties, ScxProperties scxProperties,
AliCloudEdasSdk aliCloudEdasSdk) {
return AliCloudScxInitializer.initialize(aliCloudProperties, edasProperties,
scxProperties, aliCloudEdasSdk);
}
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context.scx;
import com.alibaba.cloud.context.scx.ScxConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @author xiaolongzuo
*/
@ConfigurationProperties("spring.cloud.alicloud.scx")
public class ScxProperties implements ScxConfiguration {
/**
* Group id, please see <a href=
* "https://help.aliyun.com/document_detail/35359.html?spm=a2c4g.11186623.6.721.69ca5763p9IJly">scx
* docs</a>.
*/
private String groupId;
/**
* Domain name, please see <a href=
* "https://help.aliyun.com/document_detail/35359.html?spm=a2c4g.11186623.6.721.69ca5763p9IJly">scx
* docs</a>.
*/
private String domainName;
@Override
public String getGroupId() {
return groupId;
}
public void setGroupId(String groupId) {
this.groupId = groupId;
}
@Override
public String getDomainName() {
return domainName;
}
public void setDomainName(String domainName) {
this.domainName = domainName;
}
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context.sentinel;
import com.alibaba.alicloud.context.Constants;
import com.alibaba.alicloud.context.listener.AbstractOnceApplicationListener;
import com.alibaba.cloud.context.edas.EdasChangeOrderConfiguration;
import com.alibaba.cloud.context.edas.EdasChangeOrderConfigurationFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
/**
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
public class SentinelAliCloudListener
extends AbstractOnceApplicationListener<ApplicationEnvironmentPreparedEvent> {
private static final Logger logger = LoggerFactory
.getLogger(SentinelAliCloudListener.class);
@Override
protected void handleEvent(ApplicationEnvironmentPreparedEvent event) {
EdasChangeOrderConfiguration edasChangeOrderConfiguration = EdasChangeOrderConfigurationFactory
.getEdasChangeOrderConfiguration();
logger.info("Sentinel Nacos datasource will"
+ (edasChangeOrderConfiguration.isEdasManaged() ? " be " : " not be ")
+ "changed by edas change order.");
if (!edasChangeOrderConfiguration.isEdasManaged()) {
return;
}
System.getProperties().setProperty(Constants.Sentinel.NACOS_DATASOURCE_ENDPOINT,
edasChangeOrderConfiguration.getAddressServerDomain());
System.getProperties().setProperty(Constants.Sentinel.NACOS_DATASOURCE_NAMESPACE,
edasChangeOrderConfiguration.getTenantId());
System.getProperties().setProperty(Constants.Sentinel.NACOS_DATASOURCE_AK,
edasChangeOrderConfiguration.getDauthAccessKey());
System.getProperties().setProperty(Constants.Sentinel.NACOS_DATASOURCE_SK,
edasChangeOrderConfiguration.getDauthSecretKey());
System.getProperties().setProperty(Constants.Sentinel.PROJECT_NAME,
edasChangeOrderConfiguration.getProjectName());
}
@Override
protected String conditionalOnClass() {
return "com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource";
}
}

View File

@@ -0,0 +1,34 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context.sms;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* @author pbting
* @author xiaolongzuo
*/
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(SmsProperties.class)
@ConditionalOnClass(name = "com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest")
@ConditionalOnProperty(name = "spring.cloud.alicloud.sms.enabled", matchIfMissing = true)
public class SmsContextAutoConfiguration {
}

View File

@@ -0,0 +1,90 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context.sms;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @author pbting
* @author xiaolongzuo
*/
@ConfigurationProperties(prefix = "spring.cloud.alicloud.sms")
public class SmsProperties {
/**
* Product name.
*/
public static final String SMS_PRODUCT = "Dysmsapi";
/**
* Product domain.
*/
public static final String SMS_DOMAIN = "dysmsapi.aliyuncs.com";
/**
* Report queue name.
*/
private String reportQueueName;
/**
* Up queue name.
*/
private String upQueueName;
/**
* Connect timeout.
*/
private String connectTimeout = "10000";
/**
* Read timeout.
*/
private String readTimeout = "10000";
public String getConnectTimeout() {
return connectTimeout;
}
public void setConnectTimeout(String connectTimeout) {
this.connectTimeout = connectTimeout;
}
public String getReadTimeout() {
return readTimeout;
}
public void setReadTimeout(String readTimeout) {
this.readTimeout = readTimeout;
}
public String getReportQueueName() {
return reportQueueName;
}
public void setReportQueueName(String reportQueueName) {
this.reportQueueName = reportQueueName;
}
public String getUpQueueName() {
return upQueueName;
}
public void setUpQueueName(String upQueueName) {
this.upQueueName = upQueueName;
}
}

View File

@@ -0,0 +1,122 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context.statistics;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.alicloud.context.acm.AcmContextBootstrapConfiguration;
import com.alibaba.alicloud.context.acm.AcmProperties;
import com.alibaba.alicloud.context.ans.AnsContextAutoConfiguration;
import com.alibaba.alicloud.context.ans.AnsProperties;
import com.alibaba.alicloud.context.edas.EdasProperties;
import com.alibaba.alicloud.context.oss.OssContextAutoConfiguration;
import com.alibaba.alicloud.context.oss.OssProperties;
import com.alibaba.alicloud.context.scx.ScxContextAutoConfiguration;
import com.alibaba.alicloud.context.scx.ScxProperties;
import com.alibaba.cloud.context.AliCloudServerMode;
import com.alibaba.cloud.context.edas.AliCloudEdasSdk;
import com.alibaba.cloud.context.statistics.StatisticsTask;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Configuration;
/**
* @author xiaolongzuo
*/
@Configuration(proxyBeanMethods = false)
@AutoConfigureAfter({ ScxContextAutoConfiguration.class,
OssContextAutoConfiguration.class, AnsContextAutoConfiguration.class,
AcmContextBootstrapConfiguration.class })
public class StatisticsTaskStarter implements InitializingBean {
private static final String NACOS_CONFIG_SERVER_MODE_KEY = "spring.cloud.nacos.config.server-mode";
private static final String NACOS_DISCOVERY_SERVER_MODE_KEY = "spring.cloud.nacos.discovery.server-mode";
private static final String NACOS_SERVER_MODE_VALUE = "EDAS";
@Autowired(required = false)
private AliCloudEdasSdk aliCloudEdasSdk;
@Autowired(required = false)
private EdasProperties edasProperties;
@Autowired(required = false)
private ScxProperties scxProperties;
@Autowired(required = false)
private OssProperties ossProperties;
@Autowired(required = false)
private AnsProperties ansProperties;
@Autowired(required = false)
private AcmProperties acmProperties;
@Autowired(required = false)
private ScxContextAutoConfiguration scxContextAutoConfiguration;
@Autowired(required = false)
private OssContextAutoConfiguration ossContextAutoConfiguration;
@Autowired(required = false)
private AnsContextAutoConfiguration ansContextAutoConfiguration;
@Autowired(required = false)
private AcmContextBootstrapConfiguration acmContextBootstrapConfiguration;
@Override
public void afterPropertiesSet() {
StatisticsTask statisticsTask = new StatisticsTask(aliCloudEdasSdk,
edasProperties, getComponents());
statisticsTask.start();
}
private List<String> getComponents() {
List<String> components = new ArrayList<>();
if (scxContextAutoConfiguration != null && scxProperties != null) {
components.add("SC-SCX");
}
if (ossContextAutoConfiguration != null && ossProperties != null) {
components.add("SC-OSS");
}
boolean edasEnabled = edasProperties != null && edasProperties.isEnabled();
boolean ansEnableEdas = edasEnabled || (ansProperties != null
&& ansProperties.getServerMode() == AliCloudServerMode.EDAS);
if (ansContextAutoConfiguration != null && ansEnableEdas) {
components.add("SC-ANS");
}
boolean acmEnableEdas = edasEnabled || (acmProperties != null
&& acmProperties.getServerMode() == AliCloudServerMode.EDAS);
if (acmContextBootstrapConfiguration != null && acmEnableEdas) {
components.add("SC-ACM");
}
if (NACOS_SERVER_MODE_VALUE
.equals(System.getProperty(NACOS_CONFIG_SERVER_MODE_KEY))) {
components.add("SC-NACOS-CONFIG");
}
if (NACOS_SERVER_MODE_VALUE
.equals(System.getProperty(NACOS_DISCOVERY_SERVER_MODE_KEY))) {
components.add("SC-NACOS-DISCOVERY");
}
return components;
}
}

View File

@@ -0,0 +1,16 @@
{
"properties": [
{
"name": "spring.cloud.alicloud.ans.client-domains",
"type": "java.lang.String",
"defaultValue": "",
"description": "Service name list, default value is ${spring.application.name}."
},
{
"name": "spring.cloud.alicloud.ans.env",
"type": "java.lang.String",
"defaultValue": "DEFAULT",
"description": "The env for ans, default value is DEFAULT."
}
]
}

View File

@@ -0,0 +1,15 @@
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.alibaba.alicloud.context.acm.AcmContextBootstrapConfiguration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.alibaba.alicloud.context.AliCloudContextAutoConfiguration,\
com.alibaba.alicloud.context.edas.EdasContextAutoConfiguration,\
com.alibaba.alicloud.context.ans.AnsContextAutoConfiguration,\
com.alibaba.alicloud.context.oss.OssContextAutoConfiguration,\
com.alibaba.alicloud.context.scx.ScxContextAutoConfiguration,\
com.alibaba.alicloud.context.statistics.StatisticsTaskStarter,\
com.alibaba.alicloud.context.sms.SmsContextAutoConfiguration
org.springframework.context.ApplicationListener=\
com.alibaba.alicloud.context.ans.AnsContextApplicationListener,\
com.alibaba.alicloud.context.nacos.NacosConfigParameterInitListener,\
com.alibaba.alicloud.context.nacos.NacosDiscoveryParameterInitListener,\
com.alibaba.alicloud.context.sentinel.SentinelAliCloudListener

View File

@@ -0,0 +1,24 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.acm;
/**
* @author xiaolongzuo
*/
public class AcmAutoConfiguration {
}

View File

@@ -0,0 +1,24 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.ans;
/**
* @author xiaolongzuo
*/
public class AnsAutoConfiguration {
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
/**
* @author xiaolongzuo
*/
public class AliCloudPropertiesTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(
AutoConfigurations.of(AliCloudContextAutoConfiguration.class));
@Test
public void testConfigurationValueDefaultsAreAsExpected() {
this.contextRunner.run(context -> {
AliCloudProperties aliCloudProperties = context
.getBean(AliCloudProperties.class);
assertThat(aliCloudProperties.getAccessKey()).isNull();
assertThat(aliCloudProperties.getSecretKey()).isNull();
});
}
@Test
public void testConfigurationValuesAreCorrectlyLoaded() {
this.contextRunner.withPropertyValues("spring.cloud.alicloud.access-key=123",
"spring.cloud.alicloud.secret-key=123456").run(context -> {
AliCloudProperties aliCloudProperties = context
.getBean(AliCloudProperties.class);
assertThat(aliCloudProperties.getAccessKey()).isEqualTo("123");
assertThat(aliCloudProperties.getSecretKey()).isEqualTo("123456");
});
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author xiaolongzuo
*/
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringRunner.class)
@PowerMockIgnore("javax.management.*")
@SpringBootTest(classes = BaseAliCloudSpringApplication.AliCloudApplication.class,
properties = { "spring.application.name=myapp",
"spring.cloud.alicloud.edas.application.name=myapp",
"spring.cloud.alicloud.access-key=ak",
"spring.cloud.alicloud.secret-key=sk",
"spring.cloud.alicloud.oss.endpoint=test",
"spring.cloud.alicloud.scx.group-id=1-2-3-4",
"spring.cloud.alicloud.edas.namespace=cn-test",
"spring.cloud.alicloud.ans.server-list=192.168.1.100",
"spring.cloud.alicloud.ans.server-port=8888",
"spring.cloud.alicloud.oss.enabled=false",
"spring.cloud.alicloud.scx.enabled=false" })
public abstract class BaseAliCloudSpringApplication {
@SpringBootApplication
public static class AliCloudApplication {
}
}

View File

@@ -0,0 +1,127 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context.acm;
import com.alibaba.alicloud.context.AliCloudContextAutoConfiguration;
import com.alibaba.alicloud.context.edas.EdasContextAutoConfiguration;
import com.alibaba.cloud.context.AliCloudServerMode;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
/**
* @author xiaolongzuo
*/
public class AcmPropertiesTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(
AutoConfigurations.of(AcmContextBootstrapConfiguration.class,
EdasContextAutoConfiguration.class,
AliCloudContextAutoConfiguration.class));
@Test
public void testConfigurationValueDefaultsAreAsExpected() {
this.contextRunner.withPropertyValues("spring.application.name=myapp")
.run(context -> {
AcmProperties config = context.getBean(AcmProperties.class);
assertThat(config.getServerMode())
.isEqualTo(AliCloudServerMode.LOCAL);
assertThat(config.getServerList()).isEqualTo("127.0.0.1");
assertThat(config.getServerPort()).isEqualTo("8080");
assertThat(config.getEndpoint()).isNull();
assertThat(config.getFileExtension()).isEqualTo("properties");
assertThat(config.getGroup()).isEqualTo("DEFAULT_GROUP");
assertThat(config.getNamespace()).isNull();
assertThat(config.getRamRoleName()).isNull();
assertThat(config.getTimeout()).isEqualTo(3000);
});
}
@Test
public void testConfigurationValuesAreCorrectlyLoaded() {
this.contextRunner.withPropertyValues("spring.application.name=myapp",
"spring.cloud.alicloud.access-key=ak",
"spring.cloud.alicloud.secret-key=sk",
"spring.cloud.alicloud.acm.server-mode=EDAS",
"spring.cloud.alicloud.acm.server-port=11111",
"spring.cloud.alicloud.acm.server-list=10.10.10.10",
"spring.cloud.alicloud.acm.namespace=testNamespace",
"spring.cloud.alicloud.acm.endpoint=testDomain",
"spring.cloud.alicloud.acm.group=testGroup",
"spring.cloud.alicloud.acm.file-extension=yaml").run(context -> {
AcmProperties acmProperties = context.getBean(AcmProperties.class);
assertThat(acmProperties.getServerMode())
.isEqualTo(AliCloudServerMode.EDAS);
assertThat(acmProperties.getServerList()).isEqualTo("10.10.10.10");
assertThat(acmProperties.getServerPort()).isEqualTo("11111");
assertThat(acmProperties.getEndpoint()).isEqualTo("testDomain");
assertThat(acmProperties.getGroup()).isEqualTo("testGroup");
assertThat(acmProperties.getFileExtension()).isEqualTo("yaml");
assertThat(acmProperties.getNamespace()).isEqualTo("testNamespace");
});
}
@Test
public void testAcmIntegrationConfigurationValuesAreCorrectlyLoaded() {
this.contextRunner.withPropertyValues("spring.application.name=myapp",
"spring.application.group=com.alicloud.test",
"spring.cloud.alicloud.access-key=ak",
"spring.cloud.alicloud.secret-key=sk",
"spring.cloud.alicloud.acm.server-mode=EDAS",
"spring.cloud.alicloud.acm.server-port=11111",
"spring.cloud.alicloud.acm.server-list=10.10.10.10",
"spring.cloud.alicloud.acm.namespace=testNamespace",
"spring.cloud.alicloud.acm.endpoint=testDomain",
"spring.cloud.alicloud.acm.group=testGroup",
"spring.cloud.alicloud.acm.file-extension=yaml").run(context -> {
AcmIntegrationProperties acmIntegrationProperties = context
.getBean(AcmIntegrationProperties.class);
assertThat(acmIntegrationProperties.getGroupConfigurationDataIds()
.size()).isEqualTo(2);
assertThat(acmIntegrationProperties
.getApplicationConfigurationDataIds().size()).isEqualTo(2);
});
}
@Test
public void testAcmIntegrationConfigurationValuesAreCorrectlyLoaded2() {
this.contextRunner.withPropertyValues("spring.application.name=myapp",
"spring.application.group=com.alicloud.test",
"spring.profiles.active=profile1,profile2",
"spring.cloud.alicloud.access-key=ak",
"spring.cloud.alicloud.secret-key=sk",
"spring.cloud.alicloud.acm.server-mode=EDAS",
"spring.cloud.alicloud.acm.server-port=11111",
"spring.cloud.alicloud.acm.server-list=10.10.10.10",
"spring.cloud.alicloud.acm.namespace=testNamespace",
"spring.cloud.alicloud.acm.endpoint=testDomain",
"spring.cloud.alicloud.acm.group=testGroup",
"spring.cloud.alicloud.acm.file-extension=yaml").run(context -> {
AcmIntegrationProperties acmIntegrationProperties = context
.getBean(AcmIntegrationProperties.class);
assertThat(acmIntegrationProperties.getGroupConfigurationDataIds()
.size()).isEqualTo(2);
assertThat(acmIntegrationProperties
.getApplicationConfigurationDataIds().size()).isEqualTo(6);
});
}
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context.ans;
import com.alibaba.alicloud.context.BaseAliCloudSpringApplication;
import org.junit.Test;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
/**
* @author xiaolongzuo
*/
public class AnsContextApplicationListenerTests extends BaseAliCloudSpringApplication {
@Test
public void testAnsContextApplicationListenerDefault() {
assertThat(System
.getProperty("com.alibaba.ans.shaded.com.taobao.vipserver.serverlist"))
.isEqualTo("192.168.1.100");
assertThat(System.getProperty("vipserver.server.port")).isEqualTo("8888");
}
}

View File

@@ -0,0 +1,146 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context.ans;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Vector;
import com.alibaba.alicloud.context.AliCloudContextAutoConfiguration;
import com.alibaba.alicloud.context.edas.EdasContextAutoConfiguration;
import com.alibaba.cloud.context.AliCloudServerMode;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
/**
* @author xiaolongzuo
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({ NetworkInterface.class, AnsProperties.class })
public class AnsPropertiesTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(AnsContextAutoConfiguration.class,
EdasContextAutoConfiguration.class,
AliCloudContextAutoConfiguration.class));
@Test
public void testConfigurationValueDefaultsAreAsExpected() {
this.contextRunner.withPropertyValues().run(context -> {
AnsProperties ansProperties = context.getBean(AnsProperties.class);
assertThat(ansProperties.getServerMode()).isEqualTo(AliCloudServerMode.LOCAL);
assertThat(ansProperties.getServerList()).isEqualTo("127.0.0.1");
assertThat(ansProperties.getServerPort()).isEqualTo("8080");
assertThat(ansProperties.getClientDomains()).isEqualTo("");
assertThat(ansProperties.getClientWeight()).isEqualTo(1.0F);
assertThat(ansProperties.getClientWeights().size()).isEqualTo(0);
assertThat(ansProperties.getClientTokens().size()).isEqualTo(0);
assertThat(ansProperties.getClientMetadata().size()).isEqualTo(0);
assertThat(ansProperties.getClientToken()).isNull();
assertThat(ansProperties.getClientCluster()).isEqualTo("DEFAULT");
assertThat(ansProperties.isRegisterEnabled()).isTrue();
assertThat(ansProperties.getClientInterfaceName()).isNull();
assertThat(ansProperties.getClientPort()).isEqualTo(-1);
assertThat(ansProperties.getEnv()).isEqualTo("DEFAULT");
assertThat(ansProperties.isSecure()).isFalse();
assertThat(ansProperties.getTags().size()).isEqualTo(1);
assertThat(ansProperties.getTags().keySet().iterator().next())
.isEqualTo("ANS_SERVICE_TYPE");
assertThat(ansProperties.getTags().get("ANS_SERVICE_TYPE"))
.isEqualTo("SPRING_CLOUD");
});
}
@Test
public void testConfigurationValuesAreCorrectlyLoaded1() {
this.contextRunner
.withPropertyValues("spring.cloud.alicloud.ans.server-mode=EDAS",
"spring.cloud.alicloud.ans.server-port=11111",
"spring.cloud.alicloud.ans.server-list=10.10.10.10",
"spring.cloud.alicloud.ans.client-domains=testDomain",
"spring.cloud.alicloud.ans.client-weight=0.9",
"spring.cloud.alicloud.ans.client-weights.testDomain=0.9")
.run(context -> {
AnsProperties ansProperties = context.getBean(AnsProperties.class);
assertThat(ansProperties.getServerMode())
.isEqualTo(AliCloudServerMode.EDAS);
assertThat(ansProperties.getServerList()).isEqualTo("10.10.10.10");
assertThat(ansProperties.getServerPort()).isEqualTo("11111");
assertThat(ansProperties.getClientDomains()).isEqualTo("testDomain");
assertThat(ansProperties.getClientWeight()).isEqualTo(0.9F);
assertThat(ansProperties.getClientWeights().size()).isEqualTo(1);
assertThat(ansProperties.getClientTokens().size()).isEqualTo(0);
assertThat(ansProperties.getClientMetadata().size()).isEqualTo(0);
assertThat(ansProperties.getClientToken()).isNull();
assertThat(ansProperties.getClientCluster()).isEqualTo("DEFAULT");
assertThat(ansProperties.isRegisterEnabled()).isTrue();
assertThat(ansProperties.getClientInterfaceName()).isNull();
assertThat(ansProperties.getClientPort()).isEqualTo(-1);
assertThat(ansProperties.getEnv()).isEqualTo("DEFAULT");
assertThat(ansProperties.isSecure()).isFalse();
assertThat(ansProperties.getTags().size()).isEqualTo(1);
assertThat(ansProperties.getTags().keySet().iterator().next())
.isEqualTo("ANS_SERVICE_TYPE");
assertThat(ansProperties.getTags().get("ANS_SERVICE_TYPE"))
.isEqualTo("SPRING_CLOUD");
});
}
@Test(expected = RuntimeException.class)
public void testConfigurationValuesAreCorrectlyLoaded2() {
this.contextRunner.withPropertyValues(
"spring.cloud.alicloud.ans.client-interface-name=noneinterfacename")
.run(context -> {
AnsProperties ansProperties = context.getBean(AnsProperties.class);
assertThat(ansProperties.getClientInterfaceName())
.isEqualTo("noneinterfacename");
});
}
// @Test
public void testConfigurationValuesAreCorrectlyLoaded3() throws SocketException {
NetworkInterface networkInterface = PowerMockito.mock(NetworkInterface.class);
Vector<InetAddress> inetAddressList = new Vector<>();
Inet4Address inetAddress = PowerMockito.mock(Inet4Address.class);
PowerMockito.when(inetAddress.getHostAddress()).thenReturn("192.168.1.100");
inetAddressList.add(inetAddress);
PowerMockito.when(networkInterface.getInetAddresses())
.thenReturn(inetAddressList.elements());
PowerMockito.mockStatic(NetworkInterface.class);
PowerMockito.when(NetworkInterface.getByName("eth0"))
.thenReturn(networkInterface);
this.contextRunner
.withPropertyValues(
"spring.cloud.alicloud.ans.client-interface-name=eth0")
.run(context -> {
AnsProperties ansProperties = context.getBean(AnsProperties.class);
assertThat(ansProperties.getClientInterfaceName()).isEqualTo("eth0");
assertThat(ansProperties.getClientIp()).isEqualTo("192.168.1.100");
});
}
}

View File

@@ -0,0 +1,69 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context.edas;
import com.alibaba.alicloud.context.AliCloudContextAutoConfiguration;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
/**
* @author xiaolongzuo
*/
public class EdasPropertiesTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(EdasContextAutoConfiguration.class,
AliCloudContextAutoConfiguration.class));
@Test
public void testConfigurationValueDefaultsAreAsExpected() {
this.contextRunner.withPropertyValues().run(context -> {
EdasProperties edasProperties = context.getBean(EdasProperties.class);
assertThat(edasProperties.getNamespace()).isNull();
assertThat(edasProperties.isApplicationNameValid()).isFalse();
});
}
@Test
public void testConfigurationValuesAreCorrectlyLoaded1() {
this.contextRunner
.withPropertyValues("spring.cloud.alicloud.edas.namespace=testns",
"spring.application.name=myapps")
.run(context -> {
EdasProperties edasProperties = context.getBean(EdasProperties.class);
assertThat(edasProperties.getNamespace()).isEqualTo("testns");
assertThat(edasProperties.getApplicationName()).isEqualTo("myapps");
});
}
@Test
public void testConfigurationValuesAreCorrectlyLoaded2() {
this.contextRunner
.withPropertyValues("spring.cloud.alicloud.edas.namespace=testns",
"spring.cloud.alicloud.edas.application.name=myapps")
.run(context -> {
EdasProperties edasProperties = context.getBean(EdasProperties.class);
assertThat(edasProperties.getNamespace()).isEqualTo("testns");
assertThat(edasProperties.getApplicationName()).isEqualTo("myapps");
});
}
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context.nacos;
import com.alibaba.alicloud.context.BaseAliCloudSpringApplication;
import com.alibaba.alicloud.utils.ChangeOrderUtils;
import com.alibaba.cloud.context.ans.AliCloudAnsInitializer;
import com.alibaba.cloud.context.edas.EdasChangeOrderConfigurationFactory;
import org.junit.BeforeClass;
import org.junit.Test;
import org.powermock.core.classloader.annotations.PrepareForTest;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
/**
* @author xiaolongzuo
*/
@PrepareForTest({ EdasChangeOrderConfigurationFactory.class,
NacosConfigParameterInitListener.class, AliCloudAnsInitializer.class })
public class NacosConfigParameterInitListenerTests extends BaseAliCloudSpringApplication {
@BeforeClass
public static void setUp() {
ChangeOrderUtils.mockChangeOrder();
}
@Test
public void testNacosParameterInitListener() {
assertThat(System.getProperty("spring.cloud.nacos.config.server-mode"))
.isEqualTo("EDAS");
assertThat(System.getProperty("spring.cloud.nacos.config.server-addr"))
.isEqualTo("");
assertThat(System.getProperty("spring.cloud.nacos.config.endpoint"))
.isEqualTo("testDomain");
assertThat(System.getProperty("spring.cloud.nacos.config.namespace"))
.isEqualTo("testTenantId");
assertThat(System.getProperty("spring.cloud.nacos.config.access-key"))
.isEqualTo("testAK");
assertThat(System.getProperty("spring.cloud.nacos.config.secret-key"))
.isEqualTo("testSK");
}
}

View File

@@ -0,0 +1,62 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context.nacos;
import com.alibaba.alicloud.context.BaseAliCloudSpringApplication;
import com.alibaba.alicloud.utils.ChangeOrderUtils;
import com.alibaba.cloud.context.ans.AliCloudAnsInitializer;
import com.alibaba.cloud.context.edas.EdasChangeOrderConfigurationFactory;
import org.junit.BeforeClass;
import org.junit.Test;
import org.powermock.core.classloader.annotations.PrepareForTest;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
/**
* @author xiaolongzuo
*/
@PrepareForTest({ EdasChangeOrderConfigurationFactory.class,
NacosDiscoveryParameterInitListener.class, AliCloudAnsInitializer.class })
public class NacosDiscoveryParameterInitListenerTests
extends BaseAliCloudSpringApplication {
@BeforeClass
public static void setUp() {
ChangeOrderUtils.mockChangeOrder();
}
@Test
public void testNacosParameterInitListener() {
assertThat(System.getProperty("spring.cloud.nacos.discovery.server-mode"))
.isEqualTo("EDAS");
assertThat(System.getProperty("spring.cloud.nacos.discovery.server-addr"))
.isEqualTo("");
assertThat(System.getProperty("spring.cloud.nacos.discovery.endpoint"))
.isEqualTo("testDomain");
assertThat(System.getProperty("spring.cloud.nacos.discovery.namespace"))
.isEqualTo("testTenantId");
assertThat(System.getProperty("spring.cloud.nacos.discovery.access-key"))
.isEqualTo("testAK");
assertThat(System.getProperty("spring.cloud.nacos.discovery.secret-key"))
.isEqualTo("testSK");
assertThat(System.getProperties().getProperty("nacos.naming.web.context"))
.isEqualTo("/vipserver");
assertThat(System.getProperties().getProperty("nacos.naming.exposed.port"))
.isEqualTo("80");
}
}

View File

@@ -0,0 +1,105 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context.oss;
import com.alibaba.alicloud.context.AliCloudProperties;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* {@link OSS} {@link OssProperties} Test.
*
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
public class OssAutoConfigurationTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(OssContextAutoConfiguration.class))
.withPropertyValues("spring.cloud.alicloud.accessKey=your-ak",
"spring.cloud.alicloud.secretKey=your-sk",
"spring.cloud.alicloud.oss.endpoint=http://oss-cn-beijing.aliyuncs.com",
"spring.cloud.alicloud.oss.config.userAgent=alibaba",
"spring.cloud.alicloud.oss.sts.access-key=your-sts-ak",
"spring.cloud.alicloud.oss.sts.secret-key=your-sts-sk",
"spring.cloud.alicloud.oss.sts.security-token=your-sts-token");
@Test
public void testOSSProperties() {
this.contextRunner.run(context -> {
assertThat(context.getBeansOfType(OssProperties.class).size() == 1).isTrue();
AliCloudProperties aliCloudProperties = context
.getBean(AliCloudProperties.class);
OssProperties ossProperties = context.getBean(OssProperties.class);
assertThat(aliCloudProperties.getAccessKey()).isEqualTo("your-ak");
assertThat(aliCloudProperties.getSecretKey()).isEqualTo("your-sk");
assertThat(ossProperties.getEndpoint())
.isEqualTo("http://oss-cn-beijing.aliyuncs.com");
assertThat(ossProperties.getConfig().getUserAgent()).isEqualTo("alibaba");
assertThat(ossProperties.getSts().getAccessKey()).isEqualTo("your-sts-ak");
assertThat(ossProperties.getSts().getSecretKey()).isEqualTo("your-sts-sk");
assertThat(ossProperties.getSts().getSecurityToken())
.isEqualTo("your-sts-token");
});
}
@Test
public void testOSSClient1() {
this.contextRunner.run(context -> {
assertThat(context.getBeansOfType(OSS.class).size() == 1).isTrue();
assertThat(context.getBeanNamesForType(OSS.class)[0]).isEqualTo("ossClient");
OSSClient ossClient = (OSSClient) context.getBean(OSS.class);
assertThat(ossClient.getEndpoint().toString())
.isEqualTo("http://oss-cn-beijing.aliyuncs.com");
assertThat(ossClient.getClientConfiguration().getUserAgent())
.isEqualTo("alibaba");
assertThat(
ossClient.getCredentialsProvider().getCredentials().getAccessKeyId())
.isEqualTo("your-ak");
assertThat(ossClient.getCredentialsProvider().getCredentials()
.getSecretAccessKey()).isEqualTo("your-sk");
});
}
@Test
public void testOSSClient2() {
this.contextRunner
.withPropertyValues("spring.cloud.alicloud.oss.authorization-mode=STS")
.run(context -> {
assertThat(context.getBeansOfType(OSS.class).size() == 1).isTrue();
assertThat(context.getBeanNamesForType(OSS.class)[0])
.isEqualTo("ossClient");
OSSClient ossClient = (OSSClient) context.getBean(OSS.class);
assertThat(ossClient.getEndpoint().toString())
.isEqualTo("http://oss-cn-beijing.aliyuncs.com");
assertThat(ossClient.getClientConfiguration().getUserAgent())
.isEqualTo("alibaba");
assertThat(ossClient.getCredentialsProvider().getCredentials()
.getAccessKeyId()).isEqualTo("your-sts-ak");
assertThat(ossClient.getCredentialsProvider().getCredentials()
.getSecretAccessKey()).isEqualTo("your-sts-sk");
assertThat(ossClient.getCredentialsProvider().getCredentials()
.getSecurityToken()).isEqualTo("your-sts-token");
});
}
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context.scx;
import com.alibaba.alicloud.context.edas.EdasProperties;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author xiaolongzuo
*/
public class ScxAutoConfigurationTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(ScxContextAutoConfiguration.class))
.withPropertyValues("spring.cloud.alicloud.scx.group-id=1-2-3-4")
.withPropertyValues("spring.cloud.alicloud.edas.namespace=cn-test");
@Test
public void testSxcProperties() {
this.contextRunner.run(context -> {
assertThat(context.getBeansOfType(ScxProperties.class).size() == 1).isTrue();
EdasProperties edasProperties = context.getBean(EdasProperties.class);
ScxProperties scxProperties = context.getBean(ScxProperties.class);
assertThat(scxProperties.getGroupId()).isEqualTo("1-2-3-4");
assertThat(edasProperties.getNamespace()).isEqualTo("cn-test");
assertThat(scxProperties.getDomainName()).isNull();
});
}
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context.sentinel;
import com.alibaba.alicloud.context.BaseAliCloudSpringApplication;
import com.alibaba.alicloud.context.Constants;
import com.alibaba.alicloud.utils.ChangeOrderUtils;
import com.alibaba.cloud.context.edas.EdasChangeOrderConfigurationFactory;
import org.junit.BeforeClass;
import org.junit.Test;
import org.powermock.core.classloader.annotations.PrepareForTest;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
/**
* @author xiaolongzuo
*/
@PrepareForTest({ EdasChangeOrderConfigurationFactory.class,
SentinelAliCloudListener.class })
public class SentinelAliCloudListenerTests extends BaseAliCloudSpringApplication {
@BeforeClass
public static void setUp() {
ChangeOrderUtils.mockChangeOrder();
}
@Test
public void testNacosParameterInitListener() {
assertThat(System.getProperty(Constants.Sentinel.NACOS_DATASOURCE_ENDPOINT))
.isEqualTo("testDomain");
assertThat(System.getProperty(Constants.Sentinel.PROJECT_NAME))
.isEqualTo("testProjectName");
assertThat(System.getProperty(Constants.Sentinel.NACOS_DATASOURCE_NAMESPACE))
.isEqualTo("testTenantId");
assertThat(System.getProperty(Constants.Sentinel.NACOS_DATASOURCE_AK))
.isEqualTo("testAK");
assertThat(System.getProperty(Constants.Sentinel.NACOS_DATASOURCE_SK))
.isEqualTo("testSK");
}
}

View File

@@ -0,0 +1,65 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.context.sms;
import com.alibaba.alicloud.context.AliCloudContextAutoConfiguration;
import com.alibaba.alicloud.context.edas.EdasContextAutoConfiguration;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
/**
* @author xiaolongzuo
*/
public class SmsPropertiesTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(SmsContextAutoConfiguration.class,
EdasContextAutoConfiguration.class,
AliCloudContextAutoConfiguration.class));
@Test
public void testConfigurationValueDefaultsAreAsExpected() {
this.contextRunner.run(context -> {
SmsProperties config = context.getBean(SmsProperties.class);
assertThat(config.getReportQueueName()).isNull();
assertThat(config.getUpQueueName()).isNull();
assertThat(config.getConnectTimeout()).isEqualTo("10000");
assertThat(config.getReadTimeout()).isEqualTo("10000");
});
}
@Test
public void testConfigurationValuesAreCorrectlyLoaded() {
this.contextRunner
.withPropertyValues("spring.cloud.alicloud.sms.reportQueueName=q1",
"spring.cloud.alicloud.sms.upQueueName=q2",
"spring.cloud.alicloud.sms.connect-timeout=20",
"spring.cloud.alicloud.sms.read-timeout=30")
.run(context -> {
SmsProperties config = context.getBean(SmsProperties.class);
assertThat(config.getReportQueueName()).isEqualTo("q1");
assertThat(config.getUpQueueName()).isEqualTo("q2");
assertThat(config.getConnectTimeout()).isEqualTo("20");
assertThat(config.getReadTimeout()).isEqualTo("30");
});
}
}

View File

@@ -0,0 +1,24 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.oss;
/**
* @author xiaolongzuo
*/
public class OssAutoConfiguration {
}

View File

@@ -0,0 +1,24 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.scx;
/**
* @author xiaolongzuo
*/
public class ScxAutoConfiguration {
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.alicloud.utils;
import com.alibaba.cloud.context.edas.EdasChangeOrderConfiguration;
import com.alibaba.cloud.context.edas.EdasChangeOrderConfigurationFactory;
import org.powermock.api.mockito.PowerMockito;
/**
* @author xiaolongzuo
*/
public final class ChangeOrderUtils {
private ChangeOrderUtils() {
}
public static void mockChangeOrder() {
EdasChangeOrderConfiguration edasChangeOrderConfiguration = PowerMockito
.mock(EdasChangeOrderConfiguration.class);
PowerMockito.when(edasChangeOrderConfiguration.isEdasManaged()).thenReturn(true);
PowerMockito.when(edasChangeOrderConfiguration.getAddressServerDomain())
.thenReturn("testDomain");
PowerMockito.when(edasChangeOrderConfiguration.getTenantId())
.thenReturn("testTenantId");
PowerMockito.when(edasChangeOrderConfiguration.getDauthAccessKey())
.thenReturn("testAK");
PowerMockito.when(edasChangeOrderConfiguration.getDauthSecretKey())
.thenReturn("testSK");
PowerMockito.when(edasChangeOrderConfiguration.getProjectName())
.thenReturn("testProjectName");
PowerMockito.when(edasChangeOrderConfiguration.getAddressServerPort())
.thenReturn("8080");
PowerMockito.mockStatic(EdasChangeOrderConfigurationFactory.class);
PowerMockito
.when(EdasChangeOrderConfigurationFactory
.getEdasChangeOrderConfiguration())
.thenReturn(edasChangeOrderConfiguration);
}
}

View File

@@ -0,0 +1,24 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.cloud.nacos;
/**
* @author xiaolongzuo
*/
public class NacosConfigAutoConfiguration {
}

View File

@@ -0,0 +1,24 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.cloud.nacos;
/**
* @author xiaolongzuo
*/
public class NacosDiscoveryAutoConfiguration {
}

View File

@@ -0,0 +1,24 @@
/*
* Copyright 2013-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
*
* https://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 com.alibaba.csp.sentinel.datasource.nacos;
/**
* @author xiaolongzuo
*/
public class NacosDataSource {
}

View File

@@ -0,0 +1,24 @@
/*
* Copyright 2013-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
*
* https://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 com.aliyuncs.dysmsapi.model.v20170525;
/**
* @author xiaolongzuo
*/
public class SendSmsRequest {
}