Get started with SOFABoot

Update time: 2019-06-21

This document introduces how to use SOFARPC for service publishing and reference in SOFABoot.

You can get the code sample of this document by clicking here. Note that the code sample requires a local installation of the zookeeper environment. If not, you need to remove the configuration in to use the local file as a registry center.

Create a project

  1. Prepare environment: SOFABoot requires JDK7 or JDK8 and needs to be compiled with Apache Maven 2.2.5 or above.
  2. Build SOFABoot project: SOFABoot is based on Spring Boot. So you can use Spring Boot’s project generation tool to generate a standard Spring Boot project.
  3. Add SOFABoot dependency: The generated standard Spring Boot project directly uses Spring parent dependency, which should be changed to the parent dependency provided by SOFABoot. The parent dependency provides and manages a variety of starters provided by SOFABoot.

Replace the above with the followings:

  1. Configure is the configuration file in SOFABoot project. Here you need to configure the application name. 
  2. Introduce RPC starter:

  1. Declare the xsd file of SOFABoot:

In the XML configuration file to be used, configure the declaration of the header xsd file to the followings. This enables development using the XML elements defined by SOFABoot.

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="" 
       xsi:schemaLocation =" 
   http://sofastack .io/schema/sofaboot.xsd"

Define service interface and implementation

public interface HelloSyncService { 

    String saySync(String string); 
public class HelloSyncServiceImpl implements HelloSyncService { 

    public String saySync(String string) { 
        return string; 

Publish service on server

Configure the followings in the xml file. When the Spring context is refreshed, SOFABoot registers the service implementation on the server, communicates with the client by bolt protocol, and publishes metadata such as address to registry center (local file is used as registry center by default).

<bean id="helloSyncServiceImpl" class=""/> 
<sofa:service ref="helloSyncServiceImpl" interface=" samples.invoke.HelloSyncService"

Reference service by client

Configure the followings in the xml file. When the Spring context is refreshed, SOFABoot generates an RPC proxy bean, personReferenceBolt. This allows you to use the bean directly in the code for remote call.

<sofa:reference id="helloSyncServiceReference" interface=""> 

Run the project

The startup class of SpringBoot is coded as follows. The above xml file is loaded here using ImportResource.

@ImportResource({ "classpath*:rpc-sofa-boot-starter-samples.xml" }) 
public class SofaBootRpcSamplesApplication {
    public static void main(String[] args) {
          SpringApplication springApplication = new SpringApplication(SofaBootRpcSamplesApplication.class); 
          ApplicationContext applicationContext =;           

          HelloSyncService helloSyncServiceReference = (HelloSyncService) applicationContext 

          System.out.println(helloSyncServiceReference.saySync("sync") ); 

The output result is as follows:


By this step, you’ve completed service publishing and reference.