From 6503f8d2cf833db2f38081a87d0d91302c426c14 Mon Sep 17 00:00:00 2001 From: "chenzhu.zxl" Date: Mon, 29 Oct 2018 14:35:34 +0800 Subject: [PATCH] Add spring-cloud-alicloud-context module. --- pom.xml | 1 + spring-cloud-alicloud-context/pom.xml | 99 +++++ .../AliCloudContextAutoConfiguration.java | 29 ++ .../alicloud/context/AliCloudProperties.java | 57 +++ .../acm/AcmContextBootstrapConfiguration.java | 56 +++ .../alicloud/context/acm/AcmProperties.java | 158 ++++++++ .../ans/AnsContextApplicationListener.java | 54 +++ .../ans/AnsContextAutoConfiguration.java | 44 +++ .../alicloud/context/ans/AnsProperties.java | 337 ++++++++++++++++++ .../edas/EdasContextAutoConfiguration.java | 48 +++ .../alicloud/context/edas/EdasProperties.java | 87 +++++ .../AbstractOnceApplicationListener.java | 86 +++++ .../oss/OssContextAutoConfiguration.java | 80 +++++ .../alicloud/context/oss/OssProperties.java | 109 ++++++ ...itional-spring-configuration-metadata.json | 34 ++ .../main/resources/META-INF/spring.factories | 9 + .../alicloud/acm/AcmAutoConfiguration.java | 23 ++ .../alicloud/ans/AnsAutoConfiguration.java | 23 ++ .../context/AliCloudPropertiesTests.java | 53 +++ .../AliCloudSpringApplicationTests.java | 50 +++ .../context/acm/AcmPropertiesTests.java | 78 ++++ .../context/ans/AnsPropertiesTests.java | 101 ++++++ .../context/edas/EdasPropertiesTests.java | 68 ++++ .../oss/OssAutoConfigurationTests.java | 77 ++++ .../alicloud/oss/OssAutoConfiguration.java | 23 ++ 25 files changed, 1784 insertions(+) create mode 100644 spring-cloud-alicloud-context/pom.xml create mode 100644 spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/AliCloudContextAutoConfiguration.java create mode 100644 spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/AliCloudProperties.java create mode 100644 spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmContextBootstrapConfiguration.java create mode 100644 spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmProperties.java create mode 100644 spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/ans/AnsContextApplicationListener.java create mode 100644 spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/ans/AnsContextAutoConfiguration.java create mode 100644 spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/ans/AnsProperties.java create mode 100644 spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/edas/EdasContextAutoConfiguration.java create mode 100644 spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/edas/EdasProperties.java create mode 100644 spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/listener/AbstractOnceApplicationListener.java create mode 100644 spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/oss/OssContextAutoConfiguration.java create mode 100644 spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/oss/OssProperties.java create mode 100644 spring-cloud-alicloud-context/src/main/resources/META-INF/additional-spring-configuration-metadata.json create mode 100644 spring-cloud-alicloud-context/src/main/resources/META-INF/spring.factories create mode 100644 spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/acm/AcmAutoConfiguration.java create mode 100644 spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/ans/AnsAutoConfiguration.java create mode 100644 spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/AliCloudPropertiesTests.java create mode 100644 spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/AliCloudSpringApplicationTests.java create mode 100644 spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/acm/AcmPropertiesTests.java create mode 100644 spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/ans/AnsPropertiesTests.java create mode 100644 spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/edas/EdasPropertiesTests.java create mode 100644 spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/oss/OssAutoConfigurationTests.java create mode 100644 spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/oss/OssAutoConfiguration.java diff --git a/pom.xml b/pom.xml index c8309d132..797d28e98 100644 --- a/pom.xml +++ b/pom.xml @@ -81,6 +81,7 @@ spring-cloud-starter-alibaba spring-cloud-starter-alicloud spring-cloud-alicloud-oss + spring-cloud-alicloud-context diff --git a/spring-cloud-alicloud-context/pom.xml b/spring-cloud-alicloud-context/pom.xml new file mode 100644 index 000000000..0084d0910 --- /dev/null +++ b/spring-cloud-alicloud-context/pom.xml @@ -0,0 +1,99 @@ + + + + + org.springframework.cloud + spring-cloud-alibaba + 0.2.0.BUILD-SNAPSHOT + + 4.0.0 + + org.springframework.cloud + spring-cloud-alicloud-context + Spring Cloud AliCloud Context + + + + + com.aliyun + aliyun-java-sdk-edas + provided + + + com.aliyun + aliyun-java-sdk-core + + + + + + com.alibaba.cloud + alicloud-context + + + + com.aliyun + aliyun-java-sdk-core + provided + + + + com.alibaba.ans + ans-sdk + provided + + + + com.aliyun.oss + aliyun-sdk-oss + provided + + + + com.alibaba.edas.acm + acm-sdk + provided + + + + org.springframework.cloud + spring-cloud-commons + + + + org.springframework.boot + spring-boot-starter-logging + provided + true + + + + org.springframework.boot + spring-boot + provided + true + + + + org.springframework.boot + spring-boot-autoconfigure + provided + true + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.boot + spring-boot-starter-web + test + + + + + diff --git a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/AliCloudContextAutoConfiguration.java b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/AliCloudContextAutoConfiguration.java new file mode 100644 index 000000000..f0f2f5722 --- /dev/null +++ b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/AliCloudContextAutoConfiguration.java @@ -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 { + +} diff --git a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/AliCloudProperties.java b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/AliCloudProperties.java new file mode 100644 index 000000000..095173292 --- /dev/null +++ b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/AliCloudProperties.java @@ -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; + } + +} diff --git a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmContextBootstrapConfiguration.java b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmContextBootstrapConfiguration.java new file mode 100644 index 000000000..772724420 --- /dev/null +++ b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmContextBootstrapConfiguration.java @@ -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); + } + +} diff --git a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmProperties.java b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmProperties.java new file mode 100644 index 000000000..18b037a39 --- /dev/null +++ b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmProperties.java @@ -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; + } +} diff --git a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/ans/AnsContextApplicationListener.java b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/ans/AnsContextApplicationListener.java new file mode 100644 index 000000000..6d9e9ca20 --- /dev/null +++ b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/ans/AnsContextApplicationListener.java @@ -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 { + + @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); + } + +} diff --git a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/ans/AnsContextAutoConfiguration.java b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/ans/AnsContextAutoConfiguration.java new file mode 100644 index 000000000..fc58b69e9 --- /dev/null +++ b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/ans/AnsContextAutoConfiguration.java @@ -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); + } + +} diff --git a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/ans/AnsProperties.java b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/ans/AnsProperties.java new file mode 100644 index 000000000..2889ae796 --- /dev/null +++ b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/ans/AnsProperties.java @@ -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 clientWeights = new HashMap(); + + /** + * 注册服务的 token ,从 spring.cloud.alicloud.ans.token 中获取 + */ + private String clientToken; + + /** + * 当存在多个doms,需要对应不同的token时,通过 spring.cloud.alicloud.ans.tokens.dom1=token1 的方式配置 + */ + private Map clientTokens = new HashMap(); + + /** + * 配置注册到哪个集群,从 spring.cloud.alicloud.ans.cluster 中获取,默认为 DEFAULT + */ + private String clientCluster = "DEFAULT"; + + /** + * metadata 实现 serviceInstance 接口所需的字段,但 ans 目前尚不支持此字段,配置了也没用 + */ + private Map 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 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 = 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 getTags() { + return tags; + } + + public void setTags(Map 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 getClientWeights() { + return clientWeights; + } + + public void setClientWeights(Map clientWeights) { + this.clientWeights = clientWeights; + } + + @Override + public String getClientToken() { + return clientToken; + } + + public void setClientToken(String clientToken) { + this.clientToken = clientToken; + } + + @Override + public Map getClientTokens() { + return clientTokens; + } + + public void setClientTokens(Map clientTokens) { + this.clientTokens = clientTokens; + } + + @Override + public String getClientCluster() { + return clientCluster; + } + + public void setClientCluster(String clientCluster) { + this.clientCluster = clientCluster; + } + + @Override + public Map getClientMetadata() { + return clientMetadata; + } + + public void setClientMetadata(Map 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 + '}'; + } +} diff --git a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/edas/EdasContextAutoConfiguration.java b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/edas/EdasContextAutoConfiguration.java new file mode 100644 index 000000000..12945c231 --- /dev/null +++ b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/edas/EdasContextAutoConfiguration.java @@ -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()); + } + +} diff --git a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/edas/EdasProperties.java b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/edas/EdasProperties.java new file mode 100644 index 000000000..43c9a7cb9 --- /dev/null +++ b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/edas/EdasProperties.java @@ -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; + } +} diff --git a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/listener/AbstractOnceApplicationListener.java b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/listener/AbstractOnceApplicationListener.java new file mode 100644 index 000000000..d8b822a9b --- /dev/null +++ b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/listener/AbstractOnceApplicationListener.java @@ -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 + implements ApplicationListener { + + private static final String BOOTSTRAP_CONFIG_NAME_VALUE = "bootstrap"; + + private static final String BOOTSTRAP_CONFIG_NAME_KEY = "spring.config.name"; + + private static ConcurrentHashMap, 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; + } + +} diff --git a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/oss/OssContextAutoConfiguration.java b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/oss/OssContextAutoConfiguration.java new file mode 100644 index 000000000..e1993a376 --- /dev/null +++ b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/oss/OssContextAutoConfiguration.java @@ -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 Jim + * @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."); + } + } + +} diff --git a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/oss/OssProperties.java b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/oss/OssProperties.java new file mode 100644 index 000000000..59f64cc3c --- /dev/null +++ b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/oss/OssProperties.java @@ -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 Jim + * @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; + } + + } + +} diff --git a/spring-cloud-alicloud-context/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-alicloud-context/src/main/resources/META-INF/additional-spring-configuration-metadata.json new file mode 100644 index 000000000..d50714101 --- /dev/null +++ b/spring-cloud-alicloud-context/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -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." + } + ] +} \ No newline at end of file diff --git a/spring-cloud-alicloud-context/src/main/resources/META-INF/spring.factories b/spring-cloud-alicloud-context/src/main/resources/META-INF/spring.factories new file mode 100644 index 000000000..e10dcb959 --- /dev/null +++ b/spring-cloud-alicloud-context/src/main/resources/META-INF/spring.factories @@ -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 \ No newline at end of file diff --git a/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/acm/AcmAutoConfiguration.java b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/acm/AcmAutoConfiguration.java new file mode 100644 index 000000000..1ecff17f8 --- /dev/null +++ b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/acm/AcmAutoConfiguration.java @@ -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 { +} diff --git a/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/ans/AnsAutoConfiguration.java b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/ans/AnsAutoConfiguration.java new file mode 100644 index 000000000..1f15f8827 --- /dev/null +++ b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/ans/AnsAutoConfiguration.java @@ -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 { +} diff --git a/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/AliCloudPropertiesTests.java b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/AliCloudPropertiesTests.java new file mode 100644 index 000000000..3253a756f --- /dev/null +++ b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/AliCloudPropertiesTests.java @@ -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"); + }); + } + +} diff --git a/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/AliCloudSpringApplicationTests.java b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/AliCloudSpringApplicationTests.java new file mode 100644 index 000000000..b40edd268 --- /dev/null +++ b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/AliCloudSpringApplicationTests.java @@ -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 { + + } + +} diff --git a/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/acm/AcmPropertiesTests.java b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/acm/AcmPropertiesTests.java new file mode 100644 index 000000000..1a51309df --- /dev/null +++ b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/acm/AcmPropertiesTests.java @@ -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"); + }); + } + +} diff --git a/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/ans/AnsPropertiesTests.java b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/ans/AnsPropertiesTests.java new file mode 100644 index 000000000..11762c1b3 --- /dev/null +++ b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/ans/AnsPropertiesTests.java @@ -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"); + }); + } + +} diff --git a/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/edas/EdasPropertiesTests.java b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/edas/EdasPropertiesTests.java new file mode 100644 index 000000000..5f5f08ae0 --- /dev/null +++ b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/edas/EdasPropertiesTests.java @@ -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"); + }); + } + +} diff --git a/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/oss/OssAutoConfigurationTests.java b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/oss/OssAutoConfigurationTests.java new file mode 100644 index 000000000..042ec0873 --- /dev/null +++ b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/oss/OssAutoConfigurationTests.java @@ -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 Jim + */ +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"); + }); + } + +} diff --git a/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/oss/OssAutoConfiguration.java b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/oss/OssAutoConfiguration.java new file mode 100644 index 000000000..2f6961c6d --- /dev/null +++ b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/oss/OssAutoConfiguration.java @@ -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 { +}