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:03 UTC
[servicecomb-pack] 05/24: add alpha/alpha-spring-cloud-starter-nacos
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 5eac82b2a547a12d0c390826b5cb869b2451d1f5
Author: CMonkey <42...@gmail.com>
AuthorDate: Fri Jul 5 15:43:46 2019 +0800
add alpha/alpha-spring-cloud-starter-nacos
---
alpha/alpha-spring-cloud-starter-nacos/pom.xml | 84 ++++++++++++++++++++++
.../nacos/GrpcStartableStartedEventListener.java | 74 +++++++++++++++++++
2 files changed, 158 insertions(+)
diff --git a/alpha/alpha-spring-cloud-starter-nacos/pom.xml b/alpha/alpha-spring-cloud-starter-nacos/pom.xml
new file mode 100644
index 0000000..a85d164
--- /dev/null
+++ b/alpha/alpha-spring-cloud-starter-nacos/pom.xml
@@ -0,0 +1,84 @@
+<?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>alpha</artifactId>
+ <groupId>org.apache.servicecomb.pack</groupId>
+ <version>0.5.0-SNAPSHOT</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+
+ <artifactId>alpha-spring-cloud-starter-nacos</artifactId>
+ <name>Pack::Alpha::Spring Cloud Starter Nacos</name>
+
+ <dependencyManagement>
+ <dependencies>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-test</artifactId>
+ <version>${spring.boot.version}</version>
+ <exclusions>
+ <exclusion>
+ <groupId>org.json</groupId>
+ <artifactId>json</artifactId>
+ </exclusion>
+ </exclusions>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-dependencies</artifactId>
+ <version>${spring.boot.version}</version>
+ <type>pom</type>
+ <scope>import</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.cloud</groupId>
+ <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
+ <version>${spring.cloud.starter.alibaba.nacos.discovery.version}</version>
+ </dependency>
+ </dependencies>
+ </dependencyManagement>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.servicecomb.pack</groupId>
+ <artifactId>alpha-core</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-test</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-log4j2</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.cloud</groupId>
+ <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>com.google.guava</groupId>
+ <artifactId>guava</artifactId>
+ </dependency>
+ </dependencies>
+
+</project>
diff --git a/alpha/alpha-spring-cloud-starter-nacos/src/main/java/org/apache/servicecomb/pack/alpha/server/discovery/nacos/GrpcStartableStartedEventListener.java b/alpha/alpha-spring-cloud-starter-nacos/src/main/java/org/apache/servicecomb/pack/alpha/server/discovery/nacos/GrpcStartableStartedEventListener.java
new file mode 100644
index 0000000..7f9b072
--- /dev/null
+++ b/alpha/alpha-spring-cloud-starter-nacos/src/main/java/org/apache/servicecomb/pack/alpha/server/discovery/nacos/GrpcStartableStartedEventListener.java
@@ -0,0 +1,74 @@
+/*
+ * 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.alpha.server.discovery.nacos;
+
+import com.google.common.eventbus.EventBus;
+import com.google.common.eventbus.Subscribe;
+import org.apache.servicecomb.pack.alpha.core.event.GrpcStartableStartedEvent;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.cloud.alibaba.nacos.NacosDiscoveryAutoConfiguration;
+import org.springframework.cloud.alibaba.nacos.NacosDiscoveryProperties;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.lang.invoke.MethodHandles;
+
+/**
+ * Listen for GrpcStartableStartedEvent and initialize the Nacos instance metadata
+ * */
+
+@Component
+@ConditionalOnClass({NacosDiscoveryAutoConfiguration.class})
+@ConditionalOnProperty(value = {"nacos.client.enabled"}, matchIfMissing = false)
+public class GrpcStartableStartedEventListener {
+
+ private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+ public static final String ALPHA_SERVER_GRPC_ADDRESS_KEY = "servicecomb-alpha-server";
+
+ @Autowired
+ @Qualifier("alphaEventBus")
+ EventBus eventBus;
+
+ @Autowired(required = false)
+ public NacosDiscoveryProperties nacosDiscoveryProperties;
+
+ @PostConstruct
+ public void init(){
+ LOG.info("GrpcStartableStartedEventListener init");
+ eventBus.register(this);
+ }
+
+ /**
+ * Update grpc port to Nacos instance metadata
+ * */
+ @Subscribe
+ public void listenGrpcStartableStartedEvent(GrpcStartableStartedEvent grpcStartableStartedEvent) {
+ LOG.info("event port = {}", grpcStartableStartedEvent.getPort());
+ if(null != nacosDiscoveryProperties){
+ String grpcAddressValue = nacosDiscoveryProperties.getIp() +":" + grpcStartableStartedEvent.getPort();
+ this.nacosDiscoveryProperties.getMetadata().put(ALPHA_SERVER_GRPC_ADDRESS_KEY,grpcAddressValue);
+ LOG.info("Register grpc address {} to Nacos instance metadata",grpcAddressValue);
+ }
+ }
+}