Dynamically Specify IP Call

Specify the target IP for this call before initiating a Dubbo call

Feature Description

When initiating an RPC request, it is necessary to specify the server for this call. Common scenarios include message callbacks, traffic isolation, etc.

Usage

Plugin Dependency

First, add the following plugin dependency to your project

  1. <dependency>
  2. <groupId>org.apache.dubbo.extensions</groupId>
  3. <artifactId>dubbo-cluster-specify-address-dubbo3</artifactId>
  4. <version>3.3.0</version>
  5. </dependency>

For Dubbo 2 version

  1. <dependency>
  2. <groupId>org.apache.dubbo.extensions</groupId>
  3. <artifactId>dubbo-cluster-specify-address-dubbo2</artifactId>
  4. <version>1.0.0</version>
  5. </dependency>

Call Example

  1. ReferenceConfig<DemoService> referenceConfig = new ReferenceConfig<>();
  2. // ... init
  3. DemoService demoService = referenceConfig.get();
  4. // for invoke
  5. // 1. find 10.10.10.10:20880 exist
  6. // 2. if not exist, create a invoker to 10.10.10.10:20880 if `needToCreate` is true (only support in Dubbo 3.x's implementation)
  7. UserSpecifiedAddressUtil.setAddress(new Address("10.10.10.10", 20880, true));
  8. demoService.sayHello("world");
  9. // for invoke
  10. // 1. find 10.10.10.10:any exist
  11. // 2. if not exist, create a invoker to 10.10.10.10:20880 if `needToCreate` is true (only support in Dubbo 3.x's implementation)
  12. UserSpecifiedAddressUtil.setAddress(new Address("10.10.10.10", 0, true));
  13. demoService.sayHello("world");

Parameter Description

The parameters for specifying IP calls revolve around the Address object. The parameter types are as follows:

  1. package org.apache.dubbo.rpc.cluster.specifyaddress;
  2. public class Address implements Serializable {
  3. // ip - priority: 3
  4. private String ip;
  5. // ip+port - priority: 2
  6. private int port;
  7. // address - priority: 1
  8. private URL urlAddress;
  9. private boolean needToCreate = false;
  10. // ignore setter and getter
  11. }
  1. urlAddress has the highest priority; if a target URL is specified, it will be used first. (No further matching will be performed)
  2. ip + port (non-0 port) is the second priority; matching will be done from addresses already pushed by the registry. (No further matching will be performed)
  3. ip is the third priority; matching will be done from addresses already pushed by the registry.

Specifically, if needToCreate is specified as true, an invoker will be automatically constructed based on the passed parameters. For addresses specified via ip (+ port), the parameters of the first address in the registry will be used as a template for creation; if no address is available, it will be automatically created based on the Dubbo protocol. If you need customized invoker creation logic, please implement the org.apache.dubbo.rpc.cluster.specifyaddress.UserSpecifiedServiceAddressBuilder SPI interface. (This feature is only supported by Dubbo 3 implementation)

Before each request, the constructed Address parameter should be passed to the Dubbo framework through the UserSpecifiedAddressUtil utility class.

  1. package org.apache.dubbo.rpc.cluster.specifyaddress;
  2. public class UserSpecifiedAddressUtil {
  3. public static void setAddress(Address address) { ... }
  4. }

You must set it every time, and you must immediately make the call after setting it. If an interceptor error occurs (the removal of this value in the Dubbo framework happens during the address selection process), it is recommended to set null to avoid memory leaks in ThreadLocal, which may affect subsequent calls.

Feedback

Was this page helpful?

Yes No

Last modified September 30, 2024: Update & Translate Overview Docs (#3040) (d37ebceaea7)