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:
Design an interface in the
app/endpoint/folder. The following is an example:// com.alipay.APPNAME.facade.SampleService public interface SampleService{ String message(); }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!"; } }In the module that contains the interface, configure the implementation class as a Java bean in the XML file in the
resources/META-INFdirectory.<bean id="sampleServiceBean" class="com.alipay.APPNAME.service.SampleServiceImpl"/>In the module that contains the interface, publish the RPC service in the XML file in the
resources/META-INFdirectory. 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>NoteA 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 areinterface,unique-id, andref.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 theunique-idproperty 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
In the root directory of the project, run the
mvn clean installcommand. This generates an executable Fat JAR file in thetargetdirectory, such asAPPNAME-service-1.0-SNAPSHOT-executable.jar.Run the JAR file in one of the following ways. If no error logs are generated, the
sofaboot-rpc-serverproject has started successfully.Run the
java -jar APPNAME-service-1.0-SNAPSHOT-executable.jarcommand from the command line.Run the
mainfunction 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:
Classic application services: Quick Start.
Containerized application services: Quick Start.
When you publish to the cloud, configure the following properties in application.properties:
run.mode=NORMALcom.alipay.env=sharedcom.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
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!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]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.