Add spring-cloud-alicloud-context module.

pull/70/head
chenzhu.zxl 6 years ago
parent 5d82ab1c5a
commit 6503f8d2cf

@ -81,6 +81,7 @@
<module>spring-cloud-starter-alibaba</module>
<module>spring-cloud-starter-alicloud</module>
<module>spring-cloud-alicloud-oss</module>
<module>spring-cloud-alicloud-context</module>
</modules>
<dependencyManagement>

@ -0,0 +1,99 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba</artifactId>
<version>0.2.0.BUILD-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.cloud</groupId>
<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.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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<scope>provided</scope>
<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>
</dependencies>
</project>

@ -0,0 +1,29 @@
/*
* Copyright (C) 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.alicloud.context;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* @author xiaolongzuo
*/
@Configuration
@EnableConfigurationProperties(AliCloudProperties.class)
public class AliCloudContextAutoConfiguration {
}

@ -0,0 +1,57 @@
/*
* Copyright (C) 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.alicloud.context;
import org.springframework.boot.context.properties.ConfigurationProperties;
import com.alibaba.cloud.context.AliCloudConfiguration;
/**
* @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;
}
}

@ -0,0 +1,56 @@
/*
* Copyright (C) 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.alicloud.context.acm;
import javax.annotation.PostConstruct;
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.cloud.alicloud.context.AliCloudProperties;
import org.springframework.cloud.alicloud.context.edas.EdasContextAutoConfiguration;
import org.springframework.cloud.alicloud.context.edas.EdasProperties;
import org.springframework.context.annotation.Configuration;
import com.alibaba.cloud.context.acm.AliCloudAcmInitializer;
/**
* @author xiaolongzuo
*/
@Configuration
@EnableConfigurationProperties(AcmProperties.class)
@ConditionalOnClass(name = "org.springframework.cloud.alicloud.acm.AcmAutoConfiguration")
@ImportAutoConfiguration(EdasContextAutoConfiguration.class)
public class AcmContextBootstrapConfiguration {
@Autowired
private AcmProperties acmProperties;
@Autowired
private EdasProperties edasProperties;
@Autowired
private AliCloudProperties aliCloudProperties;
@PostConstruct
public void initAcmProperties() {
AliCloudAcmInitializer.initialize(aliCloudProperties, edasProperties,
acmProperties);
}
}

@ -0,0 +1,158 @@
/*
* Copyright (C) 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.alicloud.context.acm;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import com.alibaba.cloud.context.AliCloudServerMode;
import com.alibaba.cloud.context.acm.AcmConfiguration;
/**
* acm properties
*
* @author leijuan
* @author xiaolongzuo
*/
@ConfigurationProperties(prefix = "spring.cloud.alicloud.acm")
public class AcmProperties implements AcmConfiguration {
@Value("${spring.cloud.alicloud.acm.server-mode:LOCAL}")
private AliCloudServerMode serverMode;
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 endpoint2 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;
}
}

@ -0,0 +1,54 @@
/*
* Copyright (C) 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.alicloud.context.ans;
import org.springframework.cloud.alicloud.context.AliCloudProperties;
import org.springframework.cloud.alicloud.context.edas.EdasProperties;
import org.springframework.cloud.alicloud.context.listener.AbstractOnceApplicationListener;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.ContextRefreshedEvent;
import com.alibaba.cloud.context.ans.AliCloudAnsInitializer;
import com.alibaba.cloud.context.edas.AliCloudEdasSdk;
/**
* Init {@link com.alibaba.ans.core.NamingService} properties.
*
* @author xiaolongzuo
*/
public class AnsContextApplicationListener
extends AbstractOnceApplicationListener<ContextRefreshedEvent> {
@Override
protected String conditionalOnClass() {
return "org.springframework.cloud.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);
}
}

@ -0,0 +1,44 @@
/*
* Copyright (C) 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.alicloud.context.ans;
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.cloud.alicloud.context.edas.EdasContextAutoConfiguration;
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
@ConditionalOnClass(name = "org.springframework.cloud.alicloud.ans.AnsAutoConfiguration")
@EnableConfigurationProperties({ AnsProperties.class, InetUtilsProperties.class })
@ImportAutoConfiguration(EdasContextAutoConfiguration.class)
public class AnsContextAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public InetUtils inetUtils(InetUtilsProperties inetUtilsProperties) {
return new InetUtils(inetUtilsProperties);
}
}

@ -0,0 +1,337 @@
/*
* Copyright (C) 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.alicloud.context.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 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;
import com.alibaba.cloud.context.AliCloudServerMode;
import com.alibaba.cloud.context.ans.AnsConfiguration;
/**
* @author xiaolongzuo
*/
@ConfigurationProperties("spring.cloud.alicloud.ans")
public class AnsProperties implements AnsConfiguration {
/**
* LOCAL
*/
@Value("${spring.cloud.alicloud.ans.server-mode:LOCAL}")
private AliCloudServerMode serverMode;
/**
*
*/
@Value("${spring.cloud.alicloud.ans.server-list:127.0.0.1}")
private String serverList;
/**
*
*/
@Value("${spring.cloud.alicloud.ans.server-port:8080}")
private String serverPort;
/**
* spring.cloud.alicloud.ans.doms 使 spring.application.name
*/
@Value("${spring.cloud.alicloud.ans.client-domains:${spring.application.name:}}")
private String clientDomains;
/**
* spring.cloud.alicloud.ans.weight 1
*/
private float clientWeight = 1;
/**
* doms weight spring.cloud.alicloud.ans.weight.dom1=weight1
*/
private Map<String, Float> clientWeights = new HashMap<String, Float>();
/**
* token spring.cloud.alicloud.ans.token
*/
private String clientToken;
/**
* domstoken spring.cloud.alicloud.ans.tokens.dom1=token1
*/
private Map<String, String> clientTokens = new HashMap<String, String>();
/**
* spring.cloud.alicloud.ans.cluster DEFAULT
*/
private String clientCluster = "DEFAULT";
/**
* metadata serviceInstance ans
*/
private Map<String, String> clientMetadata = new HashMap<>();
/**
* spring.cloud.alicloud.ans.register-enabled=false
*/
private boolean registerEnabled = true;
/**
* ip spring.cloud.alicloud.ans.client-ip
*/
private String clientIp;
/**
* ip
*/
private String clientInterfaceName;
/**
* spring.cloud.alicloud.ans.port
*/
private int clientPort = -1;
/**
*
*/
@Value("${spring.cloud.alicloud.ans.env:${env.id:DEFAULT}}")
private String env;
/**
* https spring.cloud.alicloud.ans.secure false
*/
private boolean secure = false;
@Autowired
private InetUtils inetUtils;
private Map<String, String> tags = new HashMap<>();
@PostConstruct
public void init() throws SocketException {
// 增加注册类型,标记为 spring cloud 应用
tags.put("ANS_SERVICE_TYPE", "SPRING_CLOUD");
if (StringUtils.isEmpty(clientIp)) {
// 如果没有指定注册的ip对应的网卡名则通过遍历网卡去获取
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 + '}';
}
}

@ -0,0 +1,48 @@
/*
* Copyright (C) 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.alicloud.context.edas;
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.cloud.alicloud.context.AliCloudContextAutoConfiguration;
import org.springframework.cloud.alicloud.context.AliCloudProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.cloud.context.edas.AliCloudEdasSdk;
import com.alibaba.cloud.context.edas.AliCloudEdasSdkFactory;
/**
* @author xiaolongzuo
*/
@Configuration
@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());
}
}

@ -0,0 +1,87 @@
/*
* Copyright (C) 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.alicloud.context.edas;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import com.alibaba.cloud.context.edas.EdasConfiguration;
/**
* @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;
}
}

@ -0,0 +1,86 @@
/*
* Copyright (C) 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.alicloud.context.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 (applicationContext.getParent() == null
|| 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
*/
protected abstract void handleEvent(T event);
/**
* condition on class.
*
* @return
*/
protected String conditionalOnClass() {
return null;
}
}

@ -0,0 +1,80 @@
/*
* Copyright (C) 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.alicloud.context.oss;
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.cloud.alicloud.context.AliCloudContextAutoConfiguration;
import org.springframework.cloud.alicloud.context.AliCloudProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import com.alibaba.cloud.context.AliCloudAuthorizationMode;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
/**
* OSS Auto {@link Configuration}
*
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
* @author xiaolongzuo
*/
@Configuration
@ConditionalOnClass(name = "org.springframework.cloud.alicloud.oss.OssAutoConfiguration")
@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()),
"Access key can't be empty.");
Assert.isTrue(!StringUtils.isEmpty(aliCloudProperties.getSecretKey()),
"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.");
}
}
}

@ -0,0 +1,109 @@
/*
* Copyright (C) 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.alicloud.context.oss;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import com.alibaba.cloud.context.AliCloudAuthorizationMode;
import com.aliyun.oss.ClientBuilderConfiguration;
/**
* {@link ConfigurationProperties} for configuring OSS.
*
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
* @author xiaolongzuo
*/
@ConfigurationProperties("spring.cloud.alicloud.oss")
public class OssProperties {
@Value("${spring.cloud.alicloud.oss.authorization-mode:AK_SK}")
private AliCloudAuthorizationMode authorizationMode;
private String endpoint;
private StsToken sts;
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;
}
}
}

@ -0,0 +1,34 @@
{
"properties": [
{
"name": "spring.cloud.alicloud.ans.server.mode",
"type": "java.lang.String",
"defaultValue": "LOCAL",
"description": "Server mode."
},
{
"name": "spring.cloud.alicloud.ans.server.list",
"type": "java.lang.String",
"defaultValue": "127.0.0.1",
"description": "ANS server list."
},
{
"name": "spring.cloud.alicloud.ans.server.port",
"type": "java.lang.Integer",
"defaultValue": "80",
"description": "ANS server port."
},
{
"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.client.env",
"type": "java.lang.String",
"defaultValue": "DEFAULT",
"description": "The env for ans, default value is DEFAULT."
}
]
}

@ -0,0 +1,9 @@
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
org.springframework.cloud.alicloud.context.acm.AcmContextBootstrapConfiguration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.alicloud.context.AliCloudContextAutoConfiguration,\
org.springframework.cloud.alicloud.context.edas.EdasContextAutoConfiguration,\
org.springframework.cloud.alicloud.context.ans.AnsContextAutoConfiguration,\
org.springframework.cloud.alicloud.context.oss.OssContextAutoConfiguration
org.springframework.context.ApplicationListener=\
org.springframework.cloud.alicloud.context.ans.AnsContextApplicationListener

@ -0,0 +1,23 @@
/*
* Copyright (C) 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.alicloud.acm;
/**
* @author xiaolongzuo
*/
public class AcmAutoConfiguration {
}

@ -0,0 +1,23 @@
/*
* Copyright (C) 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.alicloud.ans;
/**
* @author xiaolongzuo
*/
public class AnsAutoConfiguration {
}

@ -0,0 +1,53 @@
/*
* Copyright (C) 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.alicloud.context;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
/**
* @author xiaolongzuo
*/
public class AliCloudPropertiesTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(
AutoConfigurations.of(AliCloudContextAutoConfiguration.class));
@Test
public void testConfigurationValueDefaultsAreAsExpected() {
this.contextRunner.run(context -> {
AliCloudProperties config = context.getBean(AliCloudProperties.class);
assertThat(config.getAccessKey()).isNull();
assertThat(config.getSecretKey()).isNull();
});
}
@Test
public void testConfigurationValuesAreCorrectlyLoaded() {
this.contextRunner.withPropertyValues("spring.cloud.alicloud.access-key=123",
"spring.cloud.alicloud.secret-key=123456").run(context -> {
AliCloudProperties config = context.getBean(AliCloudProperties.class);
assertThat(config.getAccessKey()).isEqualTo("123");
assertThat(config.getSecretKey()).isEqualTo("123456");
});
}
}

@ -0,0 +1,50 @@
/*
* Copyright (C) 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.alicloud.context;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author xiaolongzuo
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = AliCloudSpringApplicationTests.EurekaClientDisabledApp.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" }, webEnvironment = RANDOM_PORT)
@DirtiesContext
public class AliCloudSpringApplicationTests {
@Test
public void contextLoads() {
System.out.println("Context load...");
}
@SpringBootApplication
public static class EurekaClientDisabledApp {
}
}

@ -0,0 +1,78 @@
/*
* Copyright (C) 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.alicloud.context.acm;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.alicloud.context.AliCloudContextAutoConfiguration;
import org.springframework.cloud.alicloud.context.edas.EdasContextAutoConfiguration;
import com.alibaba.cloud.context.AliCloudServerMode;
/**
* @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().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.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 config = context.getBean(AcmProperties.class);
assertThat(config.getServerMode()).isEqualTo(AliCloudServerMode.EDAS);
assertThat(config.getServerList()).isEqualTo("10.10.10.10");
assertThat(config.getServerPort()).isEqualTo("11111");
assertThat(config.getEndpoint()).isEqualTo("testDomain");
assertThat(config.getGroup()).isEqualTo("testGroup");
assertThat(config.getFileExtension()).isEqualTo("yaml");
assertThat(config.getNamespace()).isEqualTo("testNamespace");
});
}
}

@ -0,0 +1,101 @@
/*
* Copyright (C) 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.alicloud.context.ans;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.alicloud.context.AliCloudContextAutoConfiguration;
import org.springframework.cloud.alicloud.context.edas.EdasContextAutoConfiguration;
import com.alibaba.cloud.context.AliCloudServerMode;
/**
* @author xiaolongzuo
*/
public class AnsPropertiesTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(AnsContextAutoConfiguration.class,
EdasContextAutoConfiguration.class,
AliCloudContextAutoConfiguration.class));
@Test
public void testConfigurationValueDefaultsAreAsExpected()
throws ClassNotFoundException {
this.contextRunner.withPropertyValues().run(context -> {
AnsProperties config = context.getBean(AnsProperties.class);
assertThat(config.getServerMode()).isEqualTo(AliCloudServerMode.LOCAL);
assertThat(config.getServerList()).isEqualTo("127.0.0.1");
assertThat(config.getServerPort()).isEqualTo("8080");
assertThat(config.getClientDomains()).isEqualTo("");
assertThat(config.getClientWeight()).isEqualTo(1.0F);
assertThat(config.getClientWeights().size()).isEqualTo(0);
assertThat(config.getClientTokens().size()).isEqualTo(0);
assertThat(config.getClientMetadata().size()).isEqualTo(0);
assertThat(config.getClientToken()).isNull();
assertThat(config.getClientCluster()).isEqualTo("DEFAULT");
assertThat(config.isRegisterEnabled()).isTrue();
assertThat(config.getClientInterfaceName()).isNull();
assertThat(config.getClientPort()).isEqualTo(-1);
assertThat(config.getEnv()).isEqualTo("DEFAULT");
assertThat(config.isSecure()).isFalse();
assertThat(config.getTags().size()).isEqualTo(1);
assertThat(config.getTags().keySet().iterator().next())
.isEqualTo("ANS_SERVICE_TYPE");
assertThat(config.getTags().get("ANS_SERVICE_TYPE"))
.isEqualTo("SPRING_CLOUD");
});
}
@Test
public void testConfigurationValuesAreCorrectlyLoaded() {
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 config = context.getBean(AnsProperties.class);
assertThat(config.getServerMode()).isEqualTo(AliCloudServerMode.EDAS);
assertThat(config.getServerList()).isEqualTo("10.10.10.10");
assertThat(config.getServerPort()).isEqualTo("11111");
assertThat(config.getClientDomains()).isEqualTo("testDomain");
assertThat(config.getClientWeight()).isEqualTo(0.9F);
assertThat(config.getClientWeights().size()).isEqualTo(1);
assertThat(config.getClientTokens().size()).isEqualTo(0);
assertThat(config.getClientMetadata().size()).isEqualTo(0);
assertThat(config.getClientToken()).isNull();
assertThat(config.getClientCluster()).isEqualTo("DEFAULT");
assertThat(config.isRegisterEnabled()).isTrue();
assertThat(config.getClientInterfaceName()).isNull();
assertThat(config.getClientPort()).isEqualTo(-1);
assertThat(config.getEnv()).isEqualTo("DEFAULT");
assertThat(config.isSecure()).isFalse();
assertThat(config.getTags().size()).isEqualTo(1);
assertThat(config.getTags().keySet().iterator().next())
.isEqualTo("ANS_SERVICE_TYPE");
assertThat(config.getTags().get("ANS_SERVICE_TYPE"))
.isEqualTo("SPRING_CLOUD");
});
}
}

@ -0,0 +1,68 @@
/*
* Copyright (C) 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.alicloud.context.edas;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.alicloud.context.AliCloudContextAutoConfiguration;
/**
* @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 config = context.getBean(EdasProperties.class);
assertThat(config.getNamespace()).isNull();
assertThat(config.isApplicationNameValid()).isFalse();
});
}
@Test
public void testConfigurationValuesAreCorrectlyLoaded1() {
this.contextRunner
.withPropertyValues("spring.cloud.alicloud.edas.namespace=testns",
"spring.application.name=myapps")
.run(context -> {
EdasProperties config = context.getBean(EdasProperties.class);
assertThat(config.getNamespace()).isEqualTo("testns");
assertThat(config.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 config = context.getBean(EdasProperties.class);
assertThat(config.getNamespace()).isEqualTo("testns");
assertThat(config.getApplicationName()).isEqualTo("myapps");
});
}
}

@ -0,0 +1,77 @@
/*
* Copyright (C) 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.alicloud.context.oss;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.alicloud.context.AliCloudProperties;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
/**
* {@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")
.withPropertyValues("spring.cloud.alicloud.secretKey=your-sk")
.withPropertyValues(
"spring.cloud.alicloud.oss.endpoint=http://oss-cn-beijing.aliyuncs.com")
.withPropertyValues("spring.cloud.alicloud.oss.config.userAgent=alibaba");
@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");
});
}
@Test
public void testOSSClient() {
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");
});
}
}

@ -0,0 +1,23 @@
/*
* Copyright (C) 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.alicloud.oss;
/**
* @author xiaolongzuo
*/
public class OssAutoConfiguration {
}
Loading…
Cancel
Save