Direct calls

更新时间:
复制 MD 格式

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 route tag inside the sofa:binding.bolt tag. In the route tag, add a target-url attribute 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}:port with the actual address and port. For more information, see Configuration methods.

  • Test scenario

    • Set run_mode=TEST in the application.properties file.

    • When you configure a service reference in an XML file, add a global-attrs tag inside the sofa:binding.bolt tag. Add a test-url attribute to the global-attrs tag 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>
    Important
    • test-url applies only to the XML configuration method.

    • test-url takes effect only when run_mode=TEST is 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:port format. 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:port format. 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:port format. 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>