Skip to content

Commit

Permalink
Add a new Invoker class for making requests targeted at specified ip …
Browse files Browse the repository at this point in the history
…and port
  • Loading branch information
goodjava committed May 13, 2021
1 parent e419a2f commit 785e759
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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 java.io.Serializable;

public class Address implements Serializable {

private final String ip;

private final int port;

public Address(String ip, int port) {
this.ip = ip;
this.port = port;
}

public String getIp() {
return ip;
}

public int getPort() {
return port;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.rpc.Invoker;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.cluster.Cluster;
import org.apache.dubbo.rpc.cluster.Directory;

/**
* UnicastCluster
*/
public class UnicastCluster implements Cluster {

public static final String NAME = "unicast";

@Override
public <T> Invoker<T> join(Directory<T> directory) throws RpcException {
return new UnicastClusterInvoker<>(directory);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.URL;
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 java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
* UnicastClusterInvoker
*/
public class UnicastClusterInvoker<T> extends AbstractClusterInvoker<T> {

public UnicastClusterInvoker(Directory<T> directory) {
super(directory);
}

@Override
protected Result doInvoke(Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
Address address = Address.class.cast(invocation.getObjectAttachment("address"));
if (!Optional.ofNullable(address).isPresent()) {
throw new RpcException("Address can not be empty");
}
return invokers.stream().filter(it -> {
URL url = it.getUrl();
return address.getIp().equals(url.getIp()) && (address.getPort() == url.getPort()) && it.isAvailable();
}).findAny().orElseThrow(() -> new RpcException("No provider available in " + invokers.stream().map(it-> it.getUrl().getIp()+":"+it.getUrl().getPort()).collect(Collectors.joining(",")))).invoke(invocation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ available=org.apache.dubbo.rpc.cluster.support.AvailableCluster
mergeable=org.apache.dubbo.rpc.cluster.support.MergeableCluster
broadcast=org.apache.dubbo.rpc.cluster.support.BroadcastCluster
broadcast2=org.apache.dubbo.rpc.cluster.support.BroadcastCluster2
zone-aware=org.apache.dubbo.rpc.cluster.support.registry.ZoneAwareCluster
zone-aware=org.apache.dubbo.rpc.cluster.support.registry.ZoneAwareCluster
unicast=org.apache.dubbo.rpc.cluster.support.UnicastCluster

0 comments on commit 785e759

Please sign in to comment.