jprotobuf-rpc-socket

repository·master·Indexed 19 days ago

https://github.com/baidu/jprotobuf-rpc-socket

A high-performance, binary RPC communication protocol implementation based on TCP and Protobuf. It enables a pure POJO-based development workflow using @Protobuf annotations instead of manual Protobuf IDL files. The library includes support for Spring, Spring Boot, and Redis-based service discovery, as well as a pre-compile plugin to improve application startup speed.

Tokens
1.7K
Snippets
5
Records
6
Agent score
18%

What's inside jprotobuf-rpc-socket

  1. Define RPC Data Objects using @Protobuf

    master

    Instead of writing Google Protobuf IDL files, you can define your data structures directly in Java using the @Protobuf annotation on fields. This allows for a pure POJO-based development workflow.

    public class EchoInfo {
        
        @Protobuf
        public String message;
    }
  2. Implement and publish an RPC Server

    master

    To create an RPC server:

    1. Implement the service logic in a class.
    2. Annotate the implementation method with @ProtobufRPCService. You must specify the serviceName and methodName to match the client's interface.
    3. Use RpcServer to register the implementation and start the server on a specific port.
    // 1. Implement the service
    public class EchoServiceImpl {
    
        @ProtobufRPCService(serviceName = "echoService", methodName = "echo")
        public EchoInfo doEcho(EchoInfo info) {
            EchoInfo ret = new EchoInfo();
            ret.setMessage("hello:" + info.message);
            return ret;
        }
    }
    
    // 2. Publish the service
    RpcServer rpcServer = new RpcServer();
    EchoServiceImpl echoServiceImpl = new EchoServiceImpl();
    rpcServer.registerService(echoServiceImpl);
    rpcServer.start(1031);
  3. Implement an RPC Client

    master

    To create an RPC client:

    1. Define an interface for the service.
    2. Annotate the interface methods with @ProtobufRPC. You must specify serviceName. The methodName defaults to the Java method name if not provided. You can also set onceTalkTimeout.
    3. Use RpcClient and ProtobufRpcProxy to create a dynamic proxy instance of your interface.
    // 1. Define the interface
    public interface EchoService {
        @ProtobufRPC(serviceName = "echoService", onceTalkTimeout = 200)
        EchoInfo echo(EchoInfo info);
    }
    
    // 2. Create the client and proxy
    RpcClient rpcClient = new RpcClient();
    ProtobufRpcProxy<EchoService> pbrpcProxy = new ProtobufRpcProxy<EchoService>(rpcClient, EchoService.class);
    pbrpcProxy.setPort(1031);
    
    // 3. Use the proxy
    EchoService echoService = pbrpcProxy.proxy();
    EchoInfo request = new EchoInfo();
    request.message = "hello";
    EchoInfo response = echoService.echo(request);
    
    // 4. Stop the client when done
    rpcClient.stop();
  4. Use the Jprotobuf pre-compile plugin to improve startup speed

    master

    The jprotobuf-precompile-plugin allows you to pre-compile Protobuf metadata, which speeds up application startup. Configure the filterClassPackage to specify the package to scan for pre-compilation.

    Run the plugin using mvn jprotobuf:precompile or simply mvn package.

    <plugin>
        <groupId>com.baidu</groupId>
        <artifactId>jprotobuf-precompile-plugin</artifactId>
        <version>1.2.8</version>
        <configuration>
            <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
            <filterClassPackage>com.baidu</filterClassPackage>
        </configuration>
        <executions>
            <execution>
                <phase>compile</phase>
                <goals>
                    <goal>precompile</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
  5. Install Jprotobuf-rpc-socket dependencies

    master

    To use Jprotobuf-rpc-socket, add the core dependency to your Maven pom.xml. You can also include extensions for Spring, Spring Boot, or Redis-based service discovery.

    <!-- Core dependency -->
    <dependency> 
    	<groupId>com.baidu</groupId>
    	<artifactId>jprotobuf-rpc-core</artifactId>
    	<version>4.2.1</version>
    </dependency>
    
    <!-- Spring extension -->
    <dependency>
    	<groupId>com.baidu</groupId>
    	<artifactId>jprotobuf-rpc-core-spring</artifactId>
    	<version>4.2.1</version>
    </dependency>
    
    <!-- Spring Boot extension -->
    <dependency>
    	<groupId>com.baidu</groupId>
    	<artifactId>jprotobuf-rpc-spring-starter</artifactId>
    	<version>4.2.1</version>
    </dependency>
    
    <!-- Redis-based service registration and discovery -->
    <dependency>
    	<groupId>com.baidu</groupId>
    	<artifactId>jprotobuf-rpc-registry-redis</artifactId>
    	<version>4.2.1</version>
    </dependency>
  6. Reference: @ProtobufRPC and @ProtobufRPCService annotations

    master

    These annotations are used to define RPC service boundaries.

    @ProtobufRPC (Client Side) Used on interface methods to define how a client calls a remote service.

    • serviceName (String): The name of the service on the server.
    • methodName (String, optional): The name of the method on the server. Defaults to the Java method name.
    • onceTalkTimeout (long, optional): Timeout for the request in milliseconds.

    @ProtobufRPCService (Server Side) Used on implementation methods to identify them as RPC endpoints.

    • serviceName (String): The name of the service.
    • methodName (String): The name of the method to be invoked.