This is an automated email from the ASF dual-hosted git repository.
xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new e86095c10 [type:feature] shenyu-register-instance-eureka development.
(#4144)
e86095c10 is described below
commit e86095c10d06239fdb681206f3b3c6dd4b72dd1c
Author: Shawn Jim <[email protected]>
AuthorDate: Fri Nov 4 17:35:04 2022 +0800
[type:feature] shenyu-register-instance-eureka development. (#4144)
* [type:feature] shenyu-register-instance-eureka development.
* [type:feature] Remove static modifier on
EurekaInstanceRegisterRepository#eurekaClient.
Co-authored-by: xiaoyu <[email protected]>
---
pom.xml | 7 ++
.../shenyu-register-instance/pom.xml | 1 +
.../{ => shenyu-register-instance-eureka}/pom.xml | 32 +++---
.../eureka/EurekaInstanceRegisterRepository.java | 115 ++++++++++++++++++++
...r.instance.api.ShenyuInstanceRegisterRepository | 17 +++
.../EurekaInstanceRegisterRepositoryTest.java | 116 +++++++++++++++++++++
6 files changed, 274 insertions(+), 14 deletions(-)
diff --git a/pom.xml b/pom.xml
index 51ab812b0..57ab50532 100644
--- a/pom.xml
+++ b/pom.xml
@@ -134,6 +134,7 @@
<elasticsearch-rest-client.version>8.2.3</elasticsearch-rest-client.version>
<kafka-clients.version>3.2.0</kafka-clients.version>
<clickhouse-http-client.version>0.3.2-patch11</clickhouse-http-client.version>
+ <eureka.version>1.10.17</eureka.version>
<!--maven plugin version-->
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
<jacoco-maven-plugin.version>0.8.7</jacoco-maven-plugin.version>
@@ -491,6 +492,12 @@
<artifactId>caffeine</artifactId>
<version>${caffeine.version}</version>
</dependency>
+
+ <dependency>
+ <groupId>com.netflix.eureka</groupId>
+ <artifactId>eureka-client</artifactId>
+ <version>${eureka.version}</version>
+ </dependency>
</dependencies>
</dependencyManagement>
diff --git a/shenyu-register-center/shenyu-register-instance/pom.xml
b/shenyu-register-center/shenyu-register-instance/pom.xml
index ee92fdd2e..e1ba19542 100644
--- a/shenyu-register-center/shenyu-register-instance/pom.xml
+++ b/shenyu-register-center/shenyu-register-instance/pom.xml
@@ -33,6 +33,7 @@
<module>shenyu-register-instance-etcd</module>
<module>shenyu-register-instance-consul</module>
<module>shenyu-register-instance-nacos</module>
+ <module>shenyu-register-instance-eureka</module>
</modules>
</project>
\ No newline at end of file
diff --git a/shenyu-register-center/shenyu-register-instance/pom.xml
b/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-eureka/pom.xml
similarity index 57%
copy from shenyu-register-center/shenyu-register-instance/pom.xml
copy to
shenyu-register-center/shenyu-register-instance/shenyu-register-instance-eureka/pom.xml
index ee92fdd2e..8d35c2cf7 100644
--- a/shenyu-register-center/shenyu-register-instance/pom.xml
+++
b/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-eureka/pom.xml
@@ -16,23 +16,27 @@
~ 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">
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.apache.shenyu</groupId>
- <artifactId>shenyu-register-center</artifactId>
+ <artifactId>shenyu-register-instance</artifactId>
<version>2.5.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
- <artifactId>shenyu-register-instance</artifactId>
- <packaging>pom</packaging>
-
- <modules>
- <module>shenyu-register-instance-api</module>
- <module>shenyu-register-instance-core</module>
- <module>shenyu-register-instance-zookeeper</module>
- <module>shenyu-register-instance-etcd</module>
- <module>shenyu-register-instance-consul</module>
- <module>shenyu-register-instance-nacos</module>
- </modules>
-
+ <artifactId>shenyu-register-instance-eureka</artifactId>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.shenyu</groupId>
+ <artifactId>shenyu-register-instance-api</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>com.netflix.eureka</groupId>
+ <artifactId>eureka-client</artifactId>
+ </dependency>
+ </dependencies>
</project>
\ No newline at end of file
diff --git
a/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-eureka/src/main/java/org/apache/shenyu/register/instance/eureka/EurekaInstanceRegisterRepository.java
b/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-eureka/src/main/java/org/apache/shenyu/register/instance/eureka/EurekaInstanceRegisterRepository.java
new file mode 100644
index 000000000..cbdcf9856
--- /dev/null
+++
b/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-eureka/src/main/java/org/apache/shenyu/register/instance/eureka/EurekaInstanceRegisterRepository.java
@@ -0,0 +1,115 @@
+/*
+ * 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.shenyu.register.instance.eureka;
+
+import com.netflix.appinfo.ApplicationInfoManager;
+import com.netflix.appinfo.DataCenterInfo;
+import com.netflix.appinfo.EurekaInstanceConfig;
+import com.netflix.appinfo.InstanceInfo;
+import com.netflix.appinfo.MyDataCenterInfo;
+import com.netflix.appinfo.MyDataCenterInstanceConfig;
+import com.netflix.appinfo.providers.EurekaConfigBasedInstanceInfoProvider;
+import com.netflix.config.ConfigurationManager;
+import com.netflix.discovery.DefaultEurekaClientConfig;
+import com.netflix.discovery.DiscoveryClient;
+import com.netflix.discovery.EurekaClient;
+import com.netflix.discovery.shared.transport.EurekaHttpClient;
+import com.netflix.discovery.shared.transport.EurekaHttpResponse;
+import com.netflix.discovery.shared.transport.jersey.JerseyApplicationClient;
+import com.sun.jersey.client.apache4.ApacheHttpClient4;
+import org.apache.shenyu.common.utils.IpUtils;
+import
org.apache.shenyu.register.instance.api.ShenyuInstanceRegisterRepository;
+import org.apache.shenyu.register.instance.api.config.RegisterConfig;
+import org.apache.shenyu.register.instance.api.entity.InstanceEntity;
+import org.apache.shenyu.register.instance.api.watcher.WatcherListener;
+import org.apache.shenyu.spi.Join;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+
+@Join
+public class EurekaInstanceRegisterRepository implements
ShenyuInstanceRegisterRepository {
+
+ private static final Logger LOGGER =
LoggerFactory.getLogger(EurekaInstanceRegisterRepository.class);
+
+ private EurekaClient eurekaClient;
+
+ private EurekaHttpClient eurekaHttpClient;
+
+ private final List<String> watcher = new ArrayList<>();
+
+ @Override
+ public void init(final RegisterConfig config) {
+
ConfigurationManager.getConfigInstance().setProperty("eureka.client.service-url.defaultZone",
config.getServerLists());
+
ConfigurationManager.getConfigInstance().setProperty("eureka.serviceUrl.default",
config.getServerLists());
+ ApplicationInfoManager applicationInfoManager =
initializeApplicationInfoManager(new MyDataCenterInstanceConfig());
+ eurekaClient = new DiscoveryClient(applicationInfoManager, new
DefaultEurekaClientConfig());
+ eurekaHttpClient = new JerseyApplicationClient(new
ApacheHttpClient4(), config.getServerLists(), null);
+ }
+
+ private ApplicationInfoManager initializeApplicationInfoManager(final
EurekaInstanceConfig instanceConfig) {
+ InstanceInfo instanceInfo = new
EurekaConfigBasedInstanceInfoProvider(instanceConfig).get();
+ return new ApplicationInfoManager(instanceConfig, instanceInfo);
+ }
+
+ @Override
+ public void persistInstance(final InstanceEntity instance) {
+ EurekaHttpResponse<Void> register =
eurekaHttpClient.register(generateInstanceInfo(instance));
+ LOGGER.info("eureka client register success: {}",
register.getEntity());
+ }
+
+ private InstanceInfo generateInstanceInfo(final InstanceEntity instance) {
+ return InstanceInfo.Builder.newBuilder()
+ .setAppName(instance.getAppName())
+ .setIPAddr(IpUtils.getHost())
+ .setHostName(instance.getHost())
+ .setPort(instance.getPort())
+ .setDataCenterInfo(new
MyDataCenterInfo(DataCenterInfo.Name.MyOwn))
+ .build();
+ }
+
+ @Override
+ public List<InstanceEntity> selectInstancesAndWatcher(final String
selectKey, final WatcherListener watcherListener) {
+ if (!watcher.contains(selectKey)) {
+ synchronized (this) {
+ if (!watcher.contains(selectKey)) {
+ watcher.add(selectKey);
+ eurekaClient.registerEventListener(event ->
watcherListener.listener(getInstances(selectKey)));
+ }
+ }
+ }
+ return getInstances(selectKey);
+ }
+
+ private List<InstanceEntity> getInstances(final String selectKey) {
+ List<InstanceInfo> instances =
eurekaClient.getInstancesByVipAddressAndAppName(null, selectKey, true);
+ return instances.stream()
+ .map(i -> InstanceEntity.builder()
+
.appName(i.getAppName()).host(i.getHostName()).port(i.getPort())
+ .build()
+ ).collect(Collectors.toList());
+ }
+
+ @Override
+ public void close() {
+ eurekaClient.shutdown();
+ }
+}
diff --git
a/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-eureka/src/main/resources/META-INF/shenyu/org.apache.shenyu.register.instance.api.ShenyuInstanceRegisterRepository
b/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-eureka/src/main/resources/META-INF/shenyu/org.apache.shenyu.register.instance.api.ShenyuInstanceRegisterRepository
new file mode 100644
index 000000000..bb12f6d98
--- /dev/null
+++
b/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-eureka/src/main/resources/META-INF/shenyu/org.apache.shenyu.register.instance.api.ShenyuInstanceRegisterRepository
@@ -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.
+
+eureka=org.apache.shenyu.register.instance.eureka.EurekaInstanceRegisterRepository
diff --git
a/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-eureka/src/test/java/org/apache/shenyu/register/instance/eureka/EurekaInstanceRegisterRepositoryTest.java
b/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-eureka/src/test/java/org/apache/shenyu/register/instance/eureka/EurekaInstanceRegisterRepositoryTest.java
new file mode 100644
index 000000000..3c590143d
--- /dev/null
+++
b/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-eureka/src/test/java/org/apache/shenyu/register/instance/eureka/EurekaInstanceRegisterRepositoryTest.java
@@ -0,0 +1,116 @@
+/*
+ * 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.shenyu.register.instance.eureka;
+
+import com.netflix.appinfo.InstanceInfo;
+import com.netflix.discovery.DiscoveryClient;
+import com.netflix.discovery.EurekaClient;
+import com.netflix.discovery.EurekaEventListener;
+import com.netflix.discovery.shared.transport.EurekaHttpClient;
+import com.netflix.discovery.shared.transport.EurekaHttpResponse;
+import com.netflix.discovery.shared.transport.jersey.JerseyApplicationClient;
+import org.apache.shenyu.register.instance.api.entity.InstanceEntity;
+import org.apache.shenyu.register.instance.api.watcher.WatcherListener;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.Field;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+
+public final class EurekaInstanceRegisterRepositoryTest {
+
+ private EurekaInstanceRegisterRepository repository;
+
+ private final String instanceId = "shenyu-instances";
+
+ private final Map<String, InstanceInfo> instanceStorage = new HashMap<>();
+
+ private final Map<String, EurekaEventListener> eurekaEventStorage = new
HashMap<>();
+
+ @BeforeEach
+ public void setUp() throws Exception {
+ repository = new EurekaInstanceRegisterRepository();
+ Class<? extends EurekaInstanceRegisterRepository> clazz =
repository.getClass();
+
+ Field eurekaClientField = clazz.getDeclaredField("eurekaClient");
+ eurekaClientField.setAccessible(true);
+ eurekaClientField.set(repository, mockEurekaClient());
+
+ Field eurekaHttpClientField =
clazz.getDeclaredField("eurekaHttpClient");
+ eurekaHttpClientField.setAccessible(true);
+ eurekaHttpClientField.set(repository, mockEurekaHttpClient());
+ }
+
+ private EurekaClient mockEurekaClient() {
+ DiscoveryClient discoveryClient = mock(DiscoveryClient.class);
+
+ doAnswer(invocationOnMock -> {
+ eurekaEventStorage.put(instanceId,
invocationOnMock.getArgument(0));
+ return null;
+ }).when(discoveryClient).registerEventListener(any());
+
+ doAnswer(invocationOnMock -> {
+ eurekaEventStorage.clear();
+ return null;
+ }).when(discoveryClient).shutdown();
+
+ return discoveryClient;
+ }
+
+ private EurekaHttpClient mockEurekaHttpClient() {
+ EurekaHttpClient eurekaHttpClient =
mock(JerseyApplicationClient.class);
+ doAnswer(invocationOnMock -> {
+ InstanceInfo instanceInfo = invocationOnMock.getArgument(0);
+ instanceStorage.put(instanceInfo.getAppName(), instanceInfo);
+ return EurekaHttpResponse.anEurekaHttpResponse(204, "response
success")
+ .build();
+ }).when(eurekaHttpClient).register(any());
+ return eurekaHttpClient;
+ }
+
+ @Test
+ public void persistInstance() {
+ InstanceEntity data = InstanceEntity.builder()
+ .appName(instanceId)
+ .host("shenyu-host")
+ .port(9195)
+ .build();
+ repository.persistInstance(data);
+
assertTrue(instanceStorage.containsKey(data.getAppName().toUpperCase()));
+ InstanceInfo instanceInfo =
instanceStorage.get(data.getAppName().toUpperCase());
+ assertEquals(data.getHost(), instanceInfo.getHostName());
+ assertEquals(data.getPort(), instanceInfo.getPort());
+ assertEquals(data.getAppName().toUpperCase(),
instanceInfo.getAppName());
+ }
+
+ @Test
+ public void testSelectInstancesAndWatcher() {
+ WatcherListener watcherListener = mock(WatcherListener.class);
+ repository.selectInstancesAndWatcher(instanceId, watcherListener);
+ assertTrue(eurekaEventStorage.containsKey(instanceId));
+ repository.close();
+ assertTrue(eurekaEventStorage.isEmpty());
+ }
+}