All Products
Search
Document Center

SOFARPC Quick Start

Last Updated: Aug 25, 2020

This article shows you how to quickly get started with SOFARPC by using demo projects to publish and reference services, deploy applications, and govern services.

Demo Projects

  • Click to download SOFARPC Demo Projects.
  • Open myserver-app and myclient-app projects in the demo respectively by IDEA or Eclipse.

Service Validation

  • First, start the SOFABootWebSpringApplication of the myserver-app web module to publish a service.
  • Then, start the SOFABootWebSpringApplication reference service of the myclient-app web module to reference a service.

If the reference succeeds, the myclient-app console will output as follows:

  1. Response from myserver-app: Hello SOFARpc! times = xx

You can also view the service reference result in the log. You can set the log path in src/main/resources/config/application.properties under web module. The service reference result is provided in logs/myweb-app/common-default.log by default.

For more information about other local compilation methods, see Compilation and Execution.

Demo Explaination

Key information

  • groupId: the unique ID of the project organization. It is com.alipay.mytestsofa in the demo project.
  • artifactId: the artifact ID of the project. It is myserver-app in the demo project.
  • version: the version number. The default version is 1.0-SNAPSHOT.
  • package: the app package name. It is equivalent to the groupId by default.

Configuration

The annotation-based configurations for service publishing and reference are as follows:

  • Service Publishing:
    1. @SofaService(interfaceType = SampleService.class,bindings = @SofaServiceBinding(bindingType = "bolt"))
  • Service Reference:
    1. @SofaReference(interfaceType = SampleService.class,binding = @SofaReferenceBinding(bindingType = "bolt",directUrl = "127.0.0.1:12201"))
    2. private SampleService sampleService;

    Note:

    • ReferenceHolder was created to manage all referenced instances in the demo.
    • For other configuration means, please refer to Using API and Using XML.

RPC Principle of Demo Project

  • You should provide the same service interface and implementation at the same location in the endpoint module of two projects, and discover the service by using the annotation. The two projects are linked with each other through the same interface. One of the two projects serves as the client and the other as the server. In the local test, you can discover a service by using the testurl configuration in annotation. If tests are executed in the cloud server, you can use the DSR(Direct Server Return) to discover a service.
  • Start SOFABootWebSpringApplication in the myserver-app web module to publish a service.
  • Start SOFABootWebSpringApplication in the myclient-app web module to reference a service.

Service Publishing

The steps required to publish a service are as follows:

  1. Design Interface: Design the service interface class SampleService.java. The code is as follows.
    The interface path is com.alipay.samples.rpc.SampleService.
    1. /**
    2. * Service interface class
    3. */
    4. public interface SampleService {
    5. public String hello();
    6. }
  2. Implement Interface: Create the service implementation class SampleServiceImpl.java.

    • The interface implementation path is com.alipay.samples.rpc.impl.SampleServiceImpl.
    • The code is as follows:
      1. @Service
      2. @SofaService(interfaceType = SampleService.class,bindings = @SofaServiceBinding(bindingType = "bolt"))
      3. public class SampleServiceImpl implements SampleService {
      4. private int count = 0;
      5. @Override
      6. public String hello() {
      7. return "Hello SOFARpc! times = " + count++;
      8. }
      9. }
  3. Package Scan:

    1. @SpringBootApplication(scanBasePackages = {"com.alipay.mytestsofa","com.alipay.samples.rpc"})
    2. public class SOFABootWebSpringApplication {
    3. ...
    4. }
  4. Port Modification: The following configurations are for local test only. please remove them or comment them out before cloud deployment.
    • Add rpc.tr.port=12201 to application.properties. rpc.tr.port is a port number with a default value 0f 12200 in TR protocol.
    • Designate a server port before launch the project to avoid confict.
      1. System.setProperty(“server.port”, 8083”);
  5. Properties Configuration: Please guarantee the following properties are properly configured in application.properties before cloud deployment. For more information, please refer to Import SOFA Middleware。The configuration steps are as follows:

    1. Please visit SOFAStack Console > R&D Performance > Scaffold > Step 2 for following information:

      • com.alipay.instanceid: the unique ID of the app instance in the workspace. You can obtain this ID from Framework > Step 2 > Instance ID.
      • com.antcloud.antvip.endpoint: the AntVIP used by the app to obtain the server address of each component. The AntVIP varies with the region. You can obtain the value from Framework > Step 2 > AntVIP.
      • com.antcloud.mw.access and com.antcloud.mw.secret: the authentication keys for accessing the middleware. You can obtain it from the RAM console. For more information, see Create an AccessKey pair.
    2. Add the following information:

      1. run.mode=NORMAL
      2. com.alipay.env=shared

Service Reference

The object injection and service reference are achieved through annotation. In demo myclient-app, the steps required to reference a service are as follows:

  1. Object Injection: ReferenceHolder was created to manage all referenced instances in the demo.
    1. @Component
    2. public class ReferenceHolder {
    3. @SofaReference(interfaceType = SampleService.class,binding = @SofaReferenceBinding(bindingType = bolt”,directUrl = 127.0.0.1:12201”))
    4. private SampleService sampleService;
    5. public SampleService getSampleService() {
    6. return sampleService;
    7. }
    8. public void setSampleService(SampleService sampleService) {
    9. this.sampleService = sampleService;
    10. }
    11. }

    note: 12201 is configured in application.properties in web module in myserver-app project.

  2. Object Reference:

    1. @SpringBootApplication
    2. public class SOFABootWebSpringApplication {
    3. private static final Logger logger = LoggerFactory.getLogger(SOFABootWebSpringApplication.class);
    4. public static void main(String[] args) {
    5. //*************** Attention ******************//
    6. // This configuration is to avoid port conflict only. please remove or comment out before cloud deployment.
    7. System.setProperty("server.port", "8084");
    8. //This configuration is for local test only. please remove or comment out before cloud deployment.
    9. System.setProperty("run.mode", "TEST");
    10. //********************************//
    11. SpringApplication springApplication = new SpringApplication(SOFABootWebSpringApplication.class);
    12. ApplicationContext applicationContext = springApplication.run(args);
    13. if (logger.isInfoEnabled()) {
    14. printMsg("SofaRpc Application (myclient-app) started on 8084 port.");
    15. }
    16. //reference rpc service
    17. ReferenceHolder referenceHolder = applicationContext.getBean(ReferenceHolder.class);
    18. final SampleService sampleService = referenceHolder.getSampleService();
    19. new Thread(new Runnable() {
    20. @Override
    21. public void run() {
    22. while (true) {
    23. try {
    24. String response = sampleService.hello();
    25. printMsg("Response from myserver-app: " + response);
    26. } catch (Exception e) {
    27. e.printStackTrace();
    28. } finally {
    29. try {
    30. TimeUnit.SECONDS.sleep(3);
    31. } catch (InterruptedException e) {
    32. //ignore
    33. }
    34. }
    35. }
    36. }
    37. }).start();
    38. }
    39. private static void printMsg(String msg) {
    40. System.out.println(msg);
    41. if (logger.isInfoEnabled()) {
    42. logger.info(msg);
    43. }
    44. }
    45. }
  3. Port Modification: Add rpc.tr.port=12202 to application.properties. rpc.tr.port is a port number with a default value 0f 12200 in TR protocol.

    Note: This configuration is for local test only. please remove it or comment it out before cloud deployment.

  4. Properties Configuration: Please guarantee the following properties are properly configured in application.properties before cloud deployment. For detailed configuration,please refer to Properties Configuration.

Application Deployment

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

Cloud Governance

You are able to query and govern the deployed RPC service through SOFAStack Console > Middleware > Microservice Platform > Microservice. For more information, please refer to Service Management > View and Manage RPC Service