Publish a SOFARPC service

更新时间:
复制 MD 格式

Remote Procedure Call (RPC) is a middleware commonly used in development. This topic describes how to publish an RPC service.

Prerequisites

  • You have set up the environment. For more information, see Set up the environment.

  • You have downloaded the sample project.

  • You have imported the project into your Integrated Development Environment (IDE). For more information, see Quick Start.

Publish a SOFARPC service

To publish an RPC service, follow these steps:

  1. Design an interface in the app/endpoint/ folder. The following is an example:

    // com.alipay.APPNAME.facade.SampleService
    public interface SampleService{
        String message();
    }
  2. Provide an implementation class for the interface. The following is an example:

    // com.alipay.APPNAME.service.SampleServiceImpl
    public class SampleServiceImpl implements SampleService{
       @Override
       public String message(){
            return "Hello, Service SOFABootRPC!";
       }
    }
  3. In the module that contains the interface, configure the implementation class as a Java bean in the XML file in the resources/META-INF directory.

    <bean id="sampleServiceBean" class="com.alipay.APPNAME.service.SampleServiceImpl"/>
  4. In the module that contains the interface, publish the RPC service in the XML file in the resources/META-INF directory. The configuration varies based on the number of implementation classes:

    • For a single interface with a single implementation, use the following configuration:

      <!-- Service -->
      <sofa:service ref="sampleServiceBean" interface="com.alipay.APPNAME.facade.SampleService">
      <sofa:binding.bolt/>
      </sofa:service>
    • For a single interface with multiple implementations, use the following configuration:

      <!-- Service 1 -->
      <sofa:service ref="sampleServiceBean1" interface="com.alipay.APPNAME.facade.SampleService" unique-id="service1">
      <sofa:binding.bolt/>
      </sofa:service>
      
      <!-- Service 2 -->
      <sofa:service ref="sampleServiceBean2" interface="com.alipay.APPNAME.facade.SampleService" unique-id="service2">
      <sofa:binding.bolt/>
      </sofa:service>
      Note
      • A service provider defines a service using <sofa:service> and publishes the service. A service consumer defines a service reference using <sofa:reference> and references the service. Any application node in the SOFA framework can simultaneously publish services and reference services from other nodes.

      • An RPC service provider is defined using the <sofa:service> tag. The main properties are interface, unique-id, and ref.

        • interface: This property identifies a service. Its value is a combination of the namespace package name and the Java interface name.

        • unique-id: If an interface has two different implementations and both need to be published as SOFA RPC services, use the unique-id property to distinguish between them.

        • ref: This property specifies the Spring Bean that corresponds to the service implementation. It associates the service implementation class with a Bean ID.

Run locally

  1. In the root directory of the project, run the mvn clean install command. This generates an executable Fat JAR file in the target directory, such as APPNAME-service-1.0-SNAPSHOT-executable.jar.

  2. Run the JAR file in one of the following ways. If no error logs are generated, the sofaboot-rpc-server project has started successfully.

    • Run the java -jar APPNAME-service-1.0-SNAPSHOT-executable.jar command from the command line.

    • Run the main function directly in your local IDE.

Run in the cloud

You can test SOFARPC locally using a direct connection. After you publish the service to the cloud, you can manage it from the SOFAStack console. To publish to the cloud, see the following documents:

  • For the general application publishing flow, see Technology stack guide.

  • For detailed publishing steps, see the document that corresponds to your publishing method:

Note

When you publish to the cloud, configure the following properties in application.properties:

  • run.mode=NORMAL

  • com.alipay.env=shared

  • com.alipay.instanceid=

  • com.antcloud.antvip.endpoint=

  • com.antcloud.mw.access=

  • com.antcloud.mw.secret=

The run mode and environment values are default static fields. For more information about parameter configuration, see Import SOFA middleware.

View logs

  1. View the startup log for the locally published RPC service in logs/rpc/common-default.log.

    If the log contains content similar to the following, the RPC server has started successfully:

    2016-12-1715:16:44,466 INFO  main                             - sofa rpc run.mode = DEV
    2016-12-1715:16:49,479 INFO  main                             - PID:42843 sofa rpc starting!
  2. View the log in logs/rpc/rpc-registry.log. The content is similar to the following:

    2016-12-1715:17:07,764 INFO  main                             RPC-REGISTRY -Publish RPC service: Service name[com.alipay.APPNAME.facade.SampleService:1.0@DEFAULT]
  3. View the error log in logs/rpc/common-error.log.

    If no errors are logged and the application starts normally, the RPC service was published successfully.

For more information about logs, see Log description.