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 f6316506f Apollo sync discoveryData (#4793)
f6316506f is described below
commit f6316506f3ad56977946ce476769aecc3eb7478b
Author: Misaya295 <[email protected]>
AuthorDate: Tue Jul 4 19:00:48 2023 +0800
Apollo sync discoveryData (#4793)
Co-authored-by: xiaoyu <[email protected]>
---
.../listener/AbstractListDataChangedListener.java | 18 ++++++++++-
.../listener/apollo/ApolloDataChangedListener.java | 9 ++++--
.../listener/consul/ConsulDataChangedListener.java | 2 +-
.../listener/nacos/NacosDataChangedListener.java | 2 +-
.../common/constant/ApolloPathConstants.java | 5 +++
.../shenyu/common/constant/ConsulConstants.java | 5 +++
.../shenyu/common/constant/NacosPathConstants.java | 5 +++
.../data/apollo/ApolloSyncDataConfiguration.java | 8 +++--
.../shenyu/sync/data/apollo/ApolloDataService.java | 36 ++++++++++++++++++----
9 files changed, 77 insertions(+), 13 deletions(-)
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/listener/AbstractListDataChangedListener.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/listener/AbstractListDataChangedListener.java
index c8aac51eb..b26904db9 100644
---
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/listener/AbstractListDataChangedListener.java
+++
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/listener/AbstractListDataChangedListener.java
@@ -415,6 +415,11 @@ public abstract class AbstractListDataChangedListener
implements DataChangedList
*/
private final String proxySelectorDataId;
+ /**
+ * discovery data id.
+ */
+ private final String discoveryDataId;
+
/**
* ChangeData.
*
@@ -426,13 +431,14 @@ public abstract class AbstractListDataChangedListener
implements DataChangedList
*/
public ChangeData(final String pluginDataId, final String
selectorDataId,
final String ruleDataId, final String authDataId,
- final String metaDataId, final String
proxySelectorDataId) {
+ final String metaDataId, final String
proxySelectorDataId, final String discoveryDataId) {
this.pluginDataId = pluginDataId;
this.selectorDataId = selectorDataId;
this.ruleDataId = ruleDataId;
this.authDataId = authDataId;
this.metaDataId = metaDataId;
this.proxySelectorDataId = proxySelectorDataId;
+ this.discoveryDataId = discoveryDataId;
}
/**
@@ -488,6 +494,16 @@ public abstract class AbstractListDataChangedListener
implements DataChangedList
public String getProxySelectorDataId() {
return proxySelectorDataId;
}
+
+ /**
+ * get discoveryDataId.
+ *
+ * @return discoveryDataId
+ */
+ public String getDiscoveryDataId() {
+ return discoveryDataId;
+ }
+
}
}
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/listener/apollo/ApolloDataChangedListener.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/listener/apollo/ApolloDataChangedListener.java
index d111563ca..87353e174 100644
---
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/listener/apollo/ApolloDataChangedListener.java
+++
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/listener/apollo/ApolloDataChangedListener.java
@@ -35,8 +35,13 @@ public class ApolloDataChangedListener extends
AbstractListDataChangedListener {
* @param apolloClient the apollo client
*/
public ApolloDataChangedListener(final ApolloClient apolloClient) {
- super(new ChangeData(ApolloPathConstants.PLUGIN_DATA_ID,
ApolloPathConstants.SELECTOR_DATA_ID,
- ApolloPathConstants.RULE_DATA_ID,
ApolloPathConstants.AUTH_DATA_ID, ApolloPathConstants.META_DATA_ID,
ApolloPathConstants.PROXY_SELECTOR_DATA_ID));
+ super(new ChangeData(ApolloPathConstants.PLUGIN_DATA_ID,
+ ApolloPathConstants.SELECTOR_DATA_ID,
+ ApolloPathConstants.RULE_DATA_ID,
+ ApolloPathConstants.AUTH_DATA_ID,
+ ApolloPathConstants.META_DATA_ID,
+ ApolloPathConstants.PROXY_SELECTOR_DATA_ID,
+ ApolloPathConstants.DISCOVERY_DATA_ID));
this.apolloClient = apolloClient;
}
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/listener/consul/ConsulDataChangedListener.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/listener/consul/ConsulDataChangedListener.java
index 8619e6d47..a35bdc151 100644
---
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/listener/consul/ConsulDataChangedListener.java
+++
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/listener/consul/ConsulDataChangedListener.java
@@ -34,7 +34,7 @@ public class ConsulDataChangedListener extends
AbstractListDataChangedListener {
public ConsulDataChangedListener(final ConsulClient consulClient) {
super(new ChangeData(ConsulConstants.PLUGIN_DATA,
ConsulConstants.SELECTOR_DATA,
- ConsulConstants.RULE_DATA, ConsulConstants.AUTH_DATA,
ConsulConstants.META_DATA, ConsulConstants.PROXY_SELECTOR_DATA_ID));
+ ConsulConstants.RULE_DATA, ConsulConstants.AUTH_DATA,
ConsulConstants.META_DATA, ConsulConstants.PROXY_SELECTOR_DATA_ID,
ConsulConstants.DISCOVERY_UPSTREAM));
this.consulClient = consulClient;
}
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/listener/nacos/NacosDataChangedListener.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/listener/nacos/NacosDataChangedListener.java
index aea11f44f..589eb206d 100644
---
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/listener/nacos/NacosDataChangedListener.java
+++
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/listener/nacos/NacosDataChangedListener.java
@@ -39,7 +39,7 @@ public class NacosDataChangedListener extends
AbstractListDataChangedListener {
public NacosDataChangedListener(final ConfigService configService) {
super(new ChangeData(NacosPathConstants.PLUGIN_DATA_ID,
NacosPathConstants.SELECTOR_DATA_ID,
- NacosPathConstants.RULE_DATA_ID,
NacosPathConstants.AUTH_DATA_ID, NacosPathConstants.META_DATA_ID,
NacosPathConstants.PROXY_SELECTOR_DATA_ID));
+ NacosPathConstants.RULE_DATA_ID,
NacosPathConstants.AUTH_DATA_ID, NacosPathConstants.META_DATA_ID,
NacosPathConstants.PROXY_SELECTOR_DATA_ID,
NacosPathConstants.DISCOVERY_DATA_ID));
this.configService = configService;
}
diff --git
a/shenyu-common/src/main/java/org/apache/shenyu/common/constant/ApolloPathConstants.java
b/shenyu-common/src/main/java/org/apache/shenyu/common/constant/ApolloPathConstants.java
index 603eea105..038c63029 100644
---
a/shenyu-common/src/main/java/org/apache/shenyu/common/constant/ApolloPathConstants.java
+++
b/shenyu-common/src/main/java/org/apache/shenyu/common/constant/ApolloPathConstants.java
@@ -61,6 +61,11 @@ public class ApolloPathConstants {
*/
public static final String PROXY_SELECTOR_DATA_ID =
"shenyu.proxy.selector.json";
+ /**
+ * proxy selector data id.
+ */
+ public static final String DISCOVERY_DATA_ID =
"shenyu.discovery.data.json";
+
/**
* register metadata id.
*/
diff --git
a/shenyu-common/src/main/java/org/apache/shenyu/common/constant/ConsulConstants.java
b/shenyu-common/src/main/java/org/apache/shenyu/common/constant/ConsulConstants.java
index 7e2a5dcaa..2f4c60e0c 100644
---
a/shenyu-common/src/main/java/org/apache/shenyu/common/constant/ConsulConstants.java
+++
b/shenyu-common/src/main/java/org/apache/shenyu/common/constant/ConsulConstants.java
@@ -54,6 +54,11 @@ public final class ConsulConstants {
*/
public static final String PROXY_SELECTOR_DATA_ID = SYNC_PRE_FIX +
"/proxySelector";
+ /**
+ * The constant DISCOVERY_UPSTREAM.
+ */
+ public static final String DISCOVERY_UPSTREAM = SYNC_PRE_FIX +
"/discoveryUpstream";
+
/**
* default value of get config.
*/
diff --git
a/shenyu-common/src/main/java/org/apache/shenyu/common/constant/NacosPathConstants.java
b/shenyu-common/src/main/java/org/apache/shenyu/common/constant/NacosPathConstants.java
index 28993e32e..b052fe2a3 100644
---
a/shenyu-common/src/main/java/org/apache/shenyu/common/constant/NacosPathConstants.java
+++
b/shenyu-common/src/main/java/org/apache/shenyu/common/constant/NacosPathConstants.java
@@ -57,6 +57,11 @@ public final class NacosPathConstants {
*/
public static final String PROXY_SELECTOR_DATA_ID =
"shenyu.proxy.selector.json";
+ /**
+ * proxy selector data id.
+ */
+ public static final String DISCOVERY_DATA_ID =
"shenyu.discovery.data.json";
+
/**
* default value of get config.
*/
diff --git
a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-sync-data-center/shenyu-spring-boot-starter-sync-data-apollo/src/main/java/org/apache/shenyu/springboot/sync/data/apollo/ApolloSyncDataConfiguration.java
b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-sync-data-center/shenyu-spring-boot-starter-sync-data-apollo/src/main/java/org/apache/shenyu/springboot/sync/data/apollo/ApolloSyncDataConfiguration.java
index 4076ebf50..9b27b8fde 100644
---
a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-sync-data-center/shenyu-spring-boot-starter-sync-data-apollo/src/main/java/org/apache/shenyu/springboot/sync/data/apollo/ApolloSyncDataConfiguration.java
+++
b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-sync-data-center/shenyu-spring-boot-starter-sync-data-apollo/src/main/java/org/apache/shenyu/springboot/sync/data/apollo/ApolloSyncDataConfiguration.java
@@ -23,6 +23,7 @@ import org.apache.shenyu.sync.data.api.AuthDataSubscriber;
import org.apache.shenyu.sync.data.api.MetaDataSubscriber;
import org.apache.shenyu.sync.data.api.PluginDataSubscriber;
import org.apache.shenyu.sync.data.api.ProxySelectorDataSubscriber;
+import org.apache.shenyu.sync.data.api.DiscoveryUpstreamDataSubscriber;
import org.apache.shenyu.sync.data.apollo.ApolloDataService;
import org.apache.shenyu.sync.data.apollo.config.ApolloConfig;
import org.slf4j.Logger;
@@ -65,16 +66,19 @@ public class ApolloSyncDataConfiguration {
* @param metaSubscribers the meta subscribers
* @param authSubscribers the auth subscribers
* @param proxySelectorDataSubscriber the proxySelector subscribers
+ * @param discoveryUpstreamDataSubscribers the discoveryUpstream
subscribers
* @return the apollo config
*/
@Bean
public ApolloDataService apolloSyncDataService(final
ObjectProvider<Config> configService, final
ObjectProvider<PluginDataSubscriber> pluginSubscriber,
final
ObjectProvider<List<MetaDataSubscriber>> metaSubscribers,
final
ObjectProvider<List<AuthDataSubscriber>> authSubscribers,
- final
ObjectProvider<List<ProxySelectorDataSubscriber>> proxySelectorDataSubscriber) {
+ final
ObjectProvider<List<ProxySelectorDataSubscriber>> proxySelectorDataSubscriber,
+ final
ObjectProvider<List<DiscoveryUpstreamDataSubscriber>>
discoveryUpstreamDataSubscribers) {
LOGGER.info("you use apollo sync shenyu data.......");
return new ApolloDataService(configService.getIfAvailable(),
pluginSubscriber.getIfAvailable(),
- metaSubscribers.getIfAvailable(Collections::emptyList),
authSubscribers.getIfAvailable(Collections::emptyList),
proxySelectorDataSubscriber.getIfAvailable(Collections::emptyList));
+ metaSubscribers.getIfAvailable(Collections::emptyList),
authSubscribers.getIfAvailable(Collections::emptyList),
proxySelectorDataSubscriber.getIfAvailable(Collections::emptyList),
+ discoveryUpstreamDataSubscribers.getIfAvailable());
}
/**
diff --git
a/shenyu-sync-data-center/shenyu-sync-data-apollo/src/main/java/org/apache/shenyu/sync/data/apollo/ApolloDataService.java
b/shenyu-sync-data-center/shenyu-sync-data-apollo/src/main/java/org/apache/shenyu/sync/data/apollo/ApolloDataService.java
index a1381ca01..570b2958b 100644
---
a/shenyu-sync-data-center/shenyu-sync-data-apollo/src/main/java/org/apache/shenyu/sync/data/apollo/ApolloDataService.java
+++
b/shenyu-sync-data-center/shenyu-sync-data-apollo/src/main/java/org/apache/shenyu/sync/data/apollo/ApolloDataService.java
@@ -20,25 +20,28 @@ package org.apache.shenyu.sync.data.apollo;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigChangeListener;
import org.apache.shenyu.common.constant.ApolloPathConstants;
-import org.apache.shenyu.common.dto.AppAuthData;
import org.apache.shenyu.common.dto.MetaData;
import org.apache.shenyu.common.dto.PluginData;
-import org.apache.shenyu.common.dto.RuleData;
import org.apache.shenyu.common.dto.SelectorData;
+import org.apache.shenyu.common.dto.RuleData;
+import org.apache.shenyu.common.dto.AppAuthData;
import org.apache.shenyu.common.dto.ProxySelectorData;
+import org.apache.shenyu.common.dto.DiscoverySyncData;
import org.apache.shenyu.common.utils.GsonUtils;
import org.apache.shenyu.sync.data.api.AuthDataSubscriber;
import org.apache.shenyu.sync.data.api.MetaDataSubscriber;
import org.apache.shenyu.sync.data.api.PluginDataSubscriber;
-import org.apache.shenyu.sync.data.api.ProxySelectorDataSubscriber;
import org.apache.shenyu.sync.data.api.SyncDataService;
+import org.apache.shenyu.sync.data.api.ProxySelectorDataSubscriber;
+import org.apache.shenyu.sync.data.api.DiscoveryUpstreamDataSubscriber;
+
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.util.ArrayList;
-import java.util.Collection;
import java.util.List;
import java.util.Map;
+import java.util.Collection;
+import java.util.ArrayList;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
@@ -60,6 +63,8 @@ public class ApolloDataService implements SyncDataService {
private final List<ProxySelectorDataSubscriber>
proxySelectorDataSubscribers;
+ private final List<DiscoveryUpstreamDataSubscriber>
discoveryUpstreamDataSubscribers;
+
private final Map<String, ConfigChangeListener> cache = new
ConcurrentHashMap<>();
/**
@@ -73,12 +78,14 @@ public class ApolloDataService implements SyncDataService {
public ApolloDataService(final Config configService, final
PluginDataSubscriber pluginDataSubscriber,
final List<MetaDataSubscriber>
metaDataSubscribers,
final List<AuthDataSubscriber>
authDataSubscribers,
- final List<ProxySelectorDataSubscriber>
proxySelectorDataSubscribers) {
+ final List<ProxySelectorDataSubscriber>
proxySelectorDataSubscribers,
+ final List<DiscoveryUpstreamDataSubscriber>
discoveryUpstreamDataSubscribers) {
this.configService = configService;
this.pluginDataSubscriber = pluginDataSubscriber;
this.metaDataSubscribers = metaDataSubscribers;
this.authDataSubscribers = authDataSubscribers;
this.proxySelectorDataSubscribers = proxySelectorDataSubscribers;
+ this.discoveryUpstreamDataSubscribers =
discoveryUpstreamDataSubscribers;
subAllData();
watchData();
@@ -178,6 +185,19 @@ public class ApolloDataService implements SyncDataService {
return proxySelectorDataList;
}
+ /**
+ * subscriber discovery sync data.
+ * @return discovery sync data list
+ */
+ public List<DiscoverySyncData> subscriberDiscoverySyncData() {
+ List<DiscoverySyncData> discoverySyncDataList = new
ArrayList<>(GsonUtils.getInstance().toObjectMap(configService.getProperty(ApolloPathConstants.DISCOVERY_DATA_ID,
"{}"),
+ DiscoverySyncData.class).values());
+ discoverySyncDataList.forEach(discoverySyncData ->
discoveryUpstreamDataSubscribers.forEach(subscriber -> {
+ subscriber.unSubscribe(discoverySyncData);
+ }));
+ return discoverySyncDataList;
+ }
+
/**
* watch plugin data.
*/
@@ -208,6 +228,10 @@ public class ApolloDataService implements SyncDataService {
List<ProxySelectorData> proxySelectorData =
subscriberProxySelectorData();
LOG.info("apollo listener ProxySelectorData: {}",
proxySelectorData);
break;
+ case ApolloPathConstants.DISCOVERY_DATA_ID:
+ List<DiscoverySyncData> discoverySyncData =
subscriberDiscoverySyncData();
+ LOG.info("apollo listener discoverySyncData: {}",
discoverySyncData);
+ break;
default:
break;
}