You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by he...@apache.org on 2023/10/11 05:28:15 UTC
[shenyu] branch master updated: [type:feature] websocket discovery (#5202)
This is an automated email from the ASF dual-hosted git repository.
hefengen 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 892cbeefae [type:feature] websocket discovery (#5202)
892cbeefae is described below
commit 892cbeefae617c5dc9a29f9f83108137807bb17c
Author: Misaya295 <45...@users.noreply.github.com>
AuthorDate: Wed Oct 11 13:28:08 2023 +0800
[type:feature] websocket discovery (#5202)
* websocket discovery
* zk ci fix
---------
Co-authored-by: moremind <he...@apache.org>
---
.../docker-compose.yml | 2 +
.../handler/WebSocketUpstreamDataHandler.java | 66 ++++++++++++++++++++++
.../websocket/WebSocketPluginConfiguration.java | 11 ++++
3 files changed, 79 insertions(+)
diff --git a/shenyu-integrated-test/shenyu-integrated-test-combination/docker-compose.yml b/shenyu-integrated-test/shenyu-integrated-test-combination/docker-compose.yml
index 23acd633c8..03093a6e72 100644
--- a/shenyu-integrated-test/shenyu-integrated-test-combination/docker-compose.yml
+++ b/shenyu-integrated-test/shenyu-integrated-test-combination/docker-compose.yml
@@ -192,6 +192,8 @@ services:
environment:
- shenyu.sync.websocket.urls=ws://shenyu-admin:9095/websocket
depends_on:
+ shenyu-zk:
+ condition: service_healthy
shenyu-admin:
condition: service_healthy
shenyu-zk:
diff --git a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-websocket/src/main/java/org/apache/shenyu/plugin/websocket/handler/WebSocketUpstreamDataHandler.java b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-websocket/src/main/java/org/apache/shenyu/plugin/websocket/handler/WebSocketUpstreamDataHandler.java
new file mode 100644
index 0000000000..0f54bcac4b
--- /dev/null
+++ b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-websocket/src/main/java/org/apache/shenyu/plugin/websocket/handler/WebSocketUpstreamDataHandler.java
@@ -0,0 +1,66 @@
+/*
+ * 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.plugin.websocket.handler;
+
+import org.apache.shenyu.common.dto.DiscoverySyncData;
+import org.apache.shenyu.common.dto.DiscoveryUpstreamData;
+import org.apache.shenyu.common.enums.PluginEnum;
+import org.apache.shenyu.loadbalancer.cache.UpstreamCacheManager;
+import org.apache.shenyu.loadbalancer.entity.Upstream;
+import org.apache.shenyu.plugin.base.cache.MetaDataCache;
+import org.apache.shenyu.plugin.base.handler.DiscoveryUpstreamDataHandler;
+import org.springframework.util.ObjectUtils;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Collectors;
+
+/*
+ * upstreamList data change.
+ */
+public class WebSocketUpstreamDataHandler implements DiscoveryUpstreamDataHandler {
+
+ @Override
+ public void handlerDiscoveryUpstreamData(final DiscoverySyncData discoverySyncData) {
+ if (Objects.isNull(discoverySyncData) || Objects.isNull(discoverySyncData.getSelectorId())) {
+ return;
+ }
+ List<DiscoveryUpstreamData> upstreamList = discoverySyncData.getUpstreamDataList();
+ UpstreamCacheManager.getInstance().submit(discoverySyncData.getSelectorId(), convertUpstreamList(upstreamList));
+ MetaDataCache.getInstance().clean();
+ }
+
+ private List<Upstream> convertUpstreamList(final List<DiscoveryUpstreamData> upstreamList) {
+ if (ObjectUtils.isEmpty(upstreamList)) {
+ return Collections.emptyList();
+ }
+ return upstreamList.stream().map(u -> Upstream.builder()
+ .protocol(u.getProtocol())
+ .url(u.getUrl())
+ .weight(u.getWeight())
+ .status(0 == u.getStatus())
+ .timestamp(u.getDateUpdated().getTime())
+ .build()).collect(Collectors.toList());
+ }
+
+ @Override
+ public String pluginName() {
+ return PluginEnum.WEB_SOCKET.getName();
+ }
+}
diff --git a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-websocket/src/main/java/org/apache/shenyu/springboot/plugin/websocket/WebSocketPluginConfiguration.java b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-websocket/src/main/java/org/apache/shenyu/springboot/plugin/websocket/WebSocketPluginConfiguration.java
index 4955b6821e..9a90a9e16a 100644
--- a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-websocket/src/main/java/org/apache/shenyu/springboot/plugin/websocket/WebSocketPluginConfiguration.java
+++ b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-websocket/src/main/java/org/apache/shenyu/springboot/plugin/websocket/WebSocketPluginConfiguration.java
@@ -23,6 +23,7 @@ import org.apache.shenyu.plugin.base.handler.PluginDataHandler;
import org.apache.shenyu.plugin.websocket.WebSocketPlugin;
import org.apache.shenyu.plugin.websocket.context.WebSocketShenyuContextDecorator;
import org.apache.shenyu.plugin.websocket.handler.WebSocketPluginDataHandler;
+import org.apache.shenyu.plugin.websocket.handler.WebSocketUpstreamDataHandler;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
@@ -91,6 +92,16 @@ public class WebSocketPluginConfiguration {
return new HandshakeWebSocketService();
}
+ /**
+ * websocket discovery upstream data handler.
+ *
+ * @return the websocket upstream data handler
+ */
+ @Bean
+ public WebSocketUpstreamDataHandler divideUpstreamDataHandler() {
+ return new WebSocketUpstreamDataHandler();
+ }
+
/**
* Web socket shenyu context decorator.
*