Publish and reference JVM services

Updated at:

This topic describes how to publish and reference JVM services, including the following two parts:

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:

  1. Configure a bean:

    <bean id="sampleService" class="com.alipay.sofa.runtime.test.service.SampleServiceImpl">
  2. 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>
    Note

    • interface 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:

  1. 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>
    Note

    • interface: 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.

  2. 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 @SofaService is to publish a bean as a JVM service.

  • Interface support

    • Single interface: When the class annotated by the @SofaService has 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 @SofaService interfaceType field can be set to specify the service interface.

  • Class annotations and method annotations

    • Class annotation: if @SofaService annotation is placed on the implementation class, when Spring Boot uses Bean Method to create Bean, the @Bean and @SofaService will 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 @SofaService is 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:

  • @SofaReference if 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 interfaceType attribute of the @SofaReference.

  • As of SOFABoot 2.6.0, JVM services can be referenced by using @SofaReference annotations 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:

  1. First you need to implement the ClientFactoryAware interface to get the programming component API:

    public class ClientFactoryBean implements ClientFactoryAware {
        private ClientFactory clientFactory;
    
        @Override
        public void setClientFactory(ClientFactory clientFactory) {
            this.clientFactory = clientFactory;
        }
    }
  2. Use the clientFactory to 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 ServiceClient object is obtained by clientFactory.

    • Constructs a ServiceParam object that contains the parameters required to publish the service.

      • Set objects that need to be published as JVM services by setInstance methods.

      • Set the interface of the service by setInterfaceType.

    • Call the ServiceClient service method to publish a JVM service.

Reference service

The following example shows how to use the procedure:

  1. First you need to implement the ClientFactoryAware interface to get the programming component API. For more information, see Publish a service.

  2. Use the clientFactory to 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 ReferenceClient from the ClientFactory.

    • Similar to publishing a service, a ReferenceParam is constructed.

    • Set the interface of the service.

    • Call the ReferenceClient reference method.

    • The Reference object 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:

  1. 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>
  2. When you reference a service, you can specify a uniqueId to distinguish between different services:

    • If you want to use sampleService1 services, you can specify unique-id as ss1, for example:

      <sofa:reference interface="com.alipay.sofa.runtime.test.service.SampleService"id="sampleService"unique-id="ss1">
      </sofa:reference>
    • If you want to use sampleService2 services, you can specify unique-id as ss2, 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.