SOFARPC supports direct calls to a specified server-side address. This topic describes the scenarios and configuration methods for direct calls.
Notes
Configuring a direct connection disables registry-based software load balancing.
You can configure multiple addresses for a direct connection by separating them with commas (,) or semicolons (;). Load balancing is supported among these addresses.
Scenarios
There are two main scenarios:
Production scenario
To configure a service reference in an XML file, add a
routetag inside thesofa:binding.bolttag. In theroutetag, add atarget-urlattribute and set its value to the address to invoke.<sofa:reference id="sampelService" interface="com.alipay.test.SampleService"> <sofa:binding.bolt> <sofa:route target-url="${targetUrl}:port"/> </sofa:binding.bolt> </sofa:reference>Replace
${targetUrl}:portwith the actual address and port. For more information, see Configuration methods.Test scenario
Set
run_mode=TESTin theapplication.propertiesfile.When you configure a service reference in an XML file, add a global-attrs tag inside the
sofa:binding.bolttag. Add atest-urlattribute to theglobal-attrstag and set its value to the target address.
<sofa:reference id="sampleService" interface="com.alipay.test.SampleService"> <sofa:binding.bolt> <sofa:global-attrs test-url="${targetUrl}:port"/> </sofa:binding.bolt> </sofa:reference>Importanttest-urlapplies only to the XML configuration method.test-urltakes effect only whenrun_mode=TESTis set. It is mainly used for testing.
Configuration methods
API method
You can configure a direct call using the API method. The address must be in the
protocol://IP:portformat. The following code provides an example:ConsumerConfig<HelloService> consumer =new ConsumerConfig<HelloService>() .setInterfaceId(HelloService.class.getName()) .setRegistry(registryConfig) .setDirectUrl("bolt://127.0.0.1:12201");Annotation method
You can configure a direct call using the annotation method. The address must be in the
IP:portformat. The following code provides an example:@SofaReference(binding =@SofaReferenceBinding(bindingType ="bolt", directUrl ="127.0.0.1:12220")) private SampleService sampleService;XML method
You can configure a direct call using the XML method. The address must be in the
IP:portformat. The following code provides an example:<sofa:reference id="sampleService" interface="com.alipay.test.SampleService"> <sofa:binding.bolt> <sofa:global-attrs target-url="127.0.0.1:12200"/> </sofa:binding.bolt> </sofa:reference>