All Products
Search
Document Center

Publish SOFARPC Service

Last Updated: Aug 17, 2020

RPC is the most commonly used middleware in local development. In this tutorial, you can learn how to publish an RPC service in a SOFABoot Core project.

Demo Projects

Cloud Deployment Configuration

The followsing properties are required only by cloud deployment but not local test. Please guarantee the configuration before cloud deployment in application.properties:

  1. run.mode=NORMAL
  2. com.alipay.env=shared
  3. com.alipay.instanceid
  4. com.antcloud.antvip.endpoint
  5. com.antcloud.mw.access
  6. com.antcloud.mw.secret

For more information about the meanings of these parameters, see Import SOFA Middleware Component > Properties configuration.

Define and implement service interfaces

To publish an RPC service, we must first define an interface, as demonstrated below:

  1. // com.alipay.APPNAME.facade.SampleService
  2. public interface SampleService {
  3. String message();
  4. }

Then we must provide a default implementation for this interface, as demonstrated below:

  1. // com.alipay.APPNAME.service.SampleServiceImpl
  2. public class SampleServiceImpl implements SampleService {
  3. @Override
  4. public String message() {
  5. return "Hello, Service SOFABoot";
  6. }
  7. }

Meanwhile, configure the implementation as a Java Bean.

  1. <bean id="sampleServiceBean" class="com.alipay.APPNAME.service.SampleServiceImpl"/>

Note: For other configuration means, please refer to Using API and Using Annotation.

Publish the RPC service

The RPC service provider defines a service with <sofa:service>. Main attributes include interface, unique-id, and ref.

The service provider defines a service with <sofa:service> to publish the service; the service consumer defines a reference with <sofa:reference> to reference the service. Every SOFA app can publish services or reference services from others at the same time.

interface

SOFA services are defined as Java interfaces. The most important attribute is interface, which is used to determine the service and whose value consists of namespace package name and Java interface name.

ref

ref is used to specify the Spring Bean corresponding to the service implementation class with Bean ID.

In the example code below, the ref field value references the preceding sampleServiceBean.

  1. <! -- Service -->
  2. <sofa:service ref="sampleServiceBean" interface="com.alipay.APPNAME.facade.SampleService">
  3. <sofa:binding.bolt/>
  4. </sofa:service>

unique-id

If an interface has two different implementations, and both implementations must be published as RPC services in SOFA, you can configure the unique-id attribute to distinguish them.

  1. <! -- Service 1 -->
  2. <sofa:service ref="sampleServiceBean1" interface="com.alipay.APPNAME.facade.SampleService" unique-id="service1">
  3. <sofa:binding.bolt/>
  4. </sofa:service>
  5. <! -- Service 2 -->
  6. <sofa:service ref="sampleServiceBean2" interface="com.alipay.APPNAME.facade.SampleService" unique-id="service2">
  7. <sofa:binding.bolt/>
  8. </sofa:service>

Local Test

  1. Execute the mvn clean install command at the root directory of the project. An executable JAR file named APPNAME-service-1.0-SNAPSHOT-executable.jar is generated at the target directory. This is an executable fat jar file.
  2. Execute the JAR file in any of the following ways. If no error logs are exported, the sofaboot-rpc-server project is started.
  • Execute the java -jar APPNAME-service-1.0-SNAPSHOT-executable.jar command on the server.
  • Run the main function in the local IDE.

Application Deployment

Please refer to the following documents for local packaging and cloud deployment

View logs

  1. View the RPC service publication startup log logs/rpc/common-default.log of the sofaboot-rpc-server project. The RPC server is started if the log contains similar content as follows:

    1. 2016-12-17 15:16:44,466 INFO main - sofa rpc run.mode = DEV
    2. 2016-12-17 15:16:49,479 INFO main - PID:42843 sofa rpc starting!
  2. Then, view the logs/rpc/rpc-registry.log log. Sample content for reference:

    1. 2016-12-17 15:17:07,764 INFO main RPC-REGISTRY - Publish the RPC service: service name [com.alipay.APPNAME.facade.SampleService:1.0@DEFAULT]
  3. View the error log logs/rpc/common-error.log. If no error logs are exported and the app starts, it means that we have published an RPC service.

For more information about logs, see Log description.