Build microservices with SOFAStack

Procedure

This guide introduces how to quickly build a microservice based on SOFAStack. It mainly includes the following steps.

  • Publish service using SOFABoot and SOFARPC
  • Call service using SOFABoot and SOFARPC
  • View Tracer information reported by SOFATracer via ZipKin
  • View Metrics information via SOFALookout

Architecture

Architecture

Tasks

1. Preparation

Clone the project demo from GitHub to local

git clone https://github.com/sofastack-guides/kc-sofastack-demo.git

Import the project into IDEA or Eclipse. After import, the interface is as follows:

imported project

  • balance-mng: account management system, providing deduction balance service
  • stock-mng: account system, providing deduction inventory service

2、Introduce dependencies

Add the following dependencies into the pom.xml files of balance-mng and stock-mng project modules.

<!--SOFARPC dependency-->
<dependency>
    <groupId>com.alipay.sofa</groupId>
    <artifactId>rpc-sofa-boot-starter</artifactId>
</dependency>
<!--SOFATracer dependency-->
<dependency>
    <groupId>com.alipay.sofa</groupId>
    <artifactId>tracer-sofa-boot-starter</artifactId>
</dependency>
<!--SOFARegistry dependency-->
<dependency>
    <groupId>com.alipay.sofa</groupId>
    <artifactId>registry-client-all</artifactId>
</dependency>
<!--runtime dependency-->
<dependency>
    <groupId>com.alipay.sofa</groupId>
    <artifactId>runtime-sofa-boot-starter</artifactId>
</dependency>
<!--SOFALookout dependency-->
<dependency>
    <groupId>com.alipay.sofa.lookout</groupId>
    <artifactId>lookout-sofa-boot-starter</artifactId>
</dependency>

For balance-mng project, you need to introduce the dependencies into the pom file of balance-mng-imp module.

introduce dependencies in balance-mng

For stock-mng project, you need to introduce the dependencies into the pom file of stock-mng module.

introduce dependencies in stock-mng

3. Add configurations

Copy the following configurations into the application.properties file of balance-mng and stock-mng project module.

# 1、Add Service Registry address
com.alipay.sofa.rpc.registry.address=sofa://118.31.43.62:9603
# 2、Add the zipkin address where tracer data is reported
com.alipay.sofa.tracer.zipkin.base-url=http://139.224.123.199:9411
# 3、Add the server-side address where the metrics data is reported
com.alipay.sofa.lookout.agent-host-address=139.224.123.35

For balance-mng project, you need to add configurations to the application.properties file in balance-mng-bootstrap module.

add configurations

For stock-mng project, you need to add configurations to the application.properties file in stock-mng module.

add configurations for stock-mng

4. Modify unique id

Since everyone shares a set of service discoveries, to differentiate the services published by different users, it is required to add a unique id to the service.

KubeCon workshop will prepare a SOFAStack account for each user in the format ofuser0@sofastack.io to user99@sofastack.io. The first half of the account, namely user0 to user99 without @sofastack.io, can be used as a unique id.

For balance-mng project, you need to modify the unique id in the application.properties file of balance-mng-bootstrap module.

(uniqueid of balance-mng

For stock-mng project, you need to modify the unique id in the application.properties file of stock-mng module.

uniqueid of stock-mng

5. Publish SOFARPC service

Add the @SofaService and @Service annotations on BalanceMngImpl class to publish it as a SOFARPC service:

@Service
@SofaService(interfaceType = BalanceMngFacade.class, uniqueId = "${service.unique.id}", bindings = { @SofaServiceBinding(bindingType = "bolt") })

The BalanceMngImpl class with annotations added is shown as the following screenshot:

BalanceMngImpl

Add the @SofaService and @Service annotations on StockMngImpl class to publish it as a SOFARPC service:

@Service
@SofaService(interfaceType = StockMngFacade.class, uniqueId = "${service.unique.id}", bindings = { @SofaServiceBinding(bindingType = "bolt") })

The StockMngImpl class with annotations added is shown as the following screenshot:

StockMngImpl

6. Reference SOFARPC service

Add @SofaReference annotation on the stockMngFacade variable in BookStoreControllerImpl class to reference SOFARPC service:

@SofaReference(interfaceType = StockMngFacade.class, uniqueId = "${service.unique.id}", binding = @SofaReferenceBinding(bindingType = "bolt"))

Add @SofaReference annotation on the balanceMngFacade variable in BookStoreControllerImpl class to reference SOFARPC service:

@SofaReference(interfaceType = BalanceMngFacade.class, uniqueId = "${service.unique.id}", binding = @SofaReferenceBinding(bindingType = "bolt"))

The BookStoreControllerImpl class with annotations added is as shown in the following screenshot:

BookStoreControllerImpl

7. Verification

Run BalanceMngApplication and BalanceMngApplication to start the application. After the application is started successfully, you can access http://localhost:8080 in the browser.

Book list

To view the reported link data and link relation diagram, you can visit http://zipkin-dev.sofastack.tech:9411 in the browser.

Zipkin

To view the reported merics, you can visit http://zipkin-dev.sofastack.tech:9411 in the browser:

zipkin

  • jvm.threads.totalStarted{app=“stock_mng”}:Number of JVM startup threads
  • jvm.memory.heap.used{app=“stock_mng”}:JVM usage memory
  • jvm.gc.old.count{app=“stock_mng”}:JVM old-generation GC times
  • rpc.consumer.service.stats.total_count.count{app=“stock_mng”}:Number of calls of BalanceMngFacade interface
  • rpc.consumer.service.stats.total_time.elapPerExec{app=“stock_mng”}: Average calling duration of BalanceMngFacade
  • rpc.consumer.service.stats.total_time.max{app=“stock_mng”}:Maximum response time of BalanceMngFacade

For more usage about SOFALookout, see https://www.sofastack.tech/sofa-lookout/docs/Home