Skip to content

Commit

Permalink
improve:remove guava dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
qinliujie committed Mar 15, 2021
1 parent c47889b commit ad8b837
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 30 deletions.
6 changes: 0 additions & 6 deletions dubbo-cluster/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>

<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@

package org.apache.dubbo.rpc.cluster.router.mesh.route;

import com.google.common.eventbus.EventBus;
import org.apache.dubbo.common.config.configcenter.ConfigChangedEvent;
import org.apache.dubbo.common.config.configcenter.ConfigurationListener;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.rpc.cluster.router.mesh.rule.VsDestinationGroup;
import org.apache.dubbo.rpc.cluster.router.mesh.rule.destination.DestinationRule;
import org.apache.dubbo.rpc.cluster.router.mesh.rule.virtualservice.VirtualServiceRule;
import org.apache.dubbo.rpc.cluster.router.mesh.util.VsDestinationGroupRuleDispatcher;
import org.yaml.snakeyaml.Yaml;

import java.text.MessageFormat;
Expand All @@ -35,7 +35,7 @@ public class MeshAppRuleListener implements ConfigurationListener {

public static final Logger logger = LoggerFactory.getLogger(MeshAppRuleListener.class);

private final EventBus eventBus = new EventBus();
private final VsDestinationGroupRuleDispatcher vsDestinationGroupRuleDispatcher = new VsDestinationGroupRuleDispatcher();

private String appName;

Expand Down Expand Up @@ -74,7 +74,7 @@ public void receiveConfigInfo(String configInfo) {
logger.error("[MeshAppRule] parse failed: " + configInfo, e);
}
if (vsDestinationGroupHolder != null) {
eventBus.post(vsDestinationGroupHolder);
vsDestinationGroupRuleDispatcher.post(vsDestinationGroupHolder);
}

}
Expand All @@ -83,20 +83,12 @@ public void register(MeshRuleRouter subscriber) {
if (vsDestinationGroupHolder != null) {
subscriber.onRuleChange(vsDestinationGroupHolder);
}
eventBus.register(subscriber);
vsDestinationGroupRuleDispatcher.register(subscriber);
}

//
public void unregister(MeshRuleRouter sub) {
try {
eventBus.unregister(sub);
} catch (IllegalArgumentException e) {
if (e.getMessage().contains("missing event subscriber")) {
// ignored
return;
}
throw e;
}
vsDestinationGroupRuleDispatcher.unregister(sub);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public final class MeshRuleManager {

private static ConcurrentHashMap<String, MeshAppRuleListener> appRuleListeners = new ConcurrentHashMap<>();

public static void subscribeAppRule(String app) {
public synchronized static void subscribeAppRule(String app) {

MeshAppRuleListener meshAppRuleListener = new MeshAppRuleListener(app);
String appRuleDataId = app + MESH_RULE_DATA_ID_SUFFIX;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package org.apache.dubbo.rpc.cluster.router.mesh.route;

import com.google.common.eventbus.Subscribe;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.rpc.Invocation;
Expand All @@ -36,6 +35,7 @@
import org.apache.dubbo.rpc.cluster.router.mesh.rule.virtualservice.destination.DubboDestination;
import org.apache.dubbo.rpc.cluster.router.mesh.rule.virtualservice.destination.DubboRouteDestination;
import org.apache.dubbo.rpc.cluster.router.mesh.rule.virtualservice.match.StringMatch;
import org.apache.dubbo.rpc.cluster.router.mesh.util.VsDestinationGroupRuleListener;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -45,7 +45,7 @@
import java.util.Random;


public class MeshRuleRouter implements Router {
public class MeshRuleRouter implements Router, VsDestinationGroupRuleListener {

protected int priority = -500;
protected boolean force = false;
Expand Down Expand Up @@ -146,7 +146,6 @@ private void registerAppRule(List<Invoker<?>> invokers) {
}


@Subscribe
public void onRuleChange(VsDestinationGroup vsDestinationGroup) {
this.vsDestinationGroup = vsDestinationGroup;
computeSubset();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.mesh.util;

import org.apache.dubbo.common.utils.ConcurrentHashSet;
import org.apache.dubbo.rpc.cluster.router.mesh.rule.VsDestinationGroup;

import java.util.Set;


public class VsDestinationGroupRuleDispatcher {

private Set<VsDestinationGroupRuleListener> listenerSet = new ConcurrentHashSet<>();

public synchronized void post(VsDestinationGroup vsDestinationGroup) {
for (VsDestinationGroupRuleListener vsDestinationGroupRuleListener : listenerSet) {
try {
vsDestinationGroupRuleListener.onRuleChange(vsDestinationGroup);
} catch (Throwable throwable) {

}
}
}

public synchronized boolean register(VsDestinationGroupRuleListener listener) {
if (listener == null) {
return false;
}
return listenerSet.add(listener);
}

public synchronized void unregister(VsDestinationGroupRuleListener listener) {
if (listener == null) {
return;
}
listenerSet.remove(listener);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.mesh.util;


import org.apache.dubbo.rpc.cluster.router.mesh.rule.VsDestinationGroup;

public interface VsDestinationGroupRuleListener {
void onRuleChange(VsDestinationGroup vsDestinationGroup);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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.mesh.util;

import org.apache.dubbo.rpc.cluster.router.mesh.rule.VsDestinationGroup;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;


class VsDestinationGroupRuleDispatcherTest {

@Test
public void post() {
VsDestinationGroupRuleDispatcher vsDestinationGroupRuleDispatcher = new VsDestinationGroupRuleDispatcher();

VsDestinationGroupRuleListener vsDestinationGroupRuleListener = mock(VsDestinationGroupRuleListener.class);

vsDestinationGroupRuleDispatcher.register(vsDestinationGroupRuleListener);

vsDestinationGroupRuleDispatcher.post(new VsDestinationGroup());
vsDestinationGroupRuleDispatcher.post(new VsDestinationGroup());

verify(vsDestinationGroupRuleListener, times(2)).onRuleChange(anyObject());

}

@Test
public void register() {
VsDestinationGroupRuleDispatcher vsDestinationGroupRuleDispatcher = new VsDestinationGroupRuleDispatcher();

VsDestinationGroupRuleListener vsDestinationGroupRuleListener = mock(VsDestinationGroupRuleListener.class);

assertFalse(vsDestinationGroupRuleDispatcher.register(null));
assertTrue(vsDestinationGroupRuleDispatcher.register(vsDestinationGroupRuleListener));
assertFalse(vsDestinationGroupRuleDispatcher.register(vsDestinationGroupRuleListener));
}

@Test
public void unregister() {

VsDestinationGroupRuleDispatcher vsDestinationGroupRuleDispatcher = new VsDestinationGroupRuleDispatcher();

VsDestinationGroupRuleListener vsDestinationGroupRuleListener = mock(VsDestinationGroupRuleListener.class);
vsDestinationGroupRuleDispatcher.register(vsDestinationGroupRuleListener);

vsDestinationGroupRuleDispatcher.post(new VsDestinationGroup());

vsDestinationGroupRuleDispatcher.unregister(vsDestinationGroupRuleListener);
vsDestinationGroupRuleDispatcher.post(new VsDestinationGroup());

verify(vsDestinationGroupRuleListener, times(1)).onRuleChange(anyObject());
}
}
7 changes: 0 additions & 7 deletions dubbo-dependencies-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@
<jsonrpc_version>1.2.0</jsonrpc_version>
<mortbay_jetty_version>6.1.26</mortbay_jetty_version>
<portlet_version>2.0</portlet_version>
<guava_version>19.0</guava_version>
<maven_flatten_version>1.1.0</maven_flatten_version>
<revision>3.0.0-SNAPSHOT</revision>
</properties>
Expand Down Expand Up @@ -719,12 +718,6 @@
<scope>test</scope>
<version>${fabric8_kubernetes_version}</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava_version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down

0 comments on commit ad8b837

Please sign in to comment.