Remote Procedure Call (RPC) is a common middleware used in development. This topic describes how to reference 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 an Integrated Development Environment (IDE). For more information, see Quick Start.
Import the interface definition dependency
To reference an RPC service, you must know the interface that the service provider published. If the service has a unique-id, you must also know its value. The service provider must upload the JAR package that contains the interface and its dependencies to a Maven repository. This allows the service consumer to reference the RPC service.
If you run the project locally, you can run the
mvn clean installcommand in thesofaboot-rpc-serverproject directory to install the interface dependency JAR to the local repository.If you do not run the project locally, you can upload the interface dependency JAR to the corresponding Maven repository.
After you obtain the published interface of the RPC service, you can add the interface dependency information for the referenced RPC service to the main pom file in the sofaboot-rpc-client project. The following code shows an example:
<dependency>
<groupId>com.alipay.APPNAME</groupId>
<artifactId>APPNAME-facade</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>In this example, the application name for both the client and the server is APPNAME. This is for learning purposes only. In a real environment, the two application names cannot be identical.
Reference the RPC service
In the META-INF/APPNAME/APPNAME-web.xml configuration file, you can reference an RPC service based on the interface configuration:
<sofa:reference id="sampleRpcService" interface="com.alipay.APPNAME.facade.SampleService">
<sofa:binding.bolt/>
</sofa:reference>This RPC reference is also a bean. Its bean ID is sampleRpcService.
Inject the referenced RPC service into a controller
This step is for demonstration purposes. It allows a user to access a REST interface from a browser or another method. This triggers a call to the referenced service and then to the server-side. In actual development, do not inject the service into a controller.
This tutorial injects the RPC service into com.alipay.APPNAME.web.springrest.RpcTestController. The following code shows an example:
@RestController
@RequestMapping("/rpc")
public class RpcTestController{
@Autowired
private SampleService sampleRpcService;
@RequestMapping("/hello")
String rpcUniqueAndTimeout(){
String rpcResult =this.sampleRpcService.message();
return rpcResult;
}
}Compile locally
sofaboot-rpc-client is a SOFABoot Web project. You can run the following commands to compile locally and start the RPC client:
In the
config/application.propertiesfile for both the client and the server, you can setrun.modetoDEV. The configuration isrun.mode=DEV.In the project root directory, you can run the
mvn clean installcommand to generate the executable filetarget/APPNAME-web-1.0-SNAPSHOT-executable.jar.In the project root directory, you can run the
java -jar ./target/APPNAME-web-1.0-SNAPSHOT-executable.jarcommand.If the console displays the following output, the web container started successfully.
16:11:13.625 INFO org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer-Tomcat started on port(s):8080(http)If an error message is displayed, you can resolve the issue and retry the previous steps.
Test the RPC service
You can start the RPC server
sofaboot-rpc-serverand the clientsofaboot-rpc-client.In a browser, you can go to
http://localhost:8080/rpc/helloto test the referenced RPC service.If the browser displays the following output, the RPC service was published and referenced successfully.
Hello,ServiceSOFABoot
Run in the cloud
SOFARPC can only be tested locally through a direct connection. After the service is published in the cloud, you can manage it from the SOFAStack console. For more information about how to publish to the cloud, see the following topics:
For the overall application publishing flow, see Technology stack guide.
For detailed steps to publish an application, see the following documents based on your publishing method:
For classic application services, see Quick Start.
For containerized application services, see Quick Start.
When you publish to the cloud, you must 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 values for the run mode and run environment are default static fields. For more information about how to configure other parameters, see Import SOFA middleware.
View logs
You can view the RPC service startup log logs/rpc/rpc-registry.log in the sofaboot-rpc-client project. If a log entry similar to the following appears, the RPC service was referenced successfully.
2016-12-1715:45:50,340 INFO main RPC-REGISTRY -Subscribe to RPC service: Service name[com.alipay.APPNAME.facade.SampleService:1.0@DEFAULT]If the RPC service reference fails, you can check all common-error.log files in the logs directory.
For more information about logs, see Log description.