文档

超时控制

更新时间:

使用 BOLT 协议进行通信时,SOFARPC 的超时时间默认为 3000 毫秒,您可以在引用服务时设置超时时间,也可以在服务以及方法的维度设置超时时间。SOFARPC 的超时时间的设置的单位都为毫秒。

服务维度

如果您需要在发布服务时,在服务维度设置超时时间,可以设置 timeout 参数。

XML 方式

如果使用 XML 的方式引用服务,设置 <sofa:binding.bolt> 标签下的 <sofa:global-attrs> 标签的 timeout 属性的值即可。

<sofa:reference interface="com.example.demo.SampleService" id="sampleService">
    <sofa:binding.bolt>
        <sofa:global-attrs timeout="2000"/>
    </sofa:binding.bolt>
</sofa:reference>

Annotation 方式

如果使用 Annotation 引用服务,设置 @SofaReferenceBindingtimeout 属性的值即可。

@SofaReference(binding =@SofaReferenceBinding(bindingType ="bolt", timeout =2000))
private SampleService sampleService;

Spring 环境 API 方式

如果在 Spring 或者 Spring Boot 的环境下引用服务,设置 BoltBindingParamtimeout 属性的值即可。

BoltBindingParam boltBindingParam =new BoltBindingParam();
boltBindingParam.setTimeout(2000)

非 Spring 环境下 API 方式

如果在非 Spring 环境下直接使用 SOFARPC 的裸 API 引用服务,设置 ConsumerConfigtimeout 属性即可。

ConsumerConfig<SampleService> consumerConfig =new ConsumerConfig<SampleService>()
.setInterfaceId(SampleService.class.getName())
.setRegistry(registryConfig)
.setProtocol("bolt")
.setTimeout(2000);

方法维度

如果您希望单独调整一个服务中某一个方法的超时时间,可以在方法维度上设置超时时间。对于某一个方法,优先生效方法维度的超时时间,如果没有设置,则使用服务维度的超时时间。

XML 方式

如果使用 XML 的方式引用一个服务,设置对应的 <sofa:method> 标签的 timeout 属性即可。

<sofa:reference interface="com.example.demo.SampleService" id="sampleService">
    <sofa:binding.bolt>
        <sofa:method name="hello" timeout="2000"/>
    </sofa:binding.bolt>
</sofa:reference>

Annotation 方式

目前暂未提供通过 Annotation 的方式来设置方法级别的超时时间。

Spring 环境 API 方式

如果在 Spring 或者 Spring Boot 的环境下引用服务,设置 RpcBindingMethodInfotimeout 属性的值即可。

BoltBindingParam boltBindingParam =new BoltBindingParam();

RpcBindingMethodInfo rpcBindingMethodInfo =new RpcBindingMethodInfo();
rpcBindingMethodInfo.setName("hello");
rpcBindingMethodInfo.setTimeout(2000);

List<RpcBindingMethodInfo> rpcBindingMethodInfos =new ArrayList<>();
rpcBindingMethodInfos.add(rpcBindingMethodInfo);

boltBindingParam.setMethodInfos(rpcBindingMethodInfos);

非 Spring 环境 API 方式

如果在非 Spring 环境下使用 SOFARPC 的裸 API 引用服务,设置 MethodConfigtimeout 属性即可。

MethodConfig methodConfig =new MethodConfig();
methodConfig.setName("hello");
methodConfig.setTimeout(2000);

List<MethodConfig> methodConfigs =new ArrayList<MethodConfig>();
methodConfigs.add(methodConfig);

ConsumerConfig<SampleService> consumerConfig =new ConsumerConfig<SampleService>()
.setInterfaceId(SampleService.class.getName())
.setRegistry(registryConfig)
.setProtocol("bolt")
.setMethods(methodConfigs);
  • 本页导读 (0)
文档反馈