From 5b998a045063772df5f75dfa6441c44e94a3c1f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Mon, 17 Jul 2023 18:02:04 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E4=BF=AE=E5=A4=8D=20=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=20dubbo=20=E6=8A=A5=E6=97=A0=E7=94=A8?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E9=97=AE=E9=A2=98(=E4=B8=B4=E6=97=B6?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=AD=89=E5=BE=85=E5=AE=98=E6=96=B9=E5=8F=91?= =?UTF-8?q?=E7=89=88)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/ProviderAppStateRouter.java | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 ruoyi-common/ruoyi-common-dubbo/src/main/java/org/apache/dubbo/rpc/cluster/router/condition/config/ProviderAppStateRouter.java diff --git a/ruoyi-common/ruoyi-common-dubbo/src/main/java/org/apache/dubbo/rpc/cluster/router/condition/config/ProviderAppStateRouter.java b/ruoyi-common/ruoyi-common-dubbo/src/main/java/org/apache/dubbo/rpc/cluster/router/condition/config/ProviderAppStateRouter.java new file mode 100644 index 00000000..b39b3167 --- /dev/null +++ b/ruoyi-common/ruoyi-common-dubbo/src/main/java/org/apache/dubbo/rpc/cluster/router/condition/config/ProviderAppStateRouter.java @@ -0,0 +1,80 @@ +/* + * 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.router.condition.config; + +import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.config.configcenter.ConfigChangedEvent; +import org.apache.dubbo.common.config.configcenter.DynamicConfiguration; +import org.apache.dubbo.common.logger.ErrorTypeAwareLogger; +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.rpc.Invoker; +import org.apache.dubbo.rpc.cluster.router.state.BitList; + +import static org.apache.dubbo.common.constants.LoggerCodeConstants.CLUSTER_TAG_ROUTE_EMPTY; +import static org.apache.dubbo.common.utils.StringUtils.isEmpty; + +/** + * Application level router, "application.condition-router" + */ +public class ProviderAppStateRouter extends ListenableStateRouter { + private static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(ListenableStateRouter.class); + public static final String NAME = "PROVIDER_APP_ROUTER"; + private String application; + private final String currentApplication; + + public ProviderAppStateRouter(URL url) { + super(url, url.getApplication()); + this.currentApplication = url.getApplication(); + } + + @Override + public void notify(BitList> invokers) { + if (CollectionUtils.isEmpty(invokers)) { + return; + } + + Invoker invoker = invokers.get(0); + URL url = invoker.getUrl(); + String providerApplication = url.getRemoteApplication(); + + // provider application is empty or equals with the current application + if (isEmpty(providerApplication)) { + logger.warn(CLUSTER_TAG_ROUTE_EMPTY, "condition router get providerApplication is empty, will not subscribe to provider app rules.", "", ""); + return; + } + if (providerApplication.equals(currentApplication)) { + return; + } + + synchronized (this) { + if (!providerApplication.equals(application)) { + if (StringUtils.isNotEmpty(application)) { + this.getRuleRepository().removeListener(application + RULE_SUFFIX, this); + } + String key = providerApplication + RULE_SUFFIX; + this.getRuleRepository().addListener(key, this); + application = providerApplication; + String rawRule = this.getRuleRepository().getRule(key, DynamicConfiguration.DEFAULT_GROUP); + if (StringUtils.isNotEmpty(rawRule)) { + this.process(new ConfigChangedEvent(key, DynamicConfiguration.DEFAULT_GROUP, rawRule)); + } + } + } + } +}