-
Notifications
You must be signed in to change notification settings - Fork 26.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new Invoker class for making requests targeted at specified ip …
…and port
- Loading branch information
Showing
4 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/Address.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/UnicastCluster.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/UnicastClusterInvoker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters