preface

SOFARPC, which originated from ALIBABA’s internal HSF, is a Java RPC framework with high scalability, high performance and production level recently opened by Ant Financial. SOFA-RPC in Ant Financial has experienced more than ten years of development, committed to simplify the application between RPC calls. It provides a convenient, transparent, stable and efficient point-to-point remote service invocation scheme for applications.

SOFA-RPC provides rich model abstractions and extensible interfaces, including filters, routing and load balancing, for users and developers to easily extend functions. At the same time, the SOFA-RPC framework and its surrounding components to provide a wealth of micro-service governance solutions.

Other articles

  • Ant Financial RPC framework SOFA- INITIAL RPC experience

  • Ant Financial SOFA-Boot Integration SOFA-RPC

  • Ant Financial SOFA-Boot Integration SOFA-RPC

  • Ant Financial SOFA-Boot Integration SOFA-RPC

The body of the

1. Features

  • Transparent, high-performance remote service invocation
  • Supports multiple service routing and load balancing policies
  • Supports integration of multiple registries
  • Supports bolt, REST, duBBo and other communication protocols
  • Support synchronous, one-way, callback, generalization and other call methods
  • Supports cluster fault tolerance, service preheating, and automatic fault isolation
  • Powerful extension function, you can expand each functional component as required

2. Implementation principle

A. Service publishing

When a SOFARPC application is launched, if it finds that the current application needs to publish RPC services, SOFARPC registers those services with the service registry. The Service in the figure points to Registry.

B. Service subscription

When a SOFARPC application that references this service is started, it subscribes to the metadata information for the service from the service registry. Upon receiving the subscription request, the service registry pushes the publisher’s list of metadata to the service reference in real time. Registry points to Reference in the figure.

C. Service invocation

When the service reference gets the address, it can pick it up and make the call. In the figure, Reference points to a Service.

3. Start fast

3.1. Sofa – RPC dependency is introduced

<dependencies>
    <dependency>
        <groupId>com.alipay.sofa</groupId>
        <artifactId>sofa-rpc-all</artifactId>
        <version>5.3.1</version>
    </dependency>
</dependencies>
Copy the code

3.2. Write service interfaces and service implementation classes

HelloService.java

public interface HelloService {
    String sayHello(String string);
}
Copy the code

HelloServiceImpl.java

public class HelloServiceImpl implements HelloService {
    @Override
    public String sayHello(String string) {
        System.out.println("Server receive: " + string);
        return "hello " + string + "!"; }}Copy the code

3.3. Write a service provider startup class

QuickStartServer.java

public class QuickStartServer {
    public static void main(String[] args) {
        ServerConfig serverConfig = new ServerConfig()
                .setProtocol("bolt") // Set a protocol that defaults to bolt
                .setPort(9696) // Set a port, default 12200
                .setDaemon(false); // Non-daemon thread

        ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>()
                .setInterfaceId(HelloService.class.getName()) // Specify the interface
                .setRef(new HelloServiceImpl()) // Specify the implementation
                .setServer(serverConfig); // Specify the server

        providerConfig.export(); // Publish the service}}Copy the code

Run the server provider, and the log output is as follows:

Sofa-Middleware-Log SLF4J Warn : No log util is usable, Default app logger will be used.
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Sofa-Middleware-Log SLF4J Warn : No log util is usable, Default app logger will be used.
Copy the code

3.4. Write a service consumer startup class

QuickStartClient.java

public class QuickStartClient {
    public static void main(String[] args) {
        ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>()
                .setInterfaceId(HelloService.class.getName()) // Specify the interface
                .setProtocol("bolt") // Specify the protocol
                .setDirectUrl("Bolt: / / 127.0.0.1:9696"); // Specify the direct address

        HelloService helloService = consumerConfig.refer();

        while (true) {
            System.out.println(helloService.sayHello("world"));
            try {
                Thread.sleep(200);
            } catch(Exception e) { e.printStackTrace(); }}}}Copy the code

Run the server consumer and invoke the service provider:

  • The service provider logs are as follows:
Server receive: world
Server receive: world
Server receive: world
Server receive: world
Copy the code
  • Service consumer logs are as follows:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Sofa-Middleware-Log SLF4J Warn : No log util is usable, Default app logger will be used.
Sofa-Middleware-Log SLF4J Warn : No logUsable, Default app Logger will be used. Hello world! Hello world! Hello world! Hello world!Copy the code

summary

This is a quick start example!

It can be found that SOFA-RPC is not much different from Dubbo of Taobao and Motan of Weibo in terms of usage. Dubbo exists as a whole set of service governance, while SOFA-RPC is just a lightweight RPC framework, which is based on the transformation of HSF framework and provides more perfect, powerful and diversified RPC programming API.


Welcome to pay attention to the technical public number: Zero one Technology Stack

This account will continue to share learning materials and articles on back-end technologies, including virtual machine basics, multithreaded programming, high-performance frameworks, asynchronous, caching and messaging middleware, distributed and microservices, architecture learning and progression.