You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2021/09/08 02:18:00 UTC

[GitHub] [dubbo-spi-extensions] chickenlj commented on a change in pull request #50: [ISSUE #51] Dubbo admin mock plugin base on dubbo 3.x.

chickenlj commented on a change in pull request #50:
URL: https://github.com/apache/dubbo-spi-extensions/pull/50#discussion_r703972520



##########
File path: dubbo-mock-extensions/dubbo-mock-admin/src/main/java/org/apache/dubbo/mock/filter/AdminMockFilter.java
##########
@@ -0,0 +1,179 @@
+/*
+ * 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.dubbo.mock.filter;
+
+import org.apache.dubbo.common.config.configcenter.DynamicConfiguration;
+import org.apache.dubbo.common.constants.CommonConstants;
+import org.apache.dubbo.common.extension.Activate;
+import org.apache.dubbo.common.logger.Logger;
+import org.apache.dubbo.common.logger.LoggerFactory;
+import org.apache.dubbo.common.utils.CollectionUtils;
+import org.apache.dubbo.common.utils.StringUtils;
+import org.apache.dubbo.config.ReferenceConfig;
+import org.apache.dubbo.config.bootstrap.DubboBootstrap;
+import org.apache.dubbo.mock.api.GlobalMockRule;
+import org.apache.dubbo.mock.api.MockContext;
+import org.apache.dubbo.mock.api.MockResult;
+import org.apache.dubbo.mock.api.MockService;
+import org.apache.dubbo.mock.handler.CommonTypeHandler;
+import org.apache.dubbo.mock.handler.ResultContext;
+import org.apache.dubbo.mock.handler.TypeHandler;
+import org.apache.dubbo.mock.utils.ProtobufUtil;
+import org.apache.dubbo.rpc.AppResponse;
+import org.apache.dubbo.rpc.AsyncRpcResult;
+import org.apache.dubbo.rpc.Invocation;
+import org.apache.dubbo.rpc.Invoker;
+import org.apache.dubbo.rpc.Result;
+import org.apache.dubbo.rpc.RpcException;
+import org.apache.dubbo.rpc.RpcInvocation;
+import org.apache.dubbo.rpc.cluster.filter.ClusterFilter;
+import org.apache.dubbo.rpc.model.ApplicationModel;
+
+import com.google.gson.Gson;
+
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+
+import static org.apache.dubbo.mock.api.MockConstants.ADMIN_MOCK_RULE_GROUP;
+import static org.apache.dubbo.mock.api.MockConstants.ADMIN_MOCK_RULE_KEY;
+
+/**
+ * AdminMockFilter will intercept the request from user's consumer. if the mock tag is opened,
+ * then the user's consumer will request the mock data configured in Dubbo Admin. The mock data's
+ * request is agent by the implement of {@link MockService}.
+ */
+@Activate(group = CommonConstants.CONSUMER)
+public class AdminMockFilter implements ClusterFilter {
+
+    private static final Logger logger = LoggerFactory.getLogger(AdminMockFilter.class);
+
+    private final TypeHandler typeHandler;
+
+    private GlobalMockRule globalMockRule = new GlobalMockRule();
+    
+    static {
+        ReferenceConfig<MockService> mockServiceConfig = new ReferenceConfig<>();
+        mockServiceConfig.setCheck(false);
+        mockServiceConfig.setInterface(MockService.class);
+        DubboBootstrap.getInstance().reference(mockServiceConfig);
+    }
+
+    public AdminMockFilter() {
+        typeHandler = new CommonTypeHandler();
+        Optional<DynamicConfiguration> dynamicConfigurationOptional = ApplicationModel.getEnvironment().getDynamicConfiguration();
+        if (!dynamicConfigurationOptional.isPresent()) {
+            logger.warn("[Dubbo Admin Mock] could not find configuration center, all consumer request will not be mocked!");
+            return;
+        }
+        DynamicConfiguration dynamicConfiguration = dynamicConfigurationOptional.get();
+        String config = dynamicConfiguration.getConfig(ADMIN_MOCK_RULE_KEY, ADMIN_MOCK_RULE_GROUP);
+        if (StringUtils.isNotEmpty(config)) {
+            GlobalMockRule newGlobalMockRule = new Gson().fromJson(config, GlobalMockRule.class);
+            globalMockRule.setEnableMock(newGlobalMockRule.getEnableMock());
+            globalMockRule.getEnabledMockRules().addAll(newGlobalMockRule.getEnabledMockRules());
+        }
+
+        dynamicConfiguration.addListener(ADMIN_MOCK_RULE_KEY, ADMIN_MOCK_RULE_GROUP, event -> {
+            globalMockRule.getEnabledMockRules().clear();
+            String content = event.getContent();
+            if (StringUtils.isBlank(content)) {
+                globalMockRule.setEnableMock(false);
+                return;
+            }
+            GlobalMockRule newRule = new Gson().fromJson(config, GlobalMockRule.class);
+            globalMockRule.setEnableMock(newRule.getEnableMock());
+            globalMockRule.getEnabledMockRules().addAll(newRule.getEnabledMockRules());
+        });
+    }
+
+    @Override
+    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
+        // check if open the admin mock config, global config.
+        if (!globalMockRule.getEnableMock()) {
+            return invoker.invoke(invocation);
+        }
+
+        String interfaceName = invocation.getTargetServiceUniqueName();
+        String methodName = invocation.getMethodName();
+        Object[] params = solveParams(invocation.getArguments());
+
+        // check if the service is in mock list
+        String mockRuleName = interfaceName + "#" + methodName;
+        Set<String> mockRules = globalMockRule.getEnabledMockRules();
+        if (CollectionUtils.isEmpty(mockRules) || !mockRules.contains(mockRuleName)) {
+            return invoker.invoke(invocation);
+        }
+        
+        // check if the MockService's invoker, then request.
+        if (Objects.equals(interfaceName, MockService.class.getName())) {
+            return invoker.invoke(invocation);
+        }
+        MockService mockService = DubboBootstrap.getInstance().getCache().get(MockService.class);

Review comment:
       Better use proxy factory to create mockService directly. Check how dubbo does that with MetadataService for example.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org