Alipay Open Source SofaRPC

Overview

SOFARPC is a highly scalable, high-performance, production-grade Java RPC framework. SOFARPC has experienced more than ten years and five generations of development in Ant Financial. SOFARPC is committed to simplifying RPC calls between applications and providing applications with a convenient, transparent, stable and efficient point-to-point remote service call solution. For users and developers to easily expand functions, SOFARPC provides rich model abstractions and extensible interfaces, including filters, routing, load balancing, and more. At the same time, it provides rich microservice governance solutions around the SOFARPC framework and its surrounding components.

Features

  • Transparent, high-performance remote service invocation
  • Support multiple service routing and load balancing strategies
  • Supports integration of multiple registries
  • Supports multiple protocols
  • Support synchronization, one-way, callback, generalization and other invocation methods
  • Support cluster fault tolerance, service warm-up, automatic fault isolation
  • Powerful expansion function, each functional component can be expanded as needed

Associated project

need

Compilation requires JDK 7 and above and Maven 3.2.5 and above.

Running requires JDK 6 and above.

 

Quick start

This document will demonstrate how to use SOFARPC to publish and reference services. In this example, the local simulation server will start listening on a port and publish a service, and the client will refer to the service for direct connection calls.

You can find the sample code for this document directly under Projects .

Create project

JDK 6 and above and Maven 3 and above are required to be installed.

We create a new Maven project and introduce SOFARPC dependencies.

<dependency>
    <groupId>com.alipay.sofa</groupId>
    <artifactId>sofa-rpc-all</artifactId>
    <version>5.3.1</version>
<dependency>

Write the server implementation

Step 1: Create an interface

/**
 * Quick Start demo interface
 */
public interface HelloService {
    String sayHello(String string);
}

Step 2: Create the interface implementation

/**
 * Quick Start demo implement
 */
public class HelloServiceImpl implements HelloService {
    @Override
    public String sayHello(String string) {
        System.out.println("Server receive: " + string);
        return "hello " + string + " !";
    }
}

Step 3: Write server-side code

/**
 * Quick Start Server
 */
public class QuickStartServer {

    public static void main(String[] args) {
        ServerConfig serverConfig = new ServerConfig()
                .setProtocol("bolt") // Set a protocol, default 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 service
    }
}

Write the client implementation

Step 1: Get the server interface

Generally, the server will jarprovide the interface class to the client in the form of. In this example, it is skipped because the server and client are one more project.

Step 2: Program the Client Code

/**
 * Quick Start client
 */
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 connection address
        // generate proxy class
        HelloService helloService = consumerConfig.refer();
        while (true) {
            System.out.println(helloService.sayHello("world"));
            try {
                Thread.sleep(2000);
            } catch (Exception e) {
            }
        }
    }
}

run

Start the server and client respectively and observe the operation effect.

The server will print:

Server receive: world

The client will print:

hello world !

More

For more examples, please refer to: example

For SOFABoot example, please refer to: sofaboot-sample-with-rpc

 

 

From: https://github.com/alipay/sofa-rpc

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324981493&siteId=291194637