All Products
Search
Document Center

Reference SOFARPC Service

Last Updated: Aug 17, 2020

This topic takes you through a quick tutorial on how to reference a RPC service.

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.

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 the sofaboot-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:

  1. <dependency>
  2. <groupId>com.alipay.APPNAME</groupId>
  3. <artifactId>APPNAME-facade</artifactId>
  4. <version>1.0-SNAPSHOT</version>
  5. </dependency>
Note: The client app and server app are both named 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:

  1. <sofa:reference id="sampleRpcService"
  2. interface="com.alipay.APPNAME.facade.SampleService">
  3. <sofa:binding.bolt/>
  4. </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:

  1. @RestController
  2. @RequestMapping("/rpc")
  3. public class RpcTestController {
  4. @Autowired
  5. private SampleService sampleRpcService;
  6. @RequestMapping("/hello")
  7. String rpcUniqueAndTimeout() {
  8. String rpcResult = this.sampleRpcService.message();
  9. return rpcResult;
  10. }
  11. }

Compile locally

sofaboot-rpc-client is a SOFABoot Web project. Run the following commands in sequence to compile locally and start the RPC client:

  1. Configure run.mode in config/application.properties of both the client and the server as DEV, that is, run.mode=DEV.
  2. Run the mvn clean install command in the root directory of the project to generate the executable file target/APPNAME-web-1.0-SNAPSHOT-executable.jar.
  3. 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:
      1. 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.

Test an RPC service

  1. Start sofaboot-rpc-server on the server and sofaboot-rpc-client on the client.
  2. 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.
  1. Hello, Service SOFABoot

Application Deployment

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

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:

  1. 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.