Publish and reference JVM services
This topic describes how to publish and reference JVM services, including the following two parts:
3 ways to publish and reference JVM services. Includes:
XML mode
The configuration path of the XML is under the resources/META-INF/spring of the module where the service publishing and reference classes are located. By default, there is no spring folder. You need to create a new folder. When creating a new XML file, copy the required schema from the default XML generated under resources.
Publish service
The procedure is as follows:
Configure a bean:
<bean id="sampleService" class="com.alipay.sofa.runtime.test.service.SampleServiceImpl">Publish the above bean as a JVM service through the Spring extension tag provided by SOFA.
<sofa:service interface="com.alipay.sofa.runtime.test.service.SampleService" ref="sampleService"> <sofa:binding.jvm/> </sofa:service>Noteinterface refers to the interface that needs to be published as a service.
The ref points to the bean that needs to be published as a JVM service.
Reference service
The procedure is as follows:
Use the Spring extension tag to reference a service:
<sofa:reference interface="com.alipay.sofa.runtime.test.service.SampleService" id="sampleServiceRef"> <sofa:binding.jvm/> </sofa:reference>Noteinterface: the interface of the service, which must be the same as the interface configured when the service is published.
id: The preceding configuration generates a Spring bean with an id of sampleServiceRef.
You can also publish RPC services by using service and reference tags. For more information, see Publish a SOFARPC service.
Inject this bean anywhere in the Spring context of the current SOFABoot module.
Annotation mode
In addition to publishing JVM services and references through XML, SOFABoot also provides annotations to publish and reference JVM services.
Publish service
Use to add a @SofaService annotation to the implementation class. For this annotation, the description is as follows:
If a service has been annotated with
@SofaService, it can no longer publish the service in XML. The two methods cannot be mixed.The role of the
@SofaServiceis to publish a bean as a JVM service.Interface support
Single interface: When the class annotated by the
@SofaServicehas only one interface, the framework will directly use this interface as the interface of the service.Multiple interfaces: When the class being
@SofaService-annotated implements multiple interfaces, the@SofaServiceinterfaceTypefield can be set to specify the service interface.
Class annotations and method annotations
Class annotation: if
@SofaServiceannotation is placed on the implementation class, when Spring Boot uses Bean Method to create Bean, the@Beanand@SofaServicewill be scattered in two places, and different uniqueId cannot be used for the same implementation class.Method note: Since SOFABoot 2.6.0, the support
@SofaServiceis applied to the Bean Method.
Example:
Single interface implementation
@SofaService public class SampleImpl implements SampleInterface{ public void test(){ } }multi-interface implementation
@SofaService(interfaceType=SampleInterface.class) public class SampleImpl implements SampleInterface,Serializable{ public void test(){ } }Method annotations
@Configuration public class SampleSofaServiceConfiguration{ @Bean("sampleSofaService") @SofaService(uniqueId ="service1") SampleService service(){ return new SampleServiceImpl(""); } }
Reference service
Usage In the reference class, use the @SofaReference annotation, for the annotation, description is as follows:
@SofaReferenceif no service interface is specified, the type of the annotated field is used as the service interface.The service interface can be specified by setting the
interfaceTypeattribute of the@SofaReference.As of SOFABoot 2.6.0, JVM services can be referenced by using
@SofaReferenceannotations on bean method parameters.
Example:
Single interface implementation
public class SampleServiceRef{ @SofaReference private SampleService sampleService; }multi-interface implementation
public class SampleServiceRef{ @SofaReference(interfaceType=SampleService.class) private SampleService sampleService; }Method annotations
@Configuration public class MultiSofaReferenceConfiguration { @Bean("sampleReference") TestService service(@Value("$spring.application.name") String appName, @SofaReference(uniqueId = "service") SampleService service) { return new TestService(service); } }
Programming API mode
SOFABoot provides a set of programming APIs for publishing and referencing JVM services, making it easy to publish and reference JVM services directly in code.
publish service
The following example shows how to use the procedure:
First you need to implement the
ClientFactoryAwareinterface to get the programming component API:public class ClientFactoryBean implements ClientFactoryAware { private ClientFactory clientFactory; @Override public void setClientFactory(ClientFactory clientFactory) { this.clientFactory = clientFactory; } }Use the
clientFactoryto publish JVM services through the programming API:ServiceClient serviceClient = clientFactory.getClient(ServiceClient.class); ServiceParam serviceParam =new ServiceParam(); serviceParam.setInstance(new SampleServiceImpl()); serviceParam.setInterfaceType(SampleService.class); serviceClient.service(serviceParam);Code Description
The
ServiceClientobject is obtained byclientFactory.Constructs a
ServiceParamobject that contains the parameters required to publish the service.Set objects that need to be published as JVM services by
setInstancemethods.Set the interface of the service by
setInterfaceType.
Call the
ServiceClientservicemethod to publish a JVM service.
Reference service
The following example shows how to use the procedure:
First you need to implement the
ClientFactoryAwareinterface to get the programming component API. For more information, see Publish a service.Use the
clientFactoryto publish JVM services through the programming API:ReferenceClient referenceClient = clientFactory.getClient(ReferenceClient.class); ReferenceParam<SampleService> referenceParam =new ReferenceParam<SampleService>(); referenceParam.setInterfaceType(SampleService.class); SampleService proxy = referenceClient.reference(referenceParam);Code Description
Gets a
ReferenceClientfrom theClientFactory.Similar to publishing a service, a
ReferenceParamis constructed.Set the interface of the service.
Call the
ReferenceClientreferencemethod.The
Referenceobject created through the dynamic client is a very heavy object. Do not create it frequently when using it. Cache it yourself. Otherwise, there may be a risk of memory overflow.
uniqueId configuration
In some scenarios, for an interface, two services may need to be published, which correspond to different implementations. In this case, you can configure a uniqueId to distinguish between different implementations. The following section describes how to configure the uniqueId in the following three ways:
Example: sampleService there are two SampleService implementations, and both implementations need to be published as JVM Services.
Configure in XML
The following example shows the configuration steps:
Add the uniqueId when the service is published:
<sofa:service interface="com.alipay.sofa.runtime.test.service.SampleService" ref="sampleService1"unique-id="ss1"> </sofa:service> <sofa:service interface="com.alipay.sofa.runtime.test.service.SampleService" ref="sampleService2"unique-id="ss2"> </sofa:service>When you reference a service, you can specify a uniqueId to distinguish between different services:
If you want to use
sampleService1services, you can specifyunique-idasss1, for example:<sofa:reference interface="com.alipay.sofa.runtime.test.service.SampleService"id="sampleService"unique-id="ss1"> </sofa:reference>If you want to use
sampleService2services, you can specifyunique-idasss2, for example:<sofa:reference interface="com.alipay.sofa.runtime.test.service.SampleService"id="sampleService"unique-id="ss2"> </sofa:reference>
Configure in Annotation
When you use annotations to publish JVM services and references, you can set the uniqueId property of the @SofaService and @SofaReference.
Configure in programming API
When you publish or reference a JVM service by using a programming API, you can set the uniqueId by ServiceParam and ReferenceParam the setUniqueId method.