Basic usage of the Bolt protocol

Updated at:

This topic describes how to publish and reference services that use the Bolt protocol.

Publish a service

To publish a service with the Bolt protocol using SOFARPC, you can add a Bolt binding. The following sections describe different ways to add the binding.

  • XML

    To publish a service with the Bolt protocol using XML, you can add a <sofa:binding.bolt/> tag within the <sofa:service> tag.

    <sofa:service ref="sampleService" interface="com.alipay.sofa.rpc.sample.SampleService">
        <sofa:binding.bolt/>
    </sofa:service>
  • Annotation

    To publish a service with the Bolt protocol using an annotation, you can set the bindingType of @SofaServiceBinding to bolt.

    @Service
    @SofaService(bindings ={@SofaServiceBinding(bindingType ="bolt")})
    public class SampleServiceImpl implements SampleService{
    }

  • Using the API in a Spring environment

    To publish a service with the Bolt protocol in a Spring or Spring Boot environment, you can add a BoltBindingParam object to the ServiceParam object.

    ServiceParam serviceParam =new ServiceParam();
    serviceParam.setInterfaceType(SampleService.class);// Set the service interface.
    serviceParam.setInstance(new SampleServiceImpl());// Set the implementation of the service interface.
    
    List<BindingParam> params =new ArrayList<BindingParam>();
    BindingParam serviceBindingParam =new BoltBindingParam();
    params.add(serviceBindingParam);
    serviceParam.setBindingParams(params);

  • Using the API in a non-Spring environment

    To publish a service with the Bolt protocol using the bare SOFARPC API in a non-Spring environment, you can assign a ServerConfig object with the protocol set to bolt to the corresponding ProviderConfig object.

    RegistryConfig registryConfig =new RegistryConfig()
    .setProtocol("zookeeper")
    .setAddress("127.0.0.1:2181");
    // Create a new ServerConfig with the protocol set to bolt.
    ServerConfig serverConfig =new ServerConfig()
    .setPort(8803)
    .setProtocol("bolt");
    ProviderConfig<SampleService> providerConfig =new ProviderConfig<SampleService>()
    .setInterfaceId(SampleService.class.getName())
    .setRef(new SampleServiceImpl())
    // Assign the ServerConfig to the ProviderConfig. This specifies that the service is published with the Bolt protocol.
    .setServer(serverConfig)  
    .setRegistry(registryConfig);
    providerConfig.export();

Referencing services

To reference a service with the Bolt protocol using SOFARPC, you can add a Bolt binding. The following sections describe different ways to add the binding.

  • XML

    To reference a service using the Bolt protocol in XML, add the <sofa:binding.bolt/> tag within the <sofa:reference> tag.

    <sofa:reference id="sampleService" interface="com.alipay.sofa.rpc.sample.SampleService">
    <sofa:binding.bolt/>
    </sofa:reference>

  • Annotation

    To reference a service with the Bolt protocol using an annotation, you can set the bindingType of @SofaReferenceBinding to bolt.

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

  • Using the API in a Spring environment

    To reference a service with the Bolt protocol in a Spring or Spring Boot environment, you can add a BoltBindingParam object to the ReferenceParam object.

    ReferenceClient referenceClient = clientFactory.getClient(ReferenceClient.class);
    ReferenceParam<SampleService> referenceParam =new ReferenceParam<SampleService>();
    referenceParam.setInterfaceType(SampleService.class);
    
    BindingParam refBindingParam =new BoltBindingParam();
    referenceParam.setBindingParam(refBindingParam);

  • Using the API in a non-Spring environment

    To reference a service with the Bolt protocol using the bare SOFARPC API in a non-Spring environment, you can set the protocol of the ConsumerConfig object to bolt.

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