This topic takes you through a quick tutorial on how to reference a RPC service.
Demo Projects
- Please refer to Project Environment for project configuration.
- Click to download SOFARPC Demo Projects.
- Open
myserver-app
andmyclient-app
projects in the demo respectively by IDEA or Eclipse.
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
:
- run.mode=NORMAL
- com.alipay.env=shared
- com.alipay.instanceid
- com.antcloud.antvip.endpoint
- com.antcloud.mw.access
- com.antcloud.mw.secret
For more information about the meanings of these parameters, see Import SOFA Middleware Component > Properties configuration.
Import interface dependencies
To reference an RPC service, you need to know the interface published by the service provider. If the published service has a unique-id
, you also need to know the unique-id
. Therefore, the service provider is required to transmit the JAR file of the published interface and the dependency information to the Maven repository, so that the RPC service can be referenced.
- For local environment, you need to run the
mvn clean install
command in thesofaboot-rpc-server
project to install the dependent JAR file in the local repository. - For non-local environment, you need to upload the dependent JAR file to the corresponding Maven repository.
After obtaining the interface to publish the RPC service, you need to add the interface dependency of the referenced RPC service to the master pom file in the sofaboot-rpc-client project. The specific configuration is as follows:
<dependency>
<groupId>com.alipay.APPNAME</groupId>
<artifactId>APPNAME-facade</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
APPNAME
for learning purposes only. In actual production environment, the two app names must not be the same.Reference an RPC service
Reference an RPC service in the META-INF/APPNAME/APPNAME-web.xml
configuration file based on the interface configuration:
<sofa:reference id="sampleRpcService"
interface="com.alipay.APPNAME.facade.SampleService">
<sofa:binding.bolt/>
</sofa:reference>
The referenced RPC service should also be configured as a Bean. The Bean ID is sampleRpcService
.
Note: For other configuration means, please refer to Using API and Using Annotation.
Inject the referenced RPC service into a Controller
Note: This is designed to demonstrate how you can access a REST interface through a browser or in other ways, trigger to call the referenced service, and then call it on the server. In practice, you do not need to inject the service to a Controller.
In this tutorial, you inject this RPC service to com.alipay.APPNAME.web.springrest.RpcTestController
, as demonstrated below:
@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. Run the following commands in sequence to compile locally and start the RPC client:
- Configure
run.mode
inconfig/application.properties
of both the client and the server asDEV
, that is,run.mode=DEV
. - Run the
mvn clean install
command in the root directory of the project to generate the executable filetarget/APPNAME-web-1.0-SNAPSHOT-executable.jar
. - Run
java -jar ./target/APPNAME-web-1.0-SNAPSHOT-executable.jar
in the root directory of the project.- The Web container is started if the console exports the following information:
16:11:13.625 INFO org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer - Tomcat started on port(s): 8080 (http)
- If an error message is exported, solve the corresponding problem and try the above steps again.
- The Web container is started if the console exports the following information:
Test an RPC service
- Start sofaboot-rpc-server on the server and sofaboot-rpc-client on the client.
- Go to http://localhost:8080/rpc/hello in a browser to test the referenced RPC service. When the browser exports the following information, it indicates that the RPC service has been published and referenced.
Hello, Service SOFABoot
Application Deployment
Please refer to the following documents for local packaging and cloud deployment
- Packaging: please see Compilation and Execution.
- Cloud Deployment
- For general procedures, please see Buildpacks and Application Deployment Procedure in Buildpacks User Guide.
- For detailed procedures, please see Quick Start under Classic Application Service.
View logs
View the RPC service reference startup log of the sofaboot-rpc-client project: view logs/rpc/rpc-registry.log
. Sample content for reference:
2016-12-17 15:45:50,340 INFO main RPC-REGISTRY - Subscribe to the RPC service: service name [com.alipay.APPNAME.facade.SampleService:1.0@DEFAULT]
The log above indicates that the RPC service is referenced. If an RPC service reference fails, pay close attention to all the common-error.log
logs in the logs
directory.
For more information about logs, see Log description.