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.<sofa:service ref="sampleService" interface="com.alipay.sofa.rpc.sample.SampleService">
<sofa:binding.bolt/>
</sofa:service>
- Annotation: To publish a Bolt service by using the Annotation mode, set
bindingType
of@SofaServiceBinding
tobolt
.@Service
@SofaService(bindings = {@SofaServiceBinding(bindingType = "bolt")})
public class SampleServiceImpl implements SampleService {
}
- API:
- configure as a Spring project: To publish a Bolt service in a Spring or Spring Boot project, add
BoltBindingParam
toServiceParam
.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);
- 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 correspondingProviderConfig
.RegistryConfig registryConfig = new RegistryConfig()
.setProtocol("zookeeper")
.setAddress("127.0.0.1:2181");
// Create a ServerConfig with the Bolt protocol.
ServerConfig serverConfig = new ServerConfig()
.setPort(8803)
.setProtocol("bolt");
ProviderConfig<SampleService> providerConfig = new ProviderConfig<SampleService>()
.setInterfaceId(SampleService.class.getName())
.setRef(new SampleServiceImpl())
.setServer(serverConfig) // Set ServerConfig for ProviderConfig, to indicate that the protocol for publishing this service is Bolt.
.setRegistry(registryConfig);
providerConfig.export();
- configure as a Spring project: To publish a Bolt service in a Spring or Spring Boot project, add
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.<sofa:reference id="sampleService" interface="com.alipay.sofa.rpc.sample.SampleService">
<sofa:binding.bolt/>
</sofa:reference>
- Annotation: To reference a Bolt service by using the Annotation mode, set
bindingType
of@SofaReferenceBinding
tobolt
.@SofaReference(binding = @SofaReferenceBinding(bindingType = "bolt"))
private SampleService sampleService;
API:
configure as a Spring project: To reference a Bolt service in a Spring or Spring Boot project, add
BoltBindingParam
toReferenceParam
.ReferenceClient referenceClient = clientFactory.getClient(ReferenceClient.class);
ReferenceParam<SampleService> referenceParam = new ReferenceParam<SampleService>();
referenceParam.setInterfaceType(SampleService.class);
BindingParam refBindingParam = new BoltBindingParam();
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
tobolt
.ConsumerConfig<SampleService> consumerConfig = new ConsumerConfig<SampleService>()
.setInterfaceId(SampleService.class.getName())
.setRegistry(registryConfig)
.setProtocol("bolt");
SampleService sampleService = consumerConfig.refer();