All Products
Search
Document Center

BOLT Basics

Last Updated: Aug 17, 2020

Publish a service

To use SOFARPC to publish a Bolt service, add a Bolt Binding. The modes to add Bolt Bindings are as follows:

  • XML: To publish a Bolt service by using the XML mode, add the <sofa:binding.bolt> tag to the <sofa:service> tag.
    1. <sofa:service ref="sampleService" interface="com.alipay.sofa.rpc.sample.SampleService">
    2. <sofa:binding.bolt/>
    3. </sofa:service>
  • Annotation: To publish a Bolt service by using the Annotation mode, set bindingType of @SofaServiceBinding to bolt.
    1. @Service
    2. @SofaService(bindings = {@SofaServiceBinding(bindingType = "bolt")})
    3. public class SampleServiceImpl implements SampleService {
    4. }
  • API:
    • configure as a Spring project: To publish a Bolt service in a Spring or Spring Boot project, add BoltBindingParam to ServiceParam.
      1. ServiceParam serviceParam = new ServiceParam();
      2. serviceParam.setInterfaceType(SampleService.class); // Set the service interface.
      3. serviceParam.setInstance(new SampleServiceImpl()); // Set the implementation of the service interface.
      4. List<BindingParam> params = new ArrayList<BindingParam>();
      5. BindingParam serviceBindingParam = new BoltBindingParam();
      6. params.add(serviceBindingParam);
      7. serviceParam.setBindingParams(params);
    • configure without a Spring framework: To provide a Bolt service by using raw APIs of SOFARPC in a non-Spring project, set the ServerConfig whose protocol is Bolt for the corresponding ProviderConfig.
      1. RegistryConfig registryConfig = new RegistryConfig()
      2. .setProtocol("zookeeper")
      3. .setAddress("127.0.0.1:2181");
      4. // Create a ServerConfig with the Bolt protocol.
      5. ServerConfig serverConfig = new ServerConfig()
      6. .setPort(8803)
      7. .setProtocol("bolt");
      8. ProviderConfig<SampleService> providerConfig = new ProviderConfig<SampleService>()
      9. .setInterfaceId(SampleService.class.getName())
      10. .setRef(new SampleServiceImpl())
      11. .setServer(serverConfig) // Set ServerConfig for ProviderConfig, to indicate that the protocol for publishing this service is Bolt.
      12. .setRegistry(registryConfig);
      13. providerConfig.export();

Reference services

To use SOFARPC to reference a Bolt service, you only need to add a Bolt binding. The modes to add a Bolt Binding are as follows:

  • XML:To reference a Bolt service by using the XML mode, add the <sofa:binding.bolt> tag to the <sofa:reference> tag.
    1. <sofa:reference id="sampleService" interface="com.alipay.sofa.rpc.sample.SampleService">
    2. <sofa:binding.bolt/>
    3. </sofa:reference>
  • Annotation: To reference a Bolt service by using the Annotation mode, set bindingType of @SofaReferenceBinding to bolt.
    1. @SofaReference(binding = @SofaReferenceBinding(bindingType = "bolt"))
    2. private SampleService sampleService;
  • API

    • configure as a Spring project: To reference a Bolt service in a Spring or Spring Boot project, add BoltBindingParam toReferenceParam.

      1. ReferenceClient referenceClient = clientFactory.getClient(ReferenceClient.class);
      2. ReferenceParam<SampleService> referenceParam = new ReferenceParam<SampleService>();
      3. referenceParam.setInterfaceType(SampleService.class);
      4. BindingParam refBindingParam = new BoltBindingParam();
      5. referenceParam.setBindingParam(refBindingParam);
    • configure without a Spring framework: To reference a Bolt service by using raw APIs of SOFARPC in a non-Spring project, set the protocol of ConsumerConfig to bolt.

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