You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by zh...@apache.org on 2023/01/07 06:44:16 UTC

[shenyu] branch master updated: shenyu client api doc part2 admin module (#4299)

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

zhangzicheng 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 868afc13e shenyu client api doc part2 admin module (#4299)
868afc13e is described below

commit 868afc13ebc39c59cf9f7e08f76bd1a2ed53b561
Author: zhengpeng <84...@qq.com>
AuthorDate: Sat Jan 7 14:44:06 2023 +0800

    shenyu client api doc part2 admin module (#4299)
---
 .../ShenyuClientHttpRegistryController.java        | 13 +++++
 .../RegisterClientServerDisruptorPublisher.java    |  2 +
 .../executor/RegisterServerConsumerExecutor.java   |  1 -
 .../subscriber/ApiDocExecutorSubscriber.java       | 56 ++++++++++++++++++++++
 .../org/apache/shenyu/admin/mapper/ApiMapper.java  | 17 +++++++
 .../apache/shenyu/admin/service/ApiService.java    | 16 +++++++
 .../shenyu/admin/service/impl/ApiServiceImpl.java  | 21 ++++++++
 .../AbstractShenyuClientRegisterServiceImpl.java   | 42 +++++++++++++++-
 .../register/ShenyuClientRegisterService.java      |  8 ++++
 .../src/main/resources/mappers/api-sqlmap.xml      | 16 +++++++
 10 files changed, 189 insertions(+), 3 deletions(-)

diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/ShenyuClientHttpRegistryController.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/ShenyuClientHttpRegistryController.java
index 08c073015..af5db15e6 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/ShenyuClientHttpRegistryController.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/ShenyuClientHttpRegistryController.java
@@ -19,6 +19,7 @@ package org.apache.shenyu.admin.controller;
 
 import org.apache.shenyu.admin.utils.ShenyuResultMessage;
 import org.apache.shenyu.register.common.config.ShenyuRegisterCenterConfig;
+import org.apache.shenyu.register.common.dto.ApiDocRegisterDTO;
 import org.apache.shenyu.register.common.dto.MetaDataRegisterDTO;
 import org.apache.shenyu.register.common.dto.URIRegisterDTO;
 import org.apache.shenyu.register.client.server.api.ShenyuClientServerRegisterPublisher;
@@ -74,5 +75,17 @@ public class ShenyuClientHttpRegistryController implements ShenyuClientServerReg
         publisher.publish(uriRegisterDTO);
         return ShenyuResultMessage.SUCCESS;
     }
+
+    /**
+     * registerApiDoc.
+     * @param apiDocRegisterDTO apiDocRegisterDTO
+     * @return String
+     */
+    @PostMapping("/register-apiDoc")
+    @ResponseBody
+    public String registerApiDoc(@RequestBody final ApiDocRegisterDTO apiDocRegisterDTO) {
+        publisher.publish(apiDocRegisterDTO);
+        return ShenyuResultMessage.SUCCESS;
+    }
     
 }
diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/disruptor/RegisterClientServerDisruptorPublisher.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/disruptor/RegisterClientServerDisruptorPublisher.java
index 73734bc19..ddb1eb68c 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/disruptor/RegisterClientServerDisruptorPublisher.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/disruptor/RegisterClientServerDisruptorPublisher.java
@@ -18,6 +18,7 @@
 package org.apache.shenyu.admin.disruptor;
 
 import org.apache.shenyu.admin.disruptor.executor.RegisterServerConsumerExecutor.RegisterServerExecutorFactory;
+import org.apache.shenyu.admin.disruptor.subscriber.ApiDocExecutorSubscriber;
 import org.apache.shenyu.admin.disruptor.subscriber.MetadataExecutorSubscriber;
 import org.apache.shenyu.admin.disruptor.subscriber.URIRegisterExecutorSubscriber;
 import org.apache.shenyu.admin.service.register.ShenyuClientRegisterService;
@@ -58,6 +59,7 @@ public class RegisterClientServerDisruptorPublisher implements ShenyuClientServe
         RegisterServerExecutorFactory factory = new RegisterServerExecutorFactory();
         factory.addSubscribers(new URIRegisterExecutorSubscriber(shenyuClientRegisterService));
         factory.addSubscribers(new MetadataExecutorSubscriber(shenyuClientRegisterService));
+        factory.addSubscribers(new ApiDocExecutorSubscriber(shenyuClientRegisterService));
         providerManage = new DisruptorProviderManage<>(factory);
         providerManage.startup();
     }
diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/disruptor/executor/RegisterServerConsumerExecutor.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/disruptor/executor/RegisterServerConsumerExecutor.java
index 0e461ee96..2cc85567c 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/disruptor/executor/RegisterServerConsumerExecutor.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/disruptor/executor/RegisterServerConsumerExecutor.java
@@ -56,7 +56,6 @@ public final class RegisterServerConsumerExecutor extends QueueConsumerExecutor<
         if (CollectionUtils.isEmpty(results)) {
             return;
         }
-        
         selectExecutor(results).executor(results);
     }
     
diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/disruptor/subscriber/ApiDocExecutorSubscriber.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/disruptor/subscriber/ApiDocExecutorSubscriber.java
new file mode 100644
index 000000000..bdda61d4f
--- /dev/null
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/disruptor/subscriber/ApiDocExecutorSubscriber.java
@@ -0,0 +1,56 @@
+/*
+ * 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.admin.disruptor.subscriber;
+
+import org.apache.shenyu.admin.service.register.ShenyuClientRegisterService;
+import org.apache.shenyu.register.common.dto.ApiDocRegisterDTO;
+import org.apache.shenyu.register.common.subsriber.ExecutorTypeSubscriber;
+import org.apache.shenyu.register.common.type.DataType;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.Optional;
+
+/**
+ * The type Metadata executor subscriber.
+ */
+public class ApiDocExecutorSubscriber implements ExecutorTypeSubscriber<ApiDocRegisterDTO> {
+
+    private final Map<String, ShenyuClientRegisterService> shenyuClientRegisterService;
+
+    public ApiDocExecutorSubscriber(final Map<String, ShenyuClientRegisterService> shenyuClientRegisterService) {
+        this.shenyuClientRegisterService = shenyuClientRegisterService;
+    }
+
+    @Override
+    public DataType getType() {
+        return DataType.API_DOC;
+    }
+
+    @Override
+    public void executor(final Collection<ApiDocRegisterDTO> dataList) {
+        dataList.forEach(apiDoc -> {
+            Optional.ofNullable(this.shenyuClientRegisterService.get(apiDoc.getRpcType()))
+                    .ifPresent(shenyuClientRegisterService -> {
+                        synchronized (shenyuClientRegisterService) {
+                            shenyuClientRegisterService.registerApiDoc(apiDoc);
+                        }
+                    });
+        });
+    }
+}
diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/ApiMapper.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/ApiMapper.java
index ea1b44d71..38f655c95 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/ApiMapper.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/ApiMapper.java
@@ -110,4 +110,21 @@ public interface ApiMapper extends ExistProvider {
      * @return rows int
      */
     int deleteByIds(List<String> ids);
+
+    /**
+     * selectByApiPathHttpMethodRpcType.
+     * @param apiPath apiPath
+     * @param httpMethod httpMethod
+     * @param rpcType rpcType
+     * @return {@linkplain ApiDO}
+     */
+    List<ApiDO> selectByApiPathHttpMethodRpcType(@Param(value = "apiPath") String apiPath, @Param(value = "httpMethod") Integer httpMethod, @Param(value = "rpcType") String rpcType);
+
+    /**
+     * updateOfflineByContextPath.
+     * @param contextPath context path
+     * @return update count
+     */
+    int updateOfflineByContextPath(@Param(value = "contextPath") String contextPath);
+
 }
diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/ApiService.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/ApiService.java
index 1440eb5a2..c7deda621 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/ApiService.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/ApiService.java
@@ -62,4 +62,20 @@ public interface ApiService {
      * @return {@linkplain CommonPager}
      */
     CommonPager<ApiVO> listByPage(ApiQuery apiQuery);
+
+    /**
+     * deleteByApiPathHttpMethodRpcType.
+     * @param apiPath apiPath
+     * @param httpMethod httpMethod
+     * @param rpcType rpcType
+     * @return delete rows
+     */
+    int deleteByApiPathHttpMethodRpcType(String apiPath, Integer httpMethod, String rpcType);
+
+    /**
+     * offlineByContextPath.
+     * @param contextPath context path
+     * @return the string
+     */
+    String offlineByContextPath(String contextPath);
 }
diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ApiServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ApiServiceImpl.java
index b312f3cbc..0c27bfd7a 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ApiServiceImpl.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ApiServiceImpl.java
@@ -175,4 +175,25 @@ public class ApiServiceImpl implements ApiService {
                     return ApiVO.buildApiVO(item, tagVOS);
                 }).collect(Collectors.toList()));
     }
+
+    @Override
+    public int deleteByApiPathHttpMethodRpcType(final String apiPath, final Integer httpMethod, final String rpcType) {
+        List<ApiDO> apiDOs = apiMapper.selectByApiPathHttpMethodRpcType(apiPath, httpMethod, rpcType);
+        // delete apis.
+        if (CollectionUtils.isNotEmpty(apiDOs)) {
+            final List<String> apiIds = ListUtil.map(apiDOs, ApiDO::getId);
+            final int deleteRows = this.apiMapper.deleteByIds(apiIds);
+            if (deleteRows > 0) {
+                tagRelationMapper.deleteByApiIds(apiIds);
+            }
+            return deleteRows;
+        }
+        return 0;
+    }
+
+    @Override
+    public String offlineByContextPath(final String contextPath) {
+        apiMapper.updateOfflineByContextPath(contextPath);
+        return ShenyuResultMessage.SUCCESS;
+    }
 }
diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/AbstractShenyuClientRegisterServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/AbstractShenyuClientRegisterServiceImpl.java
index 688c62e7e..9a6288884 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/AbstractShenyuClientRegisterServiceImpl.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/AbstractShenyuClientRegisterServiceImpl.java
@@ -20,9 +20,11 @@ package org.apache.shenyu.admin.service.register;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.shenyu.admin.listener.DataChangedEvent;
+import org.apache.shenyu.admin.model.dto.ApiDTO;
 import org.apache.shenyu.admin.model.dto.RuleConditionDTO;
 import org.apache.shenyu.admin.model.dto.RuleDTO;
 import org.apache.shenyu.admin.model.entity.SelectorDO;
+import org.apache.shenyu.admin.service.ApiService;
 import org.apache.shenyu.admin.service.MetaDataService;
 import org.apache.shenyu.admin.service.RuleService;
 import org.apache.shenyu.admin.service.SelectorService;
@@ -40,8 +42,10 @@ import org.apache.shenyu.common.enums.ParamTypeEnum;
 import org.apache.shenyu.common.exception.ShenyuException;
 import org.apache.shenyu.common.utils.PathUtils;
 import org.apache.shenyu.common.utils.PluginNameAdapter;
+import org.apache.shenyu.register.common.dto.ApiDocRegisterDTO;
 import org.apache.shenyu.register.common.dto.MetaDataRegisterDTO;
 import org.apache.shenyu.register.common.dto.URIRegisterDTO;
+import org.apache.shenyu.register.common.enums.EventType;
 import org.springframework.context.ApplicationEventPublisher;
 
 import javax.annotation.Resource;
@@ -78,7 +82,10 @@ public abstract class AbstractShenyuClientRegisterServiceImpl extends FallbackSh
     
     @Resource
     private UpstreamCheckService upstreamCheckService;
-    
+
+    @Resource
+    private ApiService apiService;
+
     /**
      * Selector handler string.
      *
@@ -134,7 +141,38 @@ public abstract class AbstractShenyuClientRegisterServiceImpl extends FallbackSh
         }
         return ShenyuResultMessage.SUCCESS;
     }
-    
+
+    @Override
+    public String registerApiDoc(final ApiDocRegisterDTO apiDocRegisterDTO) {
+        if (apiDocRegisterDTO.getEventType().equals(EventType.REGISTER)) {
+            ApiDTO apiDTO = buildApiDTO(apiDocRegisterDTO);
+            apiService.deleteByApiPathHttpMethodRpcType(apiDTO.getApiPath(), apiDTO.getHttpMethod(), apiDTO.getRpcType());
+            apiService.createOrUpdate(apiDTO);
+        } else if (apiDocRegisterDTO.getEventType().equals(EventType.OFFLINE)) {
+            String contextPath = apiDocRegisterDTO.getContextPath();
+            apiService.offlineByContextPath(contextPath);
+        }
+        return ShenyuResultMessage.SUCCESS;
+    }
+
+    private ApiDTO buildApiDTO(final ApiDocRegisterDTO apiDocRegisterDTO) {
+        ApiDTO apiDTO = new ApiDTO();
+        apiDTO.setApiPath(apiDocRegisterDTO.getApiPath());
+        apiDTO.setApiSource(apiDocRegisterDTO.getApiSource());
+        apiDTO.setApiOwner(apiDocRegisterDTO.getApiOwner());
+        apiDTO.setDocument(apiDocRegisterDTO.getDocument());
+        apiDTO.setExt(apiDocRegisterDTO.getExt());
+        apiDTO.setVersion(apiDocRegisterDTO.getVersion());
+        apiDTO.setRpcType(apiDocRegisterDTO.getRpcType());
+        apiDTO.setConsume(apiDocRegisterDTO.getConsume());
+        apiDTO.setProduce(apiDocRegisterDTO.getProduce());
+        apiDTO.setContextPath(apiDocRegisterDTO.getContextPath());
+        apiDTO.setHttpMethod(apiDocRegisterDTO.getHttpMethod());
+        apiDTO.setState(apiDocRegisterDTO.getState());
+        apiDTO.setApiDesc(apiDocRegisterDTO.getApiDesc());
+        return apiDTO;
+    }
+
     /**
      * Register uri string.
      *
diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/ShenyuClientRegisterService.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/ShenyuClientRegisterService.java
index b8f7ebc1b..982c7a974 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/ShenyuClientRegisterService.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/ShenyuClientRegisterService.java
@@ -18,6 +18,7 @@
 package org.apache.shenyu.admin.service.register;
 
 import org.apache.shenyu.common.constant.Constants;
+import org.apache.shenyu.register.common.dto.ApiDocRegisterDTO;
 import org.apache.shenyu.register.common.dto.MetaDataRegisterDTO;
 import org.apache.shenyu.register.common.dto.URIRegisterDTO;
 
@@ -42,6 +43,13 @@ public interface ShenyuClientRegisterService {
      * @return the string
      */
     String register(MetaDataRegisterDTO metaDataRegisterDTO);
+
+    /**
+     * registerApiDoc.
+     * @param apiDocRegisterDTO apiDocRegisterDTO
+     * @return String
+     */
+    String registerApiDoc(ApiDocRegisterDTO apiDocRegisterDTO);
     
     /**
      * Register uri string.
diff --git a/shenyu-admin/src/main/resources/mappers/api-sqlmap.xml b/shenyu-admin/src/main/resources/mappers/api-sqlmap.xml
index 9468d0630..5df35a854 100644
--- a/shenyu-admin/src/main/resources/mappers/api-sqlmap.xml
+++ b/shenyu-admin/src/main/resources/mappers/api-sqlmap.xml
@@ -318,4 +318,20 @@
     </foreach>
   </delete>
 
+  <select id="selectByApiPathHttpMethodRpcType" resultMap="BaseResultMap">
+    SELECT
+        <include refid="Base_Column_List"/>
+    FROM api
+    WHERE api_path = #{apiPath,jdbcType=VARCHAR}
+    AND http_method = #{httpMethod,jdbcType=INTEGER}
+    AND rpc_type = #{rpcType,jdbcType=VARCHAR}
+  </select>
+
+  <update id="updateOfflineByContextPath">
+    UPDATE
+        api
+    SET state = 2
+    WHERE context_path = #{contextPath,jdbcType=VARCHAR}
+  </update>
+
 </mapper>
\ No newline at end of file