You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by sa...@apache.org on 2021/08/24 08:45:36 UTC

[incubator-shenyu] branch master updated: [new feature] add shenyu can update local data (#1969)

This is an automated email from the ASF dual-hosted git repository.

sabersola pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new 1e42643  [new feature] add shenyu can update local data (#1969)
1e42643 is described below

commit 1e42643fd7491ca01e28b465b7291364242a4d66
Author: xiaoyu <xi...@apache.org>
AuthorDate: Tue Aug 24 16:45:25 2021 +0800

    [new feature] add shenyu can update local data (#1969)
    
    * [new feature] add shenyu can update local data
    
    * [new feature] add shenyu can update local data
---
 .../src/main/resources/application-local.yml       |   2 +
 .../web/configuration/ShenyuConfiguration.java     |  14 +++
 .../shenyu/web/controller/PluginController.java    | 117 +++++++++++++++++++++
 .../shenyu/web/filter/LocalDispatcherFilter.java   |  61 +++++++++++
 4 files changed, 194 insertions(+)

diff --git a/shenyu-bootstrap/src/main/resources/application-local.yml b/shenyu-bootstrap/src/main/resources/application-local.yml
index cd664e8..985ad84 100644
--- a/shenyu-bootstrap/src/main/resources/application-local.yml
+++ b/shenyu-bootstrap/src/main/resources/application-local.yml
@@ -46,6 +46,8 @@ management:
       enabled: false
 
 shenyu:
+  local:
+    enabled: true
   file:
     enabled: true
   cross:
diff --git a/shenyu-web/src/main/java/org/apache/shenyu/web/configuration/ShenyuConfiguration.java b/shenyu-web/src/main/java/org/apache/shenyu/web/configuration/ShenyuConfiguration.java
index 4cedcfc..d06c0ee 100644
--- a/shenyu-web/src/main/java/org/apache/shenyu/web/configuration/ShenyuConfiguration.java
+++ b/shenyu-web/src/main/java/org/apache/shenyu/web/configuration/ShenyuConfiguration.java
@@ -28,6 +28,7 @@ import org.apache.shenyu.web.configuration.properties.ShenyuConfig;
 import org.apache.shenyu.web.filter.CrossFilter;
 import org.apache.shenyu.web.filter.ExcludeFilter;
 import org.apache.shenyu.web.filter.FileSizeFilter;
+import org.apache.shenyu.web.filter.LocalDispatcherFilter;
 import org.apache.shenyu.web.filter.TimeWebFilter;
 import org.apache.shenyu.web.filter.WebSocketParamFilter;
 import org.apache.shenyu.web.forward.ForwardedRemoteAddressResolver;
@@ -139,6 +140,19 @@ public class ShenyuConfiguration {
     }
     
     /**
+     * Local dispatcher filter web filter.
+     *
+     * @param dispatcherHandler the dispatcher handler
+     * @return the web filter
+     */
+    @Bean
+    @Order(-200)
+    @ConditionalOnProperty(name = "shenyu.local.enabled", havingValue = "true", matchIfMissing = true)
+    public WebFilter localDispatcherFilter(final DispatcherHandler dispatcherHandler) {
+        return new LocalDispatcherFilter(dispatcherHandler);
+    }
+    
+    /**
      * Cross filter web filter.
      * if you application has cross-domain.
      * this is demo.
diff --git a/shenyu-web/src/main/java/org/apache/shenyu/web/controller/PluginController.java b/shenyu-web/src/main/java/org/apache/shenyu/web/controller/PluginController.java
new file mode 100644
index 0000000..a474198
--- /dev/null
+++ b/shenyu-web/src/main/java/org/apache/shenyu/web/controller/PluginController.java
@@ -0,0 +1,117 @@
+/*
+ * 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.web.controller;
+
+import org.apache.shenyu.common.dto.PluginData;
+import org.apache.shenyu.plugin.base.cache.BaseDataCache;
+import org.apache.shenyu.sync.data.api.PluginDataSubscriber;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+import reactor.core.publisher.Mono;
+
+/**
+ * The type Plugin controller.
+ */
+@RestController
+@RequestMapping("/shenyu")
+public class PluginController {
+    
+    /**
+     * logger.
+     */
+    private static final Logger LOG = LoggerFactory.getLogger(PluginController.class);
+    
+    private final PluginDataSubscriber subscriber;
+    
+    /**
+     * Instantiates a new Plugin controller.
+     *
+     * @param subscriber the subscriber
+     */
+    public PluginController(final PluginDataSubscriber subscriber) {
+        this.subscriber = subscriber;
+    }
+    
+    /**
+     * Clean all mono.
+     *
+     * @return the mono
+     */
+    @GetMapping("/cleanAll")
+    public Mono<String> cleanAll() {
+        subscriber.refreshPluginDataAll();
+        subscriber.refreshSelectorDataAll();
+        subscriber.refreshRuleDataAll();
+        return Mono.just("success");
+    }
+    
+    /**
+     * Add plugin string.
+     *
+     * @param pluginData the plugin data
+     * @return the string
+     */
+    @PostMapping("/plugin/saveOrUpdate")
+    public Mono<String> saveOrUpdate(final PluginData pluginData) {
+        LOG.info("saveOrUpdate apache shenyu local plugin");
+        subscriber.onSubscribe(pluginData);
+        return Mono.just("success");
+    }
+    
+    /**
+     * Delete mono.
+     *
+     * @param name the name
+     * @return the mono
+     */
+    @GetMapping("/plugin/delete")
+    public Mono<String> delete(@RequestParam("name") final String name) {
+        LOG.info("delete apache shenyu local plugin");
+        PluginData pluginData = PluginData.builder().name(name).build();
+        subscriber.unSubscribe(pluginData);
+        return Mono.just("success");
+    }
+    
+    /**
+     * Delete all mono.
+     *
+     * @return the mono
+     */
+    @GetMapping("/plugin/deleteAll")
+    public Mono<String> deleteAll() {
+        subscriber.refreshPluginDataAll();
+        return Mono.just("success");
+    }
+    
+    /**
+     * Find by name mono.
+     *
+     * @param name the name
+     * @return the mono
+     */
+    @GetMapping("/plugin/findByName")
+    public Mono<PluginData> findByName(@RequestParam("name") final String name) {
+        PluginData pluginData = BaseDataCache.getInstance().obtainPluginData(name);
+        return Mono.just(pluginData);
+    }
+}
diff --git a/shenyu-web/src/main/java/org/apache/shenyu/web/filter/LocalDispatcherFilter.java b/shenyu-web/src/main/java/org/apache/shenyu/web/filter/LocalDispatcherFilter.java
new file mode 100644
index 0000000..437b024
--- /dev/null
+++ b/shenyu-web/src/main/java/org/apache/shenyu/web/filter/LocalDispatcherFilter.java
@@ -0,0 +1,61 @@
+/*
+ * 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.web.filter;
+
+import org.apache.shenyu.common.utils.PathMatchUtils;
+import org.springframework.web.reactive.DispatcherHandler;
+import org.springframework.web.server.ServerWebExchange;
+import org.springframework.web.server.WebFilter;
+import org.springframework.web.server.WebFilterChain;
+import reactor.core.publisher.Mono;
+
+/**
+ * The type Local dispatcher filter.
+ */
+public class LocalDispatcherFilter implements WebFilter {
+    
+    private static final String DISPATCHER_PATH = "/shenyu/**";
+
+    private final DispatcherHandler dispatcherHandler;
+    
+    /**
+     * Instantiates a new Local dispatcher filter.
+     *
+     * @param dispatcherHandler the dispatcher handler
+     */
+    public LocalDispatcherFilter(final DispatcherHandler dispatcherHandler) {
+        this.dispatcherHandler = dispatcherHandler;
+    }
+    
+    /**
+     * Process the Web request and (optionally) delegate to the next
+     * {@code WebFilter} through the given {@link WebFilterChain}.
+     *
+     * @param exchange the current server exchange
+     * @param chain provides a way to delegate to the next filter
+     * @return {@code Mono<Void>} to indicate when request processing is complete
+     */
+    @Override
+    public Mono<Void> filter(final ServerWebExchange exchange, final WebFilterChain chain) {
+        String path = exchange.getRequest().getURI().getPath();
+        if (PathMatchUtils.match(DISPATCHER_PATH, path)) {
+            return dispatcherHandler.handle(exchange);
+        }
+        return chain.filter(exchange);
+    }
+}