Skip to content

Commit

Permalink
Verify remote refer for registry (apache#8645)
Browse files Browse the repository at this point in the history
* add unit test:remote refer for registry

* fix

* fix

* add license
  • Loading branch information
CrazyHZM authored Aug 31, 2021
1 parent 3f59150 commit 23e2f4d
Show file tree
Hide file tree
Showing 31 changed files with 1,347 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,20 @@
public class RouterChain<T> {
private static final Logger logger = LoggerFactory.getLogger(RouterChain.class);

// full list of addresses from registry, classified by method name.
/**
* full list of addresses from registry, classified by method name.
*/
private volatile List<Invoker<T>> invokers = Collections.emptyList();

// containing all routers, reconstruct every time 'route://' urls change.
/**
* containing all routers, reconstruct every time 'route://' urls change.
*/
private volatile List<Router> routers = Collections.emptyList();

// Fixed router instances: ConfigConditionRouter, TagRouter, e.g., the rule for each instance may change but the
// instance will never delete or recreate.
/**
* Fixed router instances: ConfigConditionRouter, TagRouter, e.g.,
* the rule for each instance may change but the instance will never delete or recreate.
*/
private List<Router> builtinRouters = Collections.emptyList();

private List<StateRouter> builtinStateRouters = Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ public URL getUrl() {
protected Result doInvoke(Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
return null;
}

public ClusterInvoker<T> getFilterInvoker() {
return filterInvoker;
}
}

static class InvocationInterceptorInvoker<T> extends AbstractClusterInvoker<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*/
public class MockClusterWrapper implements Cluster {

private Cluster cluster;
private final Cluster cluster;

public MockClusterWrapper(Cluster cluster) {
this.cluster = cluster;
Expand All @@ -39,4 +39,7 @@ public <T> Invoker<T> join(Directory<T> directory) throws RpcException {
this.cluster.join(directory));
}

public Cluster getCluster() {
return cluster;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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;


import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.url.component.ServiceConfigURL;
import org.apache.dubbo.rpc.cluster.filter.DemoService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY;

public class RouterChainTest {

/**
* verify the router and state router loaded by default
*/
@Test
public void testBuildRouterChain() {

Map<String, String> parameters = new HashMap<>();
parameters.put(INTERFACE_KEY, DemoService.class.getName());
parameters.put("registry", "zookeeper");
URL url = new ServiceConfigURL("dubbo",
"127.0.0.1",
20881,
DemoService.class.getName(),
parameters);

RouterChain<DemoService> routerChain = RouterChain.buildChain(url);
Assertions.assertEquals(5, routerChain.getRouters().size());
Assertions.assertEquals(2, routerChain.getStateRouters().size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* 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.wrapper;


import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.url.component.ServiceConfigURL;
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.cluster.Directory;
import org.apache.dubbo.rpc.cluster.LoadBalance;
import org.apache.dubbo.rpc.cluster.filter.DemoService;
import org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder;
import org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.REFERENCE_FILTER_KEY;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class AbstractClusterTest {

@Test
public void testBuildClusterInvokerChain() {

Map<String, String> parameters = new HashMap<>();
parameters.put(INTERFACE_KEY, DemoService.class.getName());
parameters.put("registry", "zookeeper");
parameters.put(REFERENCE_FILTER_KEY, "demo");
ServiceConfigURL url = new ServiceConfigURL("registry",
"127.0.0.1",
2181,
"org.apache.dubbo.registry.RegistryService",
parameters);

URL consumerUrl = new ServiceConfigURL("dubbo",
"127.0.0.1",
20881,
DemoService.class.getName(),
parameters);

Directory<?> directory = mock(Directory.class);
when(directory.getUrl()).thenReturn(url);
when(directory.getConsumerUrl()).thenReturn(consumerUrl);
DemoCluster demoCluster = new DemoCluster();
Invoker<?> invoker = demoCluster.join(directory);
Assertions.assertTrue(invoker instanceof AbstractCluster.ClusterFilterInvoker);
Assertions.assertTrue(((AbstractCluster.ClusterFilterInvoker<?>) invoker).getFilterInvoker()
instanceof FilterChainBuilder.ClusterFilterChainNode);


}

static class DemoCluster extends AbstractCluster {
@Override
public <T> Invoker<T> join(Directory<T> directory) throws RpcException {
return super.join(directory);
}

@Override
protected <T> AbstractClusterInvoker<T> doJoin(Directory<T> directory) throws RpcException {
return new DemoAbstractClusterInvoker<>(directory, directory.getUrl());
}
}

static class DemoAbstractClusterInvoker<T> extends AbstractClusterInvoker<T> {

@Override
public URL getUrl() {
return super.getUrl();
}

public DemoAbstractClusterInvoker(Directory<T> directory, URL url) {
super(directory, url);
}

@Override
protected Result doInvoke(Invocation invocation, List list, LoadBalance loadbalance) throws RpcException {
return null;
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,22 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.registry.client.metadata.store;
package org.apache.dubbo.rpc.cluster.support.wrapper;


public class RemoteMetadataServiceImplTest {
import org.apache.dubbo.common.extension.Activate;
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.cluster.filter.ClusterFilter;

import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER;

@Activate(value = "demo",group = {CONSUMER})
public class DemoClusterFilter implements ClusterFilter {
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
return invoker.invoke(invocation);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
demo=org.apache.dubbo.rpc.cluster.support.wrapper.DemoClusterFilter
17 changes: 17 additions & 0 deletions dubbo-common/src/main/java/org/apache/dubbo/common/URLBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ public URLBuilder setProtocol(String protocol) {
return this;
}

@Override
public URLBuilder setUsername(String username) {
this.username = username;
return this;
Expand All @@ -171,16 +172,19 @@ public URLBuilder setPassword(String password) {
return this;
}

@Override
public URLBuilder setHost(String host) {
this.host = host;
return this;
}

@Override
public URLBuilder setPort(int port) {
this.port = port;
return this;
}

@Override
public URLBuilder setAddress(String address) {
int i = address.lastIndexOf(':');
String host;
Expand All @@ -196,11 +200,13 @@ public URLBuilder setAddress(String address) {
return this;
}

@Override
public URLBuilder setPath(String path) {
this.path = path;
return this;
}

@Override
public URLBuilder addParameterAndEncoded(String key, String value) {
if (StringUtils.isEmpty(value)) {
return this;
Expand Down Expand Up @@ -278,6 +284,7 @@ public URLBuilder addMethodParameter(String method, String key, String value) {
return this;
}

@Override
public URLBuilder addParameterIfAbsent(String key, String value) {
if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) {
return this;
Expand All @@ -300,6 +307,7 @@ public URLBuilder addMethodParameterIfAbsent(String method, String key, String v
return this;
}

@Override
public URLBuilder addParameters(Map<String, String> parameters) {
if (CollectionUtils.isEmptyMap(parameters)) {
return this;
Expand Down Expand Up @@ -332,6 +340,7 @@ public URLBuilder addMethodParameters(Map<String, Map<String, String>> methodPar
return this;
}

@Override
public URLBuilder addParametersIfAbsent(Map<String, String> parameters) {
if (CollectionUtils.isEmptyMap(parameters)) {
return this;
Expand All @@ -342,6 +351,7 @@ public URLBuilder addParametersIfAbsent(Map<String, String> parameters) {
return this;
}

@Override
public URLBuilder addParameters(String... pairs) {
if (pairs == null || pairs.length == 0) {
return this;
Expand All @@ -357,27 +367,31 @@ public URLBuilder addParameters(String... pairs) {
return addParameters(map);
}

@Override
public URLBuilder addParameterString(String query) {
if (StringUtils.isEmpty(query)) {
return this;
}
return addParameters(StringUtils.parseQueryString(query));
}

@Override
public URLBuilder removeParameter(String key) {
if (StringUtils.isEmpty(key)) {
return this;
}
return removeParameters(key);
}

@Override
public URLBuilder removeParameters(Collection<String> keys) {
if (CollectionUtils.isEmpty(keys)) {
return this;
}
return removeParameters(keys.toArray(new String[0]));
}

@Override
public URLBuilder removeParameters(String... keys) {
if (keys == null || keys.length == 0) {
return this;
Expand All @@ -388,16 +402,19 @@ public URLBuilder removeParameters(String... keys) {
return this;
}

@Override
public URLBuilder clearParameters() {
parameters.clear();
return this;
}

@Override
public boolean hasParameter(String key) {
String value = getParameter(key);
return StringUtils.isNotEmpty(value);
}

@Override
public boolean hasMethodParameter(String method, String key) {
if (method == null) {
String suffix = "." + key;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public DubboServiceAddressURL(URLAddress urlAddress, URLParam urlParam, URL cons
this.overrideURL = overrideURL;
}

@Override
protected <T extends URL> T newURL(URLAddress urlAddress, URLParam urlParam) {
return (T) new DubboServiceAddressURL(urlAddress, urlParam, this.consumerURL, this.overrideURL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,17 @@ public ServiceConfigURL(String protocol,
this(new PathURLAddress(protocol, username, password, path, host, port), URLParam.parse(parameters), attributes);
}

@Override
protected <T extends URL> T newURL(URLAddress urlAddress, URLParam urlParam) {
return (T) new ServiceConfigURL(urlAddress, urlParam, attributes);
}

@Override
public Map<String, Object> getAttributes() {
return attributes;
}

@Override
public Object getAttribute(String key) {
return attributes.get(key);
}
Expand All @@ -114,6 +117,7 @@ public URL addAttributes(Map<String, Object> attributes) {
return new ServiceConfigURL(getUrlAddress(), getUrlParam(), newAttributes);
}

@Override
public ServiceConfigURL putAttribute(String key, Object obj) {
Map<String, Object> newAttributes = new HashMap<>(attributes);
newAttributes.put(key, obj);
Expand Down
Loading

0 comments on commit 23e2f4d

Please sign in to comment.