You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by "ShenFeng312 (via GitHub)" <gi...@apache.org> on 2023/04/28 02:28:12 UTC

[GitHub] [dubbo] ShenFeng312 commented on a diff in pull request #12140: Fix #10681 Support dynamic cluster

ShenFeng312 commented on code in PR #12140:
URL: https://github.com/apache/dubbo/pull/12140#discussion_r1179871731


##########
dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/DynamicClusterInvoker.java:
##########
@@ -0,0 +1,69 @@
+/*
+ * 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.rpc.cluster.support;
+
+import org.apache.dubbo.common.utils.StringUtils;
+import org.apache.dubbo.rpc.Invocation;
+import org.apache.dubbo.rpc.Invoker;
+import org.apache.dubbo.rpc.Result;
+import org.apache.dubbo.rpc.RpcContext;
+import org.apache.dubbo.rpc.RpcException;
+import org.apache.dubbo.rpc.cluster.Directory;
+import org.apache.dubbo.rpc.cluster.LoadBalance;
+import org.apache.dubbo.rpc.cluster.support.wrapper.AbstractCluster;
+
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.Function;
+
+import static org.apache.dubbo.common.constants.CommonConstants.CLUSTER_KEY;
+import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_CLUSTER_KEY;
+
+public class DynamicClusterInvoker<T> extends AbstractClusterInvoker<T> {
+
+    private final Function<String, AbstractCluster> builder;
+
+
+    private final Map<String, AbstractClusterInvoker<T>> clusterInvokerMap = new ConcurrentHashMap<>();
+
+    public DynamicClusterInvoker(Directory<T> directory, Function<String, AbstractCluster> clusterBuilder) {
+        super(directory);
+        this.builder = clusterBuilder;
+    }
+
+
+    @Override
+    protected Result doInvoke(Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
+        String cluster;
+        cluster = invocation.getAttachment(CLUSTER_KEY);
+
+        if (StringUtils.isEmpty(cluster)) {
+            cluster = RpcContext.getServiceContext().getConsumerUrl().getMethodParameter(invocation.getMethodName(), CLUSTER_KEY);
+        }
+        if (StringUtils.isEmpty(cluster)) {
+            cluster = RpcContext.getServiceContext().getConsumerUrl().getParameter(DEFAULT_CLUSTER_KEY);
+        }
+
+        String finalCluster = cluster;
+        AbstractClusterInvoker<T> abstractClusterInvoker = clusterInvokerMap.computeIfAbsent(cluster,
+            (key) -> (AbstractClusterInvoker<T>) builder.apply(finalCluster).join(directory, false));

Review Comment:
   想了想要改成adaptive 的话 需要把 之前的cluster 实现改造掉。 并且解开ClusterInvoker和directory的直接依赖 改为通过参数传递的方式。因为getExtension和getActivateExtension都不支持带参数的构造函数



-- 
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