Get started with SOFABoot

Edit
Update time: 2023-02-26

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 com.alipay.sofa.rpc.registry.address configuration in application.properties 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.
<parent> 
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>${spring.boot.version}</version> 
    <relativePath/> 
</parent> 

Replace the above with the followings:

<parent> 
    <groupId>com.alipay.sofa</groupId> 
    <artifactId>sofaboot-dependencies</artifactId> 
    <version>${sofa-boot.version}</version> 
</parent> 

Here, ‘${sofa-boot.version}’ specifies the specific SOFABoot version. Refer to Release History.

  1. Configure application.properties: application.properties is the configuration file in SOFABoot project. Here you need to configure the application name.
spring.application.name=AppName 
  1. Introduce RPC starter:
<dependency>
     <groupId>com.alipay.sofa</groupId>
     <artifactId>rpc-sofa-boot-starter</artifactId>
</dependency> 

Define service interface and implementation

public interface AnnotationService {

    String sayAnnotation(String string);

}

Publish service on server

Publish the service through ‘@SofaService’ annotation, as shown in the following code: 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).

import com.alipay.sofa.runtime.api.annotation.SofaService;
import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding;
import org.springframework.stereotype.Service;

@SofaService(interfaceType = AnnotationService.class, bindings = { @SofaServiceBinding(bindingType = "bolt") })
@Service
public class AnnotationServiceImpl implements AnnotationService {
    @Override
    public String sayAnnotation(String string) {
        return string;
    }
}

Reference service by client

The reference service is annotated with ‘@SofaReference’, as shown in the following code: SOFABoot will generate a RPC proxy bean for ‘AnnotationService’, it also specifies the bolt protocol to communicate with the server. This allows you to use the bean directly in the code for remote call.

import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding;
import org.springframework.stereotype.Service;

@Service
public class AnnotationClientImpl {

    @SofaReference(interfaceType = AnnotationService.class, jvmFirst = false, 
            binding = @SofaReferenceBinding(bindingType = "bolt"))
    private AnnotationService annotationService;

    public String sayClientAnnotation(String str) {
        return annotationService.sayAnnotation(str);
    }
}

Run the project

The startup class of SpringBoot is coded as follows: Start the server

@SpringBootApplication
public class AnnotationServerApplication {

    public static void main(String[] args) {

        SpringApplication springApplication = new SpringApplication(
            AnnotationServerApplication.class);
        ApplicationContext applicationContext = springApplication.run(args);
    }
}

Start the client, get the implementation bean of AnnotationClientImpl, call sayClientAnnotation, and Indirect use of proxy class generated by @SofaReference call the remote service AnnotationServiceImpl.

public class AnotationClientApplication {

    public static void main(String[] args) {
        //change port to run in local machine
        System.setProperty("server.port", "8081");
        SpringApplication springApplication = new SpringApplication(
            AnotationClientApplication.class);
        ApplicationContext applicationContext = springApplication.run(args);

        AnnotationClientImpl annotationService = applicationContext
            .getBean(AnnotationClientImpl.class);
        String result = annotationService.sayClientAnnotation("annotation");
        System.out.println("invoke result:" + result);
    }
}

The output result is as follows:

invoke result:annotation

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