diff --git a/pom.xml b/pom.xml index 797d28e98..dd251b129 100644 --- a/pom.xml +++ b/pom.xml @@ -47,6 +47,10 @@ fangjian fangjian0423@gmail.com + + xiaolongzuo + 150349407@qq.com + hengyunabc hengyunabc@gmail.com @@ -82,6 +86,8 @@ spring-cloud-starter-alicloud spring-cloud-alicloud-oss spring-cloud-alicloud-context + spring-cloud-alicloud-acm + spring-cloud-alicloud-ans diff --git a/spring-cloud-alibaba-dependencies/pom.xml b/spring-cloud-alibaba-dependencies/pom.xml index bf95c0654..36e1bf056 100644 --- a/spring-cloud-alibaba-dependencies/pom.xml +++ b/spring-cloud-alibaba-dependencies/pom.xml @@ -151,6 +151,21 @@ spring-cloud-alibaba-nacos-config ${project.version} + + org.springframework.cloud + spring-cloud-alicloud-acm + ${project.version} + + + org.springframework.cloud + spring-cloud-alicloud-ans + ${project.version} + + + org.springframework.cloud + spring-cloud-alicloud-context + ${project.version} + @@ -176,7 +191,17 @@ ${project.version} + + org.springframework.cloud + spring-cloud-starter-alicloud-ans + ${project.version} + + + org.springframework.cloud + spring-cloud-starter-alicloud-acm + ${project.version} + diff --git a/spring-cloud-alibaba-examples/acm-example/acm-local-example/pom.xml b/spring-cloud-alibaba-examples/acm-example/acm-local-example/pom.xml new file mode 100644 index 000000000..4d9bf1d48 --- /dev/null +++ b/spring-cloud-alibaba-examples/acm-example/acm-local-example/pom.xml @@ -0,0 +1,27 @@ + + + + spring-cloud-alibaba-examples + org.springframework.cloud + 0.1.0.BUILD-SNAPSHOT + + 4.0.0 + acm-local-example + + + + org.springframework.cloud + spring-cloud-starter-alicloud-acm + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-actuator + + + \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/acm-example/acm-local-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/AcmApplication.java b/spring-cloud-alibaba-examples/acm-example/acm-local-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/AcmApplication.java new file mode 100644 index 000000000..5d48c381d --- /dev/null +++ b/spring-cloud-alibaba-examples/acm-example/acm-local-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/AcmApplication.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alibaba.cloud.examples; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; + +/** + * @author xiaolongzuo + */ +@SpringBootApplication +@EnableDiscoveryClient +public class AcmApplication { + + public static void main(String[] args) { + SpringApplication.run(AcmApplication.class, args); + } + +} diff --git a/spring-cloud-alibaba-examples/acm-example/acm-local-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/EchoController.java b/spring-cloud-alibaba-examples/acm-example/acm-local-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/EchoController.java new file mode 100644 index 000000000..b7c874610 --- /dev/null +++ b/spring-cloud-alibaba-examples/acm-example/acm-local-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/EchoController.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alibaba.cloud.examples; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author xiaolongzuo + */ +@RestController +public class EchoController { + + private static final Logger LOGGER = LoggerFactory.getLogger(EchoController.class); + + @Value("${user.id}") + private String userId; + + @RequestMapping(value = "/") + public String echo() { + LOGGER.info("User id is " + userId); + return userId; + } +} diff --git a/spring-cloud-alibaba-examples/acm-example/acm-local-example/src/main/resources/bootstrap.properties b/spring-cloud-alibaba-examples/acm-example/acm-local-example/src/main/resources/bootstrap.properties new file mode 100644 index 000000000..448a0c7dd --- /dev/null +++ b/spring-cloud-alibaba-examples/acm-example/acm-local-example/src/main/resources/bootstrap.properties @@ -0,0 +1,5 @@ +spring.application.group=com.alibaba.acm +spring.application.name=acm-local +server.port=18089 +spring.cloud.alicloud.acm.server-list=127.0.0.1 +spring.cloud.alicloud.acm.server-port=8080 \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/pom.xml b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/pom.xml new file mode 100644 index 000000000..28fafb1fd --- /dev/null +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/pom.xml @@ -0,0 +1,31 @@ + + + + spring-cloud-alibaba-examples + org.springframework.cloud + 0.1.0.BUILD-SNAPSHOT + + 4.0.0 + ans-consumer-feign-example + + + + org.springframework.cloud + spring-cloud-starter-alicloud-ans + + + org.springframework.cloud + spring-cloud-starter-feign + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-actuator + + + \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/ConsumerApplication.java b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/ConsumerApplication.java new file mode 100644 index 000000000..e37fda6e3 --- /dev/null +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/ConsumerApplication.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alibaba.cloud.examples; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.feign.EnableFeignClients; + +/** + * @author xiaolongzuo + */ +@SpringBootApplication +@EnableFeignClients(basePackages = { "org.springframework.cloud.alibaba.cloud.examples" }) +public class ConsumerApplication { + + public static void main(String[] args) { + SpringApplication.run(ConsumerApplication.class, args); + } + +} diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/EchoService.java b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/EchoService.java new file mode 100644 index 000000000..d1717dd5f --- /dev/null +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/EchoService.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alibaba.cloud.examples; + +import org.springframework.cloud.netflix.feign.FeignClient; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +/** + * @author xiaolongzuo + */ +@FeignClient(value = "ans-provider") +public interface EchoService { + + @RequestMapping(path = "echo/{str}") + String echo(@RequestParam("str") String param); + +} diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/HomeController.java b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/HomeController.java new file mode 100644 index 000000000..467d45390 --- /dev/null +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/HomeController.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alibaba.cloud.examples; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author xiaolongzuo + */ +@RestController +public class +HomeController { + + private static final Logger LOGGER = LoggerFactory.getLogger(HomeController.class); + + @Autowired + private EchoService echoService; + + @RequestMapping(value = "/", method = RequestMethod.GET, produces = "application/json") + public String home() { + LOGGER.info("-----------------consumer调用开始-----------------"); + String param = "Nice to meet you."; + LOGGER.info("消费者传递参数:" + param); + String result = echoService.echo(param); + LOGGER.info("收到提供者响应:" + result); + return param + "
" + result; + } +} diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/resources/application.properties b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/resources/application.properties new file mode 100644 index 000000000..1b25ea1e7 --- /dev/null +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/resources/application.properties @@ -0,0 +1,4 @@ +server.port=18083 +# The following configuration can be omitted. +spring.cloud.ans.server.list=127.0.0.1 +spring.cloud.ans.server.port=8080 \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/pom.xml b/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/pom.xml new file mode 100644 index 000000000..c2b43b93f --- /dev/null +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/pom.xml @@ -0,0 +1,27 @@ + + + + spring-cloud-alibaba-examples + org.springframework.cloud + 0.1.0.BUILD-SNAPSHOT + + 4.0.0 + ans-consumer-ribbon-example + + + + org.springframework.cloud + spring-cloud-starter-alicloud-ans + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-actuator + + + \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/ConsumerApplication.java b/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/ConsumerApplication.java new file mode 100644 index 000000000..223a2d891 --- /dev/null +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/ConsumerApplication.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alibaba.cloud.examples; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.client.loadbalancer.LoadBalanced; +import org.springframework.context.annotation.Bean; +import org.springframework.web.client.RestTemplate; + +/** + * @author xiaolongzuo + */ +@SpringBootApplication +@EnableDiscoveryClient +public class ConsumerApplication { + + @Bean + @LoadBalanced + public RestTemplate restTemplate() { + return new RestTemplate(); + } + + public static void main(String[] args) { + SpringApplication.run(ConsumerApplication.class, args); + } + +} diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/HomeController.java b/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/HomeController.java new file mode 100644 index 000000000..e3853fdd3 --- /dev/null +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/HomeController.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alibaba.cloud.examples; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + +/** + * @author xiaolongzuo + */ +@RestController +public class HomeController { + + private static final Logger LOGGER = LoggerFactory.getLogger(HomeController.class); + + @Autowired + private RestTemplate restTemplate; + + @RequestMapping(value = "/", method = RequestMethod.GET, produces = "application/json") + public String home() { + LOGGER.info("-----------------consumer调用开始-----------------"); + String param = "Nice to meet you."; + LOGGER.info("消费者传递参数:" + param); + String result = restTemplate.getForObject("http://ans-provider/echo/" + param, String.class); + LOGGER.info("收到提供者响应:" + result); + return param + "
" + result; + } +} diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/src/main/resources/application.properties b/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/src/main/resources/application.properties new file mode 100644 index 000000000..c7bf8206f --- /dev/null +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/src/main/resources/application.properties @@ -0,0 +1,4 @@ +server.port=18082 +# The following configuration can be omitted. +spring.cloud.ans.server.list=127.0.0.1 +spring.cloud.ans.server.port=8080 \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/ans-example/ans-provider-example/pom.xml b/spring-cloud-alibaba-examples/ans-example/ans-provider-example/pom.xml new file mode 100644 index 000000000..59070f37c --- /dev/null +++ b/spring-cloud-alibaba-examples/ans-example/ans-provider-example/pom.xml @@ -0,0 +1,27 @@ + + + + spring-cloud-alibaba-examples + org.springframework.cloud + 0.1.0.BUILD-SNAPSHOT + + 4.0.0 + ans-provider-example + + + + org.springframework.cloud + spring-cloud-starter-alicloud-ans + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-actuator + + + \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/ans-example/ans-provider-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/EchoController.java b/spring-cloud-alibaba-examples/ans-example/ans-provider-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/EchoController.java new file mode 100644 index 000000000..a9c031ead --- /dev/null +++ b/spring-cloud-alibaba-examples/ans-example/ans-provider-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/EchoController.java @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alibaba.cloud.examples; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author xiaolongzuo + */ +@RestController +public class EchoController { + + private static final Logger LOGGER = LoggerFactory.getLogger(EchoController.class); + + @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET, produces = "application/json") + public String echo(@PathVariable String str) { + LOGGER.info("-----------收到消费者请求-----------"); + LOGGER.info("收到消费者传递的参数:" + str); + String result = "Nice to meet you, too."; + LOGGER.info("提供者返回结果:" + result); + return result; + } +} diff --git a/spring-cloud-alibaba-examples/ans-example/ans-provider-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/ProviderApplication.java b/spring-cloud-alibaba-examples/ans-example/ans-provider-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/ProviderApplication.java new file mode 100644 index 000000000..03436dcb4 --- /dev/null +++ b/spring-cloud-alibaba-examples/ans-example/ans-provider-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/ProviderApplication.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alibaba.cloud.examples; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; + +/** + * @author xiaolongzuo + */ +@SpringBootApplication +@EnableDiscoveryClient +public class ProviderApplication { + + public static void main(String[] args) { + SpringApplication.run(ProviderApplication.class, args); + } + +} diff --git a/spring-cloud-alibaba-examples/ans-example/ans-provider-example/src/main/resources/application.properties b/spring-cloud-alibaba-examples/ans-example/ans-provider-example/src/main/resources/application.properties new file mode 100644 index 000000000..2cb5c6ca9 --- /dev/null +++ b/spring-cloud-alibaba-examples/ans-example/ans-provider-example/src/main/resources/application.properties @@ -0,0 +1,4 @@ +spring.application.name=ans-provider +server.port=18081 +spring.cloud.ans.server.list=127.0.0.1 +spring.cloud.ans.server.port=8080 \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/pom.xml b/spring-cloud-alibaba-examples/pom.xml index fbf652077..fad238409 100644 --- a/spring-cloud-alibaba-examples/pom.xml +++ b/spring-cloud-alibaba-examples/pom.xml @@ -23,6 +23,10 @@ nacos-example/nacos-discovery-example nacos-example/nacos-config-example oss-example + ans-example/ans-consumer-feign-example + ans-example/ans-consumer-ribbon-example + ans-example/ans-provider-example + acm-example/acm-local-example diff --git a/spring-cloud-alicloud-acm/pom.xml b/spring-cloud-alicloud-acm/pom.xml new file mode 100644 index 000000000..d884d8c4c --- /dev/null +++ b/spring-cloud-alicloud-acm/pom.xml @@ -0,0 +1,71 @@ + + + 4.0.0 + + + + org.springframework.cloud + spring-cloud-alibaba + 0.1.0.BUILD-SNAPSHOT + + + spring-cloud-alicloud-acm + + + + + org.springframework.cloud + spring-cloud-alicloud-context + + + + com.aliyun + aliyun-java-sdk-core + + + + com.aliyun + aliyun-java-sdk-edas + + + + com.alibaba.edas.acm + acm-sdk + + + + org.springframework.boot + spring-boot-autoconfigure + provided + true + + + org.springframework.boot + spring-boot-starter-actuator + true + + + + + org.springframework.cloud + spring-cloud-context + + + org.springframework.cloud + spring-cloud-commons + + + org.springframework.boot + spring-boot-configuration-processor + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/AcmAutoConfiguration.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/AcmAutoConfiguration.java new file mode 100644 index 000000000..5930568a9 --- /dev/null +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/AcmAutoConfiguration.java @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.acm; + +import com.taobao.diamond.client.Diamond; +import org.springframework.beans.BeansException; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.cloud.alicloud.acm.endpoint.AcmHealthIndicator; +import org.springframework.cloud.alicloud.acm.refresh.AcmContextRefresher; +import org.springframework.cloud.alicloud.acm.refresh.AcmRefreshHistory; +import org.springframework.cloud.alicloud.context.acm.AcmProperties; +import org.springframework.cloud.context.refresh.ContextRefresher; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Created on 01/10/2017. + * + * @author juven.xuxb + */ +@Configuration +@ConditionalOnClass({Diamond.class}) +@EnableConfigurationProperties(AcmProperties.class) +public class AcmAutoConfiguration implements ApplicationContextAware { + + private ApplicationContext applicationContext; + + @Bean + public AcmPropertySourceRepository acmPropertySourceRepository() { + return new AcmPropertySourceRepository(applicationContext); + } + + @Bean + public AcmHealthIndicator acmHealthIndicator(AcmProperties acmProperties, + AcmPropertySourceRepository acmPropertySourceRepository) { + return new AcmHealthIndicator(acmProperties, acmPropertySourceRepository); + } + + @Bean + public AcmRefreshHistory acmRefreshHistory() { + return new AcmRefreshHistory(); + } + + @Bean + public AcmContextRefresher acmContextRefresher(AcmProperties acmProperties, ContextRefresher contextRefresher, + AcmRefreshHistory refreshHistory, + AcmPropertySourceRepository propertySourceRepository) { + return new AcmContextRefresher(contextRefresher, acmProperties, refreshHistory, propertySourceRepository); + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) + throws BeansException { + this.applicationContext = applicationContext; + } +} diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/AcmPropertySourceRepository.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/AcmPropertySourceRepository.java new file mode 100644 index 000000000..42b84c89f --- /dev/null +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/AcmPropertySourceRepository.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.acm; + +import org.springframework.cloud.alicloud.acm.bootstrap.AcmPropertySource; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.env.CompositePropertySource; +import org.springframework.core.env.PropertySource; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author juven.xuxb, 5/17/16. + */ +public class AcmPropertySourceRepository { + + private final ApplicationContext applicationContext; + + public AcmPropertySourceRepository(ApplicationContext applicationContext) { + this.applicationContext = applicationContext; + } + + /** + * get all acm properties from application context + * @return + */ + public List getAll() { + List result = new ArrayList<>(); + ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) applicationContext; + for (PropertySource p : ctx.getEnvironment().getPropertySources()) { + if (p instanceof AcmPropertySource) { + result.add((AcmPropertySource) p); + } + else if (p instanceof CompositePropertySource) { + collectAcmPropertySources((CompositePropertySource) p, result); + } + } + return result; + } + + private void collectAcmPropertySources(CompositePropertySource composite, + List result) { + for (PropertySource p : composite.getPropertySources()) { + if (p instanceof AcmPropertySource) { + result.add((AcmPropertySource) p); + } + else if (p instanceof CompositePropertySource) { + collectAcmPropertySources((CompositePropertySource) p, result); + } + } + } +} diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySource.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySource.java new file mode 100644 index 000000000..3ebae2773 --- /dev/null +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySource.java @@ -0,0 +1,55 @@ +/* + * 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.bootstrap; + +import org.springframework.core.env.MapPropertySource; + +import java.util.Date; +import java.util.Map; + +/** + * @author juven.xuxb + * @author xiaolongzuo + */ +public class AcmPropertySource extends MapPropertySource { + + private final String dataId; + + private final Date timestamp; + + private final boolean groupLevel; + + AcmPropertySource(String dataId, Map source, Date timestamp, + boolean groupLevel) { + super(dataId, source); + this.dataId = dataId; + this.timestamp = timestamp; + this.groupLevel = groupLevel; + } + + public String getDataId() { + return dataId; + } + + public Date getTimestamp() { + return timestamp; + } + + public boolean isGroupLevel() { + return groupLevel; + } +} diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySourceBuilder.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySourceBuilder.java new file mode 100644 index 000000000..2bf9cbba3 --- /dev/null +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySourceBuilder.java @@ -0,0 +1,96 @@ +/* + * 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.bootstrap; + +import com.alibaba.edas.acm.ConfigService; +import com.alibaba.edas.acm.exception.ConfigException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; +import org.springframework.core.io.ByteArrayResource; +import org.springframework.util.StringUtils; + +import java.io.StringReader; +import java.util.*; + +/** + * @author juven.xuxb + * @author xiaolongzuo + */ +class AcmPropertySourceBuilder { + + private Logger logger = LoggerFactory.getLogger(AcmPropertySourceBuilder.class); + + /** + * 传入 ACM 的 DataId 和 groupID,获取到解析后的 AcmProperty 对象 + * + * @param dataId + * @param diamondGroup + * @param groupLevel + * @return + */ + AcmPropertySource build(String dataId, String diamondGroup, boolean groupLevel) { + Properties properties = loadDiamondData(dataId, diamondGroup); + if (properties == null) { + return null; + } + return new AcmPropertySource(dataId, toMap(properties), new Date(), groupLevel); + } + + private Properties loadDiamondData(String dataId, String diamondGroup) { + try { + String data = ConfigService.getConfig(dataId, diamondGroup, 3000L); + if (StringUtils.isEmpty(data)) { + return null; + } + if (dataId.endsWith(".properties")) { + Properties properties = new Properties(); + logger.info(String.format("Loading acm data, dataId: '%s', group: '%s'", + dataId, diamondGroup)); + properties.load(new StringReader(data)); + return properties; + } else if (dataId.endsWith(".yaml") || dataId.endsWith(".yml")) { + YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean(); + yamlFactory.setResources(new ByteArrayResource(data.getBytes())); + return yamlFactory.getObject(); + } + } catch (Exception e) { + if (e instanceof ConfigException) { + logger.error("DIAMOND-100500:" + dataId + ", " + e.toString(), e); + } else { + logger.error("DIAMOND-100500:" + dataId, e); + } + } + return null; + } + + @SuppressWarnings("unchecked") + private Map toMap(Properties properties) { + Map result = new HashMap<>(); + Enumeration keys = (Enumeration)properties.propertyNames(); + while (keys.hasMoreElements()) { + String key = keys.nextElement(); + Object value = properties.getProperty(key); + if (value != null) { + result.put(key, ((String)value).trim()); + } else { + result.put(key, null); + } + } + return result; + } +} diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySourceLocator.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySourceLocator.java new file mode 100644 index 000000000..a7c167619 --- /dev/null +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySourceLocator.java @@ -0,0 +1,69 @@ +/* + * 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.bootstrap; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.alicloud.context.acm.AcmIntegrationProperties; +import org.springframework.cloud.bootstrap.config.PropertySourceLocator; +import org.springframework.core.env.CompositePropertySource; +import org.springframework.core.env.Environment; +import org.springframework.core.env.PropertySource; + +/** + * @author juven.xuxb + * @author xiaolongzuo + */ +public class AcmPropertySourceLocator implements PropertySourceLocator { + + private static final String DIAMOND_PROPERTY_SOURCE_NAME = "diamond"; + + private static String defaultDiamondGroup = "DEFAULT_GROUP"; + + private AcmPropertySourceBuilder acmPropertySourceBuilder = new AcmPropertySourceBuilder(); + + @Autowired + private AcmIntegrationProperties acmIntegrationProperties; + + @Override + public PropertySource locate(Environment environment) { + + CompositePropertySource compositePropertySource = new CompositePropertySource( + DIAMOND_PROPERTY_SOURCE_NAME); + + for (String dataId : acmIntegrationProperties.getGroupConfigurationDataIds()) { + loadDiamondDataIfPresent(compositePropertySource, dataId, defaultDiamondGroup, + true); + } + + for (String dataId : acmIntegrationProperties + .getApplicationConfigurationDataIds()) { + loadDiamondDataIfPresent(compositePropertySource, dataId, defaultDiamondGroup, + false); + } + + return compositePropertySource; + } + + private void loadDiamondDataIfPresent(final CompositePropertySource composite, + final String dataId, final String diamondGroup, final boolean groupLevel) { + AcmPropertySource ps = acmPropertySourceBuilder.build(dataId, diamondGroup, + groupLevel); + if (ps != null) { + composite.addFirstPropertySource(ps); + } + } +} diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/diagnostics/analyzer/DiamondConnectionFailureAnalyzer.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/diagnostics/analyzer/DiamondConnectionFailureAnalyzer.java new file mode 100644 index 000000000..64d56be39 --- /dev/null +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/diagnostics/analyzer/DiamondConnectionFailureAnalyzer.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.acm.diagnostics.analyzer; + +import org.springframework.boot.diagnostics.AbstractFailureAnalyzer; +import org.springframework.boot.diagnostics.FailureAnalysis; + +/** + * A {@code FailureAnalyzer} that performs analysis of failures caused by a + * {@code DiamondConnectionFailureException}. + * + * @author juven.xuxb, 07/11/2016. + */ +public class DiamondConnectionFailureAnalyzer + extends AbstractFailureAnalyzer { + + @Override + protected FailureAnalysis analyze(Throwable rootFailure, + DiamondConnectionFailureException cause) { + return new FailureAnalysis( + "Application failed to connect to Diamond, unable to access http://" + + cause.getDomain() + ":" + cause.getPort() + + "/diamond-server/diamond", + "config the right endpoint", cause); + } +} diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/diagnostics/analyzer/DiamondConnectionFailureException.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/diagnostics/analyzer/DiamondConnectionFailureException.java new file mode 100644 index 000000000..a1b10abf6 --- /dev/null +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/diagnostics/analyzer/DiamondConnectionFailureException.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.acm.diagnostics.analyzer; + +/** + * A {@code DiamondConnectionFailureException} is thrown when the application fails to + * connect to Diamond Server. + * + * @author juven.xuxb, 07/11/2016. + */ +public class DiamondConnectionFailureException extends RuntimeException { + + private final String domain; + + private final String port; + + public DiamondConnectionFailureException(String domain, String port, String message) { + super(message); + this.domain = domain; + this.port = port; + } + + public DiamondConnectionFailureException(String domain, String port, String message, + Throwable cause) { + super(message, cause); + this.domain = domain; + this.port = port; + } + + String getDomain() { + return domain; + } + + String getPort() { + return port; + } + +} diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/endpoint/AcmEndpoint.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/endpoint/AcmEndpoint.java new file mode 100644 index 000000000..1d7876bd6 --- /dev/null +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/endpoint/AcmEndpoint.java @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.acm.endpoint; + +import org.springframework.boot.actuate.endpoint.AbstractEndpoint; +import org.springframework.cloud.alicloud.acm.AcmPropertySourceRepository; +import org.springframework.cloud.alicloud.acm.bootstrap.AcmPropertySource; +import org.springframework.cloud.alicloud.acm.refresh.AcmRefreshHistory; +import org.springframework.cloud.alicloud.context.acm.AcmProperties; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created on 01/10/2017. + * + * @author juven.xuxb + */ +public class AcmEndpoint extends AbstractEndpoint> { + + private final AcmProperties properties; + + private final AcmRefreshHistory refreshHistory; + + private final AcmPropertySourceRepository propertySourceRepository; + + private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + + public AcmEndpoint(AcmProperties properties, AcmRefreshHistory refreshHistory, + AcmPropertySourceRepository propertySourceRepository) { + super("acm", false); + this.properties = properties; + this.refreshHistory = refreshHistory; + this.propertySourceRepository = propertySourceRepository; + } + + @Override + public Map invoke() { + Map result = new HashMap<>(); + result.put("config", properties); + + Map runtime = new HashMap<>(); + List all = propertySourceRepository.getAll(); + + List> sources = new ArrayList<>(); + for (AcmPropertySource ps : all) { + Map source = new HashMap<>(); + source.put("dataId", ps.getDataId()); + source.put("lastSynced", dateFormat.format(ps.getTimestamp())); + sources.add(source); + } + runtime.put("sources", sources); + runtime.put("refreshHistory", refreshHistory.getRecords()); + + result.put("runtime", runtime); + return result; + } +} diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/endpoint/AcmEndpointAutoConfiguration.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/endpoint/AcmEndpointAutoConfiguration.java new file mode 100644 index 000000000..7048abe89 --- /dev/null +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/endpoint/AcmEndpointAutoConfiguration.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.acm.endpoint; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.cloud.alicloud.acm.AcmPropertySourceRepository; +import org.springframework.cloud.alicloud.acm.refresh.AcmRefreshHistory; +import org.springframework.cloud.alicloud.context.acm.AcmProperties; +import org.springframework.context.annotation.Bean; + +/** + * @author xiaojing + */ +@ConditionalOnWebApplication +@ConditionalOnClass(name = "org.springframework.boot.actuate.endpoint.AbstractEndpoint") +public class AcmEndpointAutoConfiguration { + + @Autowired + private AcmProperties acmProperties; + + @Autowired + private AcmRefreshHistory acmRefreshHistory; + + @Autowired + private AcmPropertySourceRepository acmPropertySourceRepository; + + @ConditionalOnMissingBean + @Bean + public AcmEndpoint acmEndpoint() { + return new AcmEndpoint(acmProperties, acmRefreshHistory, + acmPropertySourceRepository); + } +} diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/endpoint/AcmHealthIndicator.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/endpoint/AcmHealthIndicator.java new file mode 100644 index 000000000..27569ec1a --- /dev/null +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/endpoint/AcmHealthIndicator.java @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.acm.endpoint; + +import com.alibaba.edas.acm.ConfigService; +import org.springframework.boot.actuate.health.AbstractHealthIndicator; +import org.springframework.boot.actuate.health.Health; +import org.springframework.cloud.alicloud.acm.AcmPropertySourceRepository; +import org.springframework.cloud.alicloud.acm.bootstrap.AcmPropertySource; +import org.springframework.cloud.alicloud.context.acm.AcmProperties; +import org.springframework.util.StringUtils; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author leijuan + * @author juven + */ +public class AcmHealthIndicator extends AbstractHealthIndicator { + + private final AcmProperties acmProperties; + + private final AcmPropertySourceRepository acmPropertySourceRepository; + + private final List dataIds; + + public AcmHealthIndicator(AcmProperties acmProperties, + AcmPropertySourceRepository acmPropertySourceRepository) { + this.acmProperties = acmProperties; + this.acmPropertySourceRepository = acmPropertySourceRepository; + + this.dataIds = new ArrayList<>(); + for (AcmPropertySource acmPropertySource : this.acmPropertySourceRepository + .getAll()) { + this.dataIds.add(acmPropertySource.getDataId()); + } + } + + @Override + protected void doHealthCheck(Health.Builder builder) throws Exception { + for (String dataId : dataIds) { + try { + String config = ConfigService.getConfig(dataId, acmProperties.getGroup(), + acmProperties.getTimeout()); + if (StringUtils.isEmpty(config)) { + builder.down().withDetail(String.format("dataId: '%s', group: '%s'", + dataId, acmProperties.getGroup()), "config is empty"); + } + } catch (Exception e) { + builder.down().withDetail(String.format("dataId: '%s', group: '%s'", + dataId, acmProperties.getGroup()), e.getMessage()); + } + } + builder.up().withDetail("dataIds", dataIds); + } +} diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/refresh/AcmContextRefresher.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/refresh/AcmContextRefresher.java new file mode 100644 index 000000000..1fa9e4637 --- /dev/null +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/refresh/AcmContextRefresher.java @@ -0,0 +1,127 @@ +/* + * 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.refresh; + +import com.alibaba.edas.acm.ConfigService; +import com.alibaba.edas.acm.listener.ConfigChangeListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.event.ApplicationReadyEvent; +import org.springframework.cloud.alicloud.acm.AcmPropertySourceRepository; +import org.springframework.cloud.alicloud.acm.bootstrap.AcmPropertySource; +import org.springframework.cloud.alicloud.context.acm.AcmProperties; +import org.springframework.cloud.context.refresh.ContextRefresher; +import org.springframework.context.ApplicationListener; +import org.springframework.core.env.Environment; +import org.springframework.util.StringUtils; + +import java.io.UnsupportedEncodingException; +import java.math.BigInteger; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * On application start up, AcmContextRefresher add diamond listeners to all application + * level dataIds, when there is a change in the data, listeners will refresh + * configurations. + * + * @author juven.xuxb, 5/13/16. + */ +public class AcmContextRefresher implements ApplicationListener { + + private Logger logger = LoggerFactory.getLogger(AcmContextRefresher.class); + + private final ContextRefresher contextRefresher; + + private final AcmProperties properties; + + private final AcmRefreshHistory refreshHistory; + + private final AcmPropertySourceRepository acmPropertySourceRepository; + + private Map listenerMap = new ConcurrentHashMap<>(16); + + @Autowired + private Environment environment; + + public AcmContextRefresher(ContextRefresher contextRefresher, + AcmProperties properties, AcmRefreshHistory refreshHistory, + AcmPropertySourceRepository acmPropertySourceRepository) { + this.contextRefresher = contextRefresher; + this.properties = properties; + this.refreshHistory = refreshHistory; + this.acmPropertySourceRepository = acmPropertySourceRepository; + } + + @Override + public void onApplicationEvent(ApplicationReadyEvent event) { + this.registerDiamondListenersForApplications(); + } + + private void registerDiamondListenersForApplications() { + if (properties.isRefreshEnabled()) { + for (AcmPropertySource acmPropertySource : acmPropertySourceRepository + .getAll()) { + if (acmPropertySource.isGroupLevel()) { + continue; + } + String dataId = acmPropertySource.getDataId(); + registerDiamondListener(dataId); + } + if (acmPropertySourceRepository.getAll().isEmpty()) { + + String applicationName = environment + .getProperty("spring.application.name"); + String dataId = applicationName + "." + properties.getFileExtension(); + + registerDiamondListener(dataId); + } + } + } + + private void registerDiamondListener(final String dataId) { + + ConfigChangeListener listener = listenerMap.get(dataId); + if (listener == null) { + listener = new ConfigChangeListener() { + @Override + public void receiveConfigInfo(String configInfo) { + String md5 = ""; + if (!StringUtils.isEmpty(configInfo)) { + try { + MessageDigest md = MessageDigest.getInstance("MD5"); + md5 = new BigInteger(1, + md.digest(configInfo.getBytes("UTF-8"))).toString(16); + } + catch (NoSuchAlgorithmException + | UnsupportedEncodingException e) { + logger.warn("unable to get md5 for dataId: " + dataId, e); + } + } + refreshHistory.add(dataId, md5); + contextRefresher.refresh(); + } + + }; + listenerMap.put(dataId, listener); + } + } + +} diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/refresh/AcmRefreshHistory.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/refresh/AcmRefreshHistory.java new file mode 100644 index 000000000..439e63604 --- /dev/null +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/refresh/AcmRefreshHistory.java @@ -0,0 +1,72 @@ +/* + * 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.refresh; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.LinkedList; + +/** + * @author juven.xuxb, 5/16/16. + */ +public class AcmRefreshHistory { + + private static final int MAX_SIZE = 20; + + private LinkedList records = new LinkedList<>(); + + private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + + public void add(String dataId, String md5) { + records.addFirst(new Record(dateFormat.format(new Date()), dataId, md5)); + if (records.size() > MAX_SIZE) { + records.removeLast(); + } + } + + public LinkedList getRecords() { + return records; + } +} + +class Record { + + private final String timestamp; + + private final String dataId; + + private final String md5; + + public Record(String timestamp, String dataId, String md5) { + this.timestamp = timestamp; + this.dataId = dataId; + this.md5 = md5; + } + + public String getTimestamp() { + return timestamp; + } + + public String getDataId() { + return dataId; + } + + public String getMd5() { + return md5; + } +} diff --git a/spring-cloud-alicloud-acm/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-alicloud-acm/src/main/resources/META-INF/additional-spring-configuration-metadata.json new file mode 100644 index 000000000..29f53c76e --- /dev/null +++ b/spring-cloud-alicloud-acm/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -0,0 +1,9 @@ +{ + "properties": [ + { + "name": "spring.application.group", + "type": "java.lang.String", + "description": "spring application group." + } + ] +} \ No newline at end of file diff --git a/spring-cloud-alicloud-acm/src/main/resources/META-INF/spring.factories b/spring-cloud-alicloud-acm/src/main/resources/META-INF/spring.factories new file mode 100644 index 000000000..4f39095fe --- /dev/null +++ b/spring-cloud-alicloud-acm/src/main/resources/META-INF/spring.factories @@ -0,0 +1,9 @@ +org.springframework.cloud.bootstrap.BootstrapConfiguration=\ +org.springframework.cloud.alicloud.acm.bootstrap.AcmPropertySourceLocator + +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +org.springframework.cloud.alicloud.acm.AcmAutoConfiguration,\ +org.springframework.cloud.alicloud.acm.endpoint.AcmEndpointAutoConfiguration + +org.springframework.boot.diagnostics.FailureAnalyzer=\ +org.springframework.cloud.alicloud.acm.diagnostics.analyzer.DiamondConnectionFailureAnalyzer \ No newline at end of file diff --git a/spring-cloud-alicloud-ans/pom.xml b/spring-cloud-alicloud-ans/pom.xml new file mode 100644 index 000000000..1f1ec0bf6 --- /dev/null +++ b/spring-cloud-alicloud-ans/pom.xml @@ -0,0 +1,87 @@ + + + + spring-cloud-alibaba + org.springframework.cloud + 0.1.0.BUILD-SNAPSHOT + + 4.0.0 + + org.springframework.cloud + spring-cloud-alicloud-ans + Spring Cloud Alibaba Cloud ANS + + + + + com.alibaba.ans + ans-sdk + + + + com.aliyun + aliyun-java-sdk-core + + + + com.aliyun + aliyun-java-sdk-edas + + + + org.springframework.cloud + spring-cloud-alicloud-context + + + + org.springframework + spring-context + + + org.springframework.cloud + spring-cloud-commons + + + + org.springframework.cloud + spring-cloud-starter-netflix-ribbon + + + + org.springframework.boot + spring-boot-actuator + true + + + org.springframework.boot + spring-boot-configuration-processor + provided + true + + + org.springframework.boot + spring-boot-autoconfigure + provided + true + + + org.springframework.boot + spring-boot-starter-web + test + + + org.springframework.boot + spring-boot-starter-actuator + test + + + org.springframework.boot + spring-boot-starter-test + test + + + + + \ No newline at end of file diff --git a/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/AnsAutoConfiguration.java b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/AnsAutoConfiguration.java new file mode 100644 index 000000000..52e353093 --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/AnsAutoConfiguration.java @@ -0,0 +1,66 @@ +/* + * 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; + +import org.springframework.boot.autoconfigure.AutoConfigureBefore; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.cloud.alicloud.ans.registry.AnsAutoServiceRegistration; +import org.springframework.cloud.alicloud.ans.registry.AnsRegistration; +import org.springframework.cloud.alicloud.ans.registry.AnsServiceRegistry; +import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration; +import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * @author xiaolongzuo + */ +@Configuration +@EnableConfigurationProperties +@ConditionalOnClass(name = "org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent") +@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true) +@ConditionalOnAnsEnabled +@AutoConfigureBefore({ AutoServiceRegistrationAutoConfiguration.class, + AnsDiscoveryClientAutoConfiguration.class }) +public class AnsAutoConfiguration { + + @Bean + public AnsServiceRegistry ansServiceRegistry() { + return new AnsServiceRegistry(); + } + + @Bean + @ConditionalOnBean(AutoServiceRegistrationProperties.class) + @ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true) + public AnsRegistration ansRegistration() { + return new AnsRegistration(); + } + + @Bean + @ConditionalOnBean(AutoServiceRegistrationProperties.class) + @ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true) + public AnsAutoServiceRegistration ansAutoServiceRegistration( + AnsServiceRegistry registry, + AutoServiceRegistrationProperties autoServiceRegistrationProperties, + AnsRegistration registration) { + return new AnsAutoServiceRegistration(registry, autoServiceRegistrationProperties, + registration); + } +} diff --git a/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/AnsDiscoveryClient.java b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/AnsDiscoveryClient.java new file mode 100644 index 000000000..34bcb64c4 --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/AnsDiscoveryClient.java @@ -0,0 +1,103 @@ +/* + * 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; + +import java.util.*; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.alicloud.context.ans.AnsProperties; +import org.springframework.cloud.client.DefaultServiceInstance; +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.discovery.DiscoveryClient; + +import com.alibaba.ans.core.NamingService; +import com.alibaba.ans.shaded.com.taobao.vipserver.client.core.Host; + +/** + * @author xiaolongzuo + */ +public class AnsDiscoveryClient implements DiscoveryClient { + + public static final String DESCRIPTION = "Spring Cloud ANS Discovery Client"; + + @Autowired + private AnsProperties ansProperties; + + @Override + public String description() { + return DESCRIPTION; + } + + @Override + public ServiceInstance getLocalServiceInstance() { + String serviceId = ansProperties.getClientDomains(); + String host = ansProperties.getClientIp(); + int port = ansProperties.getClientPort(); + boolean secure = ansProperties.isSecure(); + Map metadata = ansProperties.getClientMetadata(); + return new DefaultServiceInstance(serviceId, host, port, secure, metadata); + } + + @Override + public List getInstances(String serviceId) { + try { + List hosts = NamingService.getHosts(serviceId); + return hostToServiceInstanceList(hosts, serviceId); + } + catch (Exception e) { + throw new RuntimeException( + "Can not get hosts from ans server. serviceId: " + serviceId, e); + } + } + + private static ServiceInstance hostToServiceInstance(Host host, String serviceId) { + AnsServiceInstance ansServiceInstance = new AnsServiceInstance(); + ansServiceInstance.setHost(host.getIp()); + ansServiceInstance.setPort(host.getPort()); + ansServiceInstance.setServiceId(serviceId); + Map metadata = new HashMap(5); + metadata.put("appUseType", host.getAppUseType()); + metadata.put("site", host.getSite()); + metadata.put("unit", host.getUnit()); + metadata.put("doubleWeight", "" + host.getDoubleWeight()); + metadata.put("weight", "" + host.getWeight()); + ansServiceInstance.setMetadata(metadata); + + return ansServiceInstance; + } + + private static List hostToServiceInstanceList(List hosts, + String serviceId) { + List result = new ArrayList(hosts.size()); + for (Host host : hosts) { + result.add(hostToServiceInstance(host, serviceId)); + } + return result; + } + + @Override + public List getServices() { + + Set doms = NamingService.getDomsSubscribed(); + List result = new LinkedList<>(); + for (String service : doms) { + result.add(service); + } + return result; + } + +} diff --git a/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/AnsDiscoveryClientAutoConfiguration.java b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/AnsDiscoveryClientAutoConfiguration.java new file mode 100644 index 000000000..7d2869763 --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/AnsDiscoveryClientAutoConfiguration.java @@ -0,0 +1,41 @@ +/* + * 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; + +import org.springframework.boot.autoconfigure.AutoConfigureBefore; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * @author xiaolongzuo + */ +@Configuration +@ConditionalOnMissingBean(DiscoveryClient.class) +@EnableConfigurationProperties +@AutoConfigureBefore(SimpleDiscoveryClientAutoConfiguration.class) +public class AnsDiscoveryClientAutoConfiguration { + + @Bean + public DiscoveryClient ansDiscoveryClient() { + return new AnsDiscoveryClient(); + } + +} diff --git a/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/AnsServiceInstance.java b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/AnsServiceInstance.java new file mode 100644 index 000000000..52f7858c0 --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/AnsServiceInstance.java @@ -0,0 +1,90 @@ +/* + * 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; + +import java.net.URI; +import java.util.Map; + +import org.springframework.cloud.client.DefaultServiceInstance; +import org.springframework.cloud.client.ServiceInstance; + +/** + * @author xiaolongzuo + */ +public class AnsServiceInstance implements ServiceInstance { + + private String serviceId; + + private String host; + + private int port; + + private boolean secure; + + private Map metadata; + + @Override + public String getServiceId() { + return serviceId; + } + + @Override + public String getHost() { + return host; + } + + @Override + public int getPort() { + return port; + } + + @Override + public boolean isSecure() { + return secure; + } + + @Override + public URI getUri() { + return DefaultServiceInstance.getUri(this); + } + + @Override + public Map getMetadata() { + return metadata; + } + + public void setServiceId(String serviceId) { + this.serviceId = serviceId; + } + + public void setHost(String host) { + this.host = host; + } + + public void setPort(int port) { + this.port = port; + } + + public void setSecure(boolean secure) { + this.secure = secure; + } + + public void setMetadata(Map metadata) { + this.metadata = metadata; + } + +} diff --git a/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ConditionalOnAnsEnabled.java b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ConditionalOnAnsEnabled.java new file mode 100644 index 000000000..dc12044e6 --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ConditionalOnAnsEnabled.java @@ -0,0 +1,33 @@ +/* + * 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; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; + +/** + * @author xiaolongzuo + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.TYPE, ElementType.METHOD }) +@ConditionalOnProperty(value = "spring.cloud.ans.enabled", matchIfMissing = true) +public @interface ConditionalOnAnsEnabled { +} diff --git a/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/endpoint/AnsEndpoint.java b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/endpoint/AnsEndpoint.java new file mode 100644 index 000000000..53be50450 --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/endpoint/AnsEndpoint.java @@ -0,0 +1,70 @@ +/* + * 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.endpoint; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.actuate.endpoint.AbstractEndpoint; +import org.springframework.cloud.alicloud.context.ans.AnsProperties; + +import com.alibaba.ans.core.NamingService; +import com.alibaba.ans.shaded.com.taobao.vipserver.client.core.Host; + +/** + * @author xiaolongzuo + */ +public class AnsEndpoint extends AbstractEndpoint> { + + private static final Logger LOGGER = LoggerFactory.getLogger(AnsEndpoint.class); + + private AnsProperties ansProperties; + + public AnsEndpoint(AnsProperties ansProperties) { + super("ans", false); + } + + /** + * @return ans endpoint + */ + @Override + public Map invoke() { + Map ansEndpoint = new HashMap<>(); + LOGGER.info("ANS endpoint invoke, ansProperties is {}", ansProperties); + ansEndpoint.put("ansProperties", ansProperties); + + Map subscribes = new HashMap<>(); + Set subscribeServices = NamingService.getDomsSubscribed(); + for (String service : subscribeServices) { + try { + List hosts = NamingService.getHosts(service); + subscribes.put(service, hosts); + } + catch (Exception ignoreException) { + + } + } + ansEndpoint.put("subscribes", subscribes); + LOGGER.info("ANS endpoint invoke, subscribes is {}", subscribes); + return ansEndpoint; + } + +} diff --git a/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/endpoint/AnsEndpointAutoConfiguration.java b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/endpoint/AnsEndpointAutoConfiguration.java new file mode 100644 index 000000000..62883e9a1 --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/endpoint/AnsEndpointAutoConfiguration.java @@ -0,0 +1,35 @@ +/* + * 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.endpoint; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.cloud.alicloud.context.ans.AnsProperties; +import org.springframework.context.annotation.Bean; + +/** + * @author xiaolongzuo + */ +@ConditionalOnWebApplication +@ConditionalOnClass(name = "org.springframework.boot.actuate.endpoint.AbstractEndpoint") +public class AnsEndpointAutoConfiguration { + + @Bean + public AnsEndpoint ansEndpoint(AnsProperties ansProperties) { + return new AnsEndpoint(ansProperties); + } +} diff --git a/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/registry/AnsAutoServiceRegistration.java b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/registry/AnsAutoServiceRegistration.java new file mode 100644 index 000000000..870c5fc89 --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/registry/AnsAutoServiceRegistration.java @@ -0,0 +1,113 @@ +/* + * 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.registry; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration; +import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties; +import org.springframework.cloud.client.serviceregistry.ServiceRegistry; +import org.springframework.util.Assert; +import org.springframework.util.StringUtils; + +/** + * @author xiaolongzuo + */ +public class AnsAutoServiceRegistration + extends AbstractAutoServiceRegistration { + private static final Logger LOGGER = LoggerFactory + .getLogger(AnsAutoServiceRegistration.class); + + @Autowired + private AnsRegistration registration; + + public AnsAutoServiceRegistration(ServiceRegistry serviceRegistry, + AutoServiceRegistrationProperties autoServiceRegistrationProperties, + AnsRegistration registration) { + super(serviceRegistry, autoServiceRegistrationProperties); + this.registration = registration; + } + + @Deprecated + public void setPort(int port) { + getPort().set(port); + } + + @Override + protected AnsRegistration getRegistration() { + if (this.registration.getPort() < 0 && this.getPort().get() > 0) { + this.registration.setPort(this.getPort().get()); + } + Assert.isTrue(this.registration.getPort() > 0, "service.port has not been set"); + return this.registration; + } + + @Override + protected AnsRegistration getManagementRegistration() { + return null; + } + + @Override + protected void register() { + if (!this.registration.getAnsProperties().isRegisterEnabled()) { + LOGGER.debug("Registration disabled."); + return; + } + if (this.registration.getPort() < 0) { + this.registration.setPort(getPort().get()); + } + super.register(); + } + + @Override + protected void registerManagement() { + if (!this.registration.getAnsProperties().isRegisterEnabled()) { + return; + } + super.registerManagement(); + + } + + @Override + protected int getConfiguredPort() { + return this.getPort().get(); + } + + @Override + protected void setConfiguredPort(int port) { + this.getPort().set(port); + } + + @Override + protected Object getConfiguration() { + return this.registration.getAnsProperties(); + } + + @Override + protected boolean isEnabled() { + return this.registration.getAnsProperties().isRegisterEnabled(); + } + + @Override + @SuppressWarnings("deprecation") + protected String getAppName() { + String appName = registration.getServiceId(); + return StringUtils.isEmpty(appName) ? super.getAppName() : appName; + } + +} diff --git a/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/registry/AnsRegistration.java b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/registry/AnsRegistration.java new file mode 100644 index 000000000..a78414fd6 --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/registry/AnsRegistration.java @@ -0,0 +1,130 @@ +/* + * 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.registry; + +import java.net.URI; +import java.util.Map; + +import javax.annotation.PostConstruct; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.alicloud.context.ans.AnsProperties; +import org.springframework.cloud.client.DefaultServiceInstance; +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.discovery.ManagementServerPortUtils; +import org.springframework.cloud.client.serviceregistry.Registration; +import org.springframework.context.ApplicationContext; +import org.springframework.core.env.Environment; +import org.springframework.util.StringUtils; + +/** + * @author xiaolongzuo + */ +public class AnsRegistration implements Registration, ServiceInstance { + + private static final String MANAGEMENT_PORT = "management.port"; + private static final String MANAGEMENT_CONTEXT_PATH = "management.context-path"; + private static final String MANAGEMENT_ADDRESS = "management.address"; + + @Autowired + private AnsProperties ansProperties; + + @Autowired + private ApplicationContext context; + + @PostConstruct + public void init() { + + Environment env = context.getEnvironment(); + Integer managementPort = ManagementServerPortUtils.getPort(context); + if (null != managementPort) { + Map metadata = ansProperties.getClientMetadata(); + metadata.put(MANAGEMENT_PORT, managementPort.toString()); + String contextPath = env.getProperty("management.context-path"); + String address = env.getProperty("management.address"); + if (!StringUtils.isEmpty(contextPath)) { + metadata.put(MANAGEMENT_CONTEXT_PATH, contextPath); + } + if (!StringUtils.isEmpty(address)) { + metadata.put(MANAGEMENT_ADDRESS, address); + } + } + } + + @Override + public String getServiceId() { + return ansProperties.getClientDomains(); + } + + @Override + public String getHost() { + return ansProperties.getClientIp(); + } + + @Override + public int getPort() { + return ansProperties.getClientPort(); + } + + public void setPort(int port) { + // if spring.cloud.ans.port is not set,use the port detected from context + if (ansProperties.getClientPort() < 0) { + this.ansProperties.setClientPort(port); + } + } + + @Override + public boolean isSecure() { + return ansProperties.isSecure(); + } + + @Override + public URI getUri() { + return DefaultServiceInstance.getUri(this); + } + + @Override + public Map getMetadata() { + return ansProperties.getClientMetadata(); + } + + public boolean isRegisterEnabled() { + return ansProperties.isRegisterEnabled(); + } + + public String getCluster() { + return ansProperties.getClientCluster(); + } + + public float getRegisterWeight(String dom) { + if (null != ansProperties.getClientWeights().get(dom) + && ansProperties.getClientWeights().get(dom) > 0) { + return ansProperties.getClientWeights().get(dom); + } + return ansProperties.getClientWeight(); + } + + public AnsProperties getAnsProperties() { + return ansProperties; + } + + @Override + public String toString() { + return "AnsRegistration{" + "ansProperties=" + ansProperties + '}'; + } + +} diff --git a/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/registry/AnsServiceRegistry.java b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/registry/AnsServiceRegistry.java new file mode 100644 index 000000000..ebe65ee39 --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/registry/AnsServiceRegistry.java @@ -0,0 +1,114 @@ +/* + * 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.registry; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.cloud.client.serviceregistry.ServiceRegistry; + +import com.alibaba.ans.core.NamingService; +import com.alibaba.ans.shaded.com.taobao.vipserver.client.ipms.NodeReactor; + +/** + * @author xiaolongzuo + */ +public class AnsServiceRegistry implements ServiceRegistry { + + private static Logger logger = LoggerFactory.getLogger(AnsServiceRegistry.class); + + private static final String SEPARATOR = ","; + + @Override + public void register(AnsRegistration registration) { + + if (!registration.isRegisterEnabled()) { + logger.info("Registration is disabled..."); + return; + } + if (StringUtils.isEmpty(registration.getServiceId())) { + logger.info("No service to register for client..."); + return; + } + + List tags = new ArrayList<>(); + for (Map.Entry entry : registration.getAnsProperties().getTags() + .entrySet()) { + NodeReactor.Tag tag = new NodeReactor.Tag(); + tag.setName(entry.getKey()); + tag.setValue(entry.getValue()); + tags.add(tag); + } + + for (String dom : registration.getServiceId().split(SEPARATOR)) { + try { + NamingService.regDom(dom, registration.getHost(), registration.getPort(), + registration.getRegisterWeight(dom), registration.getCluster(), + tags); + logger.info("INFO_ANS_REGISTER, {} {}:{} register finished", dom, + registration.getAnsProperties().getClientIp(), + registration.getAnsProperties().getClientPort()); + } + catch (Exception e) { + logger.error("ERR_ANS_REGISTER, {} register failed...{},", dom, + registration.toString(), e); + } + } + } + + @Override + public void deregister(AnsRegistration registration) { + + logger.info("De-registering from ANSServer now..."); + + if (StringUtils.isEmpty(registration.getServiceId())) { + logger.info("No dom to de-register for client..."); + return; + } + + try { + NamingService.deRegDom(registration.getServiceId(), registration.getHost(), + registration.getPort(), registration.getCluster()); + } + catch (Exception e) { + logger.error("ERR_ANS_DEREGISTER, de-register failed...{},", + registration.toString(), e); + } + + logger.info("De-registration finished."); + } + + @Override + public void close() { + + } + + @Override + public void setStatus(AnsRegistration registration, String status) { + + } + + @Override + public T getStatus(AnsRegistration registration) { + return null; + } + +} diff --git a/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/AnsRibbonClientConfiguration.java b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/AnsRibbonClientConfiguration.java new file mode 100644 index 000000000..7734e0371 --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/AnsRibbonClientConfiguration.java @@ -0,0 +1,39 @@ +/* + * 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.ribbon; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.netflix.client.config.IClientConfig; +import com.netflix.loadbalancer.ServerList; + +/** + * @author xiaolongzuo + */ +@Configuration +public class AnsRibbonClientConfiguration { + + @Bean + @ConditionalOnMissingBean + public ServerList ribbonServerList(IClientConfig config) { + AnsServerList serverList = new AnsServerList(config.getClientName()); + return serverList; + } + +} \ No newline at end of file diff --git a/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/AnsServer.java b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/AnsServer.java new file mode 100644 index 000000000..8438a7f35 --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/AnsServer.java @@ -0,0 +1,74 @@ +/* + * 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.ribbon; + +import java.util.Collections; +import java.util.Map; + +import com.alibaba.ans.shaded.com.taobao.vipserver.client.core.Host; +import com.netflix.loadbalancer.Server; + +/** + * @author xiaolongzuo + */ +public class AnsServer extends Server { + + private final MetaInfo metaInfo; + private final Host host; + private final Map metadata; + + public AnsServer(final Host host, final String dom) { + super(host.getIp(), host.getPort()); + this.host = host; + this.metadata = Collections.emptyMap(); + metaInfo = new MetaInfo() { + @Override + public String getAppName() { + return dom; + } + + @Override + public String getServerGroup() { + return getMetadata().get("group"); + } + + @Override + public String getServiceIdForDiscovery() { + return null; + } + + @Override + public String getInstanceId() { + return null; + } + }; + } + + @Override + public MetaInfo getMetaInfo() { + return metaInfo; + } + + public Host getHealthService() { + return this.host; + } + + public Map getMetadata() { + return metadata; + } + +} diff --git a/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/AnsServerList.java b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/AnsServerList.java new file mode 100644 index 000000000..f34f19ffd --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/AnsServerList.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.ans.ribbon; + +import java.util.ArrayList; +import java.util.List; + +import com.alibaba.ans.core.NamingService; +import com.alibaba.ans.shaded.com.taobao.vipserver.client.core.Host; +import com.netflix.client.config.IClientConfig; +import com.netflix.loadbalancer.AbstractServerList; + +/** + * @author xiaolongzuo + */ +public class AnsServerList extends AbstractServerList { + + private String dom; + + public AnsServerList(String dom) { + this.dom = dom; + } + + @Override + public List getInitialListOfServers() { + try { + List hosts = NamingService.getHosts(getDom()); + return hostsToServerList(hosts); + } + catch (Exception e) { + throw new IllegalStateException("Can not get ans hosts, dom=" + getDom(), e); + } + } + + @Override + public List getUpdatedListOfServers() { + return getInitialListOfServers(); + } + + private AnsServer hostToServer(Host host) { + AnsServer server = new AnsServer(host, getDom()); + return server; + } + + private List hostsToServerList(List hosts) { + List result = new ArrayList(hosts.size()); + for (Host host : hosts) { + if (host.isValid()) { + result.add(hostToServer(host)); + } + } + + return result; + } + + public String getDom() { + return dom; + } + + @Override + public void initWithNiwsConfig(IClientConfig iClientConfig) { + this.dom = iClientConfig.getClientName(); + } +} diff --git a/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/ConditionalOnRibbonAns.java b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/ConditionalOnRibbonAns.java new file mode 100644 index 000000000..078e6fdd1 --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/ConditionalOnRibbonAns.java @@ -0,0 +1,34 @@ +/* + * 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.ribbon; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; + +/** + * @author xiaolongzuo + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.TYPE, ElementType.METHOD }) +@ConditionalOnProperty(value = "ribbon.ans.enabled", matchIfMissing = true) +public @interface ConditionalOnRibbonAns { + +} diff --git a/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/RibbonAnsAutoConfiguration.java b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/RibbonAnsAutoConfiguration.java new file mode 100644 index 000000000..4333cb534 --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/RibbonAnsAutoConfiguration.java @@ -0,0 +1,39 @@ +/* + * 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.ribbon; + +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.cloud.alicloud.ans.ConditionalOnAnsEnabled; +import org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration; +import org.springframework.cloud.netflix.ribbon.RibbonClients; +import org.springframework.cloud.netflix.ribbon.SpringClientFactory; +import org.springframework.context.annotation.Configuration; + +/** + * @author xiaolongzuo + */ +@Configuration +@EnableConfigurationProperties +@ConditionalOnAnsEnabled +@ConditionalOnBean(SpringClientFactory.class) +@ConditionalOnRibbonAns +@AutoConfigureAfter(RibbonAutoConfiguration.class) +@RibbonClients(defaultConfiguration = AnsRibbonClientConfiguration.class) +public class RibbonAnsAutoConfiguration { +} diff --git a/spring-cloud-alicloud-ans/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-alicloud-ans/src/main/resources/META-INF/additional-spring-configuration-metadata.json new file mode 100644 index 000000000..69ade28d5 --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -0,0 +1,28 @@ +{ + "properties": [ + { + "name": "spring.cloud.ans.server.list", + "type": "java.lang.String", + "defaultValue": "127.0.0.1", + "description": "ANS server list." + }, + { + "name": "spring.cloud.ans.server.port", + "type": "java.lang.Integer", + "defaultValue": "80", + "description": "ANS server port." + }, + { + "name": "spring.cloud.ans.client.domains", + "type": "java.lang.String", + "defaultValue": "", + "description": "Service name list, default value is ${spring.application.name}." + }, + { + "name": "spring.cloud.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-ans/src/main/resources/META-INF/spring.factories b/spring-cloud-alicloud-ans/src/main/resources/META-INF/spring.factories new file mode 100644 index 000000000..63d6cd5c8 --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/resources/META-INF/spring.factories @@ -0,0 +1,6 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ + org.springframework.cloud.alicloud.ans.endpoint.AnsEndpointAutoConfiguration,\ + org.springframework.cloud.alicloud.ans.ribbon.RibbonAnsAutoConfiguration,\ + org.springframework.cloud.alicloud.ans.AnsAutoConfiguration +org.springframework.cloud.client.discovery.EnableDiscoveryClient=\ + org.springframework.cloud.alicloud.ans.AnsDiscoveryClientAutoConfiguration diff --git a/spring-cloud-alicloud-ans/src/test/java/org/springframework/cloud/alibaba/ans/ribbon/AnsServiceListTests.java b/spring-cloud-alicloud-ans/src/test/java/org/springframework/cloud/alibaba/ans/ribbon/AnsServiceListTests.java new file mode 100644 index 000000000..5cb38532a --- /dev/null +++ b/spring-cloud-alicloud-ans/src/test/java/org/springframework/cloud/alibaba/ans/ribbon/AnsServiceListTests.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.alibaba.ans.ribbon; + +import static org.junit.Assert.*; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; + +import java.util.Arrays; +import java.util.List; + +import org.junit.Test; +import org.springframework.cloud.alicloud.ans.ribbon.AnsServer; +import org.springframework.cloud.alicloud.ans.ribbon.AnsServerList; + +import com.alibaba.ans.shaded.com.taobao.vipserver.client.core.Host; +import com.netflix.loadbalancer.Server; + +/** + * @author xiaolongzuo + */ +public class AnsServiceListTests { + + static final String IP_ADDR = "10.0.0.2"; + + static final int PORT = 8080; + + @Test + public void testAnsServer() { + AnsServerList serverList = getAnsServerList(); + List servers = serverList.getInitialListOfServers(); + assertNotNull("servers was null", servers); + assertEquals("servers was not size 1", 1, servers.size()); + Server des = assertAnsServer(servers); + assertEquals("hostPort was wrong", IP_ADDR + ":" + PORT, des.getHostPort()); + } + + protected Server assertAnsServer(List servers) { + Server actualServer = servers.get(0); + assertTrue("server was not a DomainExtractingServer", + actualServer instanceof AnsServer); + AnsServer des = AnsServer.class.cast(actualServer); + assertNotNull("host is null", des.getHealthService()); + assertEquals("unit was wrong", "DEFAULT", des.getHealthService().getUnit()); + return des; + } + + protected AnsServerList getAnsServerList() { + Host host = mock(Host.class); + given(host.getIp()).willReturn(IP_ADDR); + given(host.getDoubleWeight()).willReturn(1.0); + given(host.getPort()).willReturn(PORT); + given(host.getWeight()).willReturn(1); + given(host.getUnit()).willReturn("DEFAULT"); + + AnsServer server = new AnsServer(host, "testDom"); + @SuppressWarnings("unchecked") + AnsServerList originalServerList = mock(AnsServerList.class); + given(originalServerList.getInitialListOfServers()) + .willReturn(Arrays.asList(server)); + return originalServerList; + } + +} diff --git a/spring-cloud-alicloud-context/pom.xml b/spring-cloud-alicloud-context/pom.xml index 316c63fb3..dd4051485 100644 --- a/spring-cloud-alicloud-context/pom.xml +++ b/spring-cloud-alicloud-context/pom.xml @@ -63,28 +63,28 @@ org.springframework.boot - spring-boot-configuration-processor + spring-boot-starter-logging provided true org.springframework.boot - spring-boot-starter-logging + spring-boot provided true org.springframework.boot - spring-boot + spring-boot-autoconfigure provided true org.springframework.boot - spring-boot-autoconfigure + spring-boot-configuration-processor provided true 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 index fc58b69e9..8966cccdc 100644 --- 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 @@ -31,7 +31,7 @@ import org.springframework.context.annotation.Configuration; */ @Configuration @ConditionalOnClass(name = "org.springframework.cloud.alicloud.ans.AnsAutoConfiguration") -@EnableConfigurationProperties({ AnsProperties.class, InetUtilsProperties.class }) +@EnableConfigurationProperties({ AnsProperties.class }) @ImportAutoConfiguration(EdasContextAutoConfiguration.class) public class AnsContextAutoConfiguration { diff --git a/spring-cloud-starter-alicloud/pom.xml b/spring-cloud-starter-alicloud/pom.xml index c7c63285a..57429edf1 100644 --- a/spring-cloud-starter-alicloud/pom.xml +++ b/spring-cloud-starter-alicloud/pom.xml @@ -14,6 +14,8 @@ Spring Cloud Alibaba Cloud Starters spring-cloud-starter-alicloud-oss + spring-cloud-starter-alicloud-acm + spring-cloud-starter-alicloud-ans diff --git a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-acm/pom.xml b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-acm/pom.xml new file mode 100644 index 000000000..c7ffea2ac --- /dev/null +++ b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-acm/pom.xml @@ -0,0 +1,20 @@ + + 4.0.0 + + + org.springframework.cloud + spring-cloud-starter-alicloud + 0.1.0.BUILD-SNAPSHOT + + spring-cloud-starter-alicloud-acm + Spring Cloud Starter Alibaba Cloud ACM + + + + org.springframework.cloud + spring-cloud-alicloud-acm + + + + diff --git a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-ans/pom.xml b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-ans/pom.xml new file mode 100644 index 000000000..b227b6431 --- /dev/null +++ b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-ans/pom.xml @@ -0,0 +1,21 @@ + + + + spring-cloud-starter-alibaba + org.springframework.cloud + 0.1.0.BUILD-SNAPSHOT + + 4.0.0 + spring-cloud-starter-alicloud-ans + Spring Cloud Starter Alibaba Cloud ANS + + + + org.springframework.cloud + spring-cloud-alicloud-ans + + + + \ No newline at end of file