You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ni...@apache.org on 2019/07/07 12:22:06 UTC
[servicecomb-pack] 08/24: add
omega/omega-spring-cloud-nacos-starter module
This is an automated email from the ASF dual-hosted git repository.
ningjiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-pack.git
commit 9227e979379e7609931e059abe0005739bc9b845
Author: CMonkey <42...@gmail.com>
AuthorDate: Fri Jul 5 15:53:21 2019 +0800
add omega/omega-spring-cloud-nacos-starter module
---
omega/omega-spring-cloud-nacos-starter/pom.xml | 51 +++++++++++
.../cloud/OmegaSpringNacosAutoConfiguration.java | 28 ++++++
.../omega/spring/cloud/OmegaSpringNacosConfig.java | 101 +++++++++++++++++++++
.../src/main/resources/META-INF/spring.factories | 17 ++++
4 files changed, 197 insertions(+)
diff --git a/omega/omega-spring-cloud-nacos-starter/pom.xml b/omega/omega-spring-cloud-nacos-starter/pom.xml
new file mode 100644
index 0000000..3448abc
--- /dev/null
+++ b/omega/omega-spring-cloud-nacos-starter/pom.xml
@@ -0,0 +1,51 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Licensed to the Apache Software Foundation (ASF) under one or more
+ ~ contributor license agreements. See the NOTICE file distributed with
+ ~ this work for additional information regarding copyright ownership.
+ ~ The ASF licenses this file to You under the Apache License, Version 2.0
+ ~ (the "License"); you may not use this file except in compliance with
+ ~ the License. You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing, software
+ ~ distributed under the License is distributed on an "AS IS" BASIS,
+ ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ~ See the License for the specific language governing permissions and
+ ~ limitations under the License.
+ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <parent>
+ <artifactId>omega</artifactId>
+ <groupId>org.apache.servicecomb.pack</groupId>
+ <version>0.5.0-SNAPSHOT</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+
+ <artifactId>omega-spring-cloud-nacos-starter</artifactId>
+ <name>Pack::Omega::Spring Cloud Nacos Starter</name>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.servicecomb.pack</groupId>
+ <artifactId>omega-spring-tx</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.servicecomb.pack</groupId>
+ <artifactId>omega-format</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.servicecomb.pack</groupId>
+ <artifactId>omega-connector-grpc</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.cloud</groupId>
+ <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
+ </dependency>
+ </dependencies>
+
+</project>
diff --git a/omega/omega-spring-cloud-nacos-starter/src/main/java/org/apache/servicecomb/pack/omega/spring/cloud/OmegaSpringNacosAutoConfiguration.java b/omega/omega-spring-cloud-nacos-starter/src/main/java/org/apache/servicecomb/pack/omega/spring/cloud/OmegaSpringNacosAutoConfiguration.java
new file mode 100644
index 0000000..ad51986
--- /dev/null
+++ b/omega/omega-spring-cloud-nacos-starter/src/main/java/org/apache/servicecomb/pack/omega/spring/cloud/OmegaSpringNacosAutoConfiguration.java
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.servicecomb.pack.omega.spring.cloud;
+
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Import;
+
+@Configuration
+@Import(OmegaSpringNacosConfig.class)
+@ConditionalOnProperty(value = {"omega.enabled"}, matchIfMissing = true)
+public class OmegaSpringNacosAutoConfiguration {
+}
diff --git a/omega/omega-spring-cloud-nacos-starter/src/main/java/org/apache/servicecomb/pack/omega/spring/cloud/OmegaSpringNacosConfig.java b/omega/omega-spring-cloud-nacos-starter/src/main/java/org/apache/servicecomb/pack/omega/spring/cloud/OmegaSpringNacosConfig.java
new file mode 100644
index 0000000..6007464
--- /dev/null
+++ b/omega/omega-spring-cloud-nacos-starter/src/main/java/org/apache/servicecomb/pack/omega/spring/cloud/OmegaSpringNacosConfig.java
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Get the access address of Alpah Server from Nacos Server
+ * Turn this feautre on by set alpha.cluster.register.type=nacos
+ * First omega gets the Alpha address from Nacos with ${alpha.cluster.serviceId}
+ * If omega can't get it in Nacos then use ${alpha.cluster.address}
+ */
+
+package org.apache.servicecomb.pack.omega.spring.cloud;
+
+import org.apache.servicecomb.pack.omega.connector.grpc.AlphaClusterDiscovery;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.autoconfigure.AutoConfigureAfter;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.cloud.alibaba.nacos.NacosDiscoveryAutoConfiguration;
+import org.springframework.cloud.client.ServiceInstance;
+import org.springframework.cloud.client.discovery.DiscoveryClient;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+@Configuration
+@ConditionalOnProperty(value = {"alpha.cluster.register.type"}, havingValue = "nacos")
+@AutoConfigureAfter(value = {NacosDiscoveryAutoConfiguration.class})
+class OmegaSpringNacosConfig {
+
+ private static final Logger LOG = LoggerFactory.getLogger(OmegaSpringNacosConfig.class);
+
+ @Autowired
+ public DiscoveryClient discoveryClient;
+
+ @Bean
+ AlphaClusterDiscovery alphaClusterAddress(
+ @Value("${alpha.cluster.serviceId:servicecomb-alpha-server}") String serviceId,
+ @Value("${alpha.cluster.address:0.0.0.0:8080}") String[] addresses) {
+ StringBuffer serviceUrls = new StringBuffer();
+
+ String[] alphaAddresses = this.getAlphaAddress(serviceId);
+
+ LOG.info("alphaAddress = {}", Arrays.toString(alphaAddresses));
+
+ if (alphaAddresses.length > 0) {
+ AlphaClusterDiscovery alphaClusterDiscovery = AlphaClusterDiscovery.builder()
+ .discoveryType(AlphaClusterDiscovery.DiscoveryType.ZOOKEEPER)
+ .discoveryInfo(serviceUrls.toString())
+ .addresses(alphaAddresses)
+ .build();
+ return alphaClusterDiscovery;
+ } else {
+ AlphaClusterDiscovery alphaClusterDiscovery = AlphaClusterDiscovery.builder()
+ .discoveryType(AlphaClusterDiscovery.DiscoveryType.DEFAULT)
+ .addresses(addresses)
+ .build();
+ return alphaClusterDiscovery;
+ }
+ }
+
+ private String[] getAlphaAddress(String serviceId) {
+ List<String> alphaAddresses = new ArrayList<>();
+ List<ServiceInstance> serviceInstances = discoveryClient.getInstances(serviceId);
+ boolean foundAlphaServer = Boolean.FALSE;
+ for (ServiceInstance serviceInstance : serviceInstances) {
+ foundAlphaServer = Boolean.TRUE;
+ if (serviceInstance.getMetadata().containsKey(serviceId)) {
+ String alphaAddress = serviceInstance.getMetadata().get(serviceId);
+ alphaAddresses.add(alphaAddress);
+ }
+ }
+ if (foundAlphaServer) {
+ if (alphaAddresses.size() == 0) {
+ LOG.warn("Alpha has been found in Nacos, " +
+ "but Alpha's registered address information is not found in Nacos instance metadata. " +
+ "Please check Alpha is configured spring.profiles.active=spring-cloud");
+ }
+ } else {
+ LOG.warn("No Alpha Server {} found in the Nacos", serviceId);
+ }
+ return alphaAddresses.toArray(new String[alphaAddresses.size()]);
+ }
+}
diff --git a/omega/omega-spring-cloud-nacos-starter/src/main/resources/META-INF/spring.factories b/omega/omega-spring-cloud-nacos-starter/src/main/resources/META-INF/spring.factories
new file mode 100644
index 0000000..a276d59
--- /dev/null
+++ b/omega/omega-spring-cloud-nacos-starter/src/main/resources/META-INF/spring.factories
@@ -0,0 +1,17 @@
+## ---------------------------------------------------------------------------
+## Licensed to the Apache Software Foundation (ASF) under one or more
+## contributor license agreements. See the NOTICE file distributed with
+## this work for additional information regarding copyright ownership.
+## The ASF licenses this file to You under the Apache License, Version 2.0
+## (the "License"); you may not use this file except in compliance with
+## the License. You may obtain a copy of the License at
+##
+## http://www.apache.org/licenses/LICENSE-2.0
+##
+## Unless required by applicable law or agreed to in writing, software
+## distributed under the License is distributed on an "AS IS" BASIS,
+## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+## See the License for the specific language governing permissions and
+## limitations under the License.
+## ---------------------------------------------------------------------------
+org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.apache.servicecomb.pack.omega.spring.cloud.OmegaSpringNacosAutoConfiguration