NettyRpc Documentation

repository·master·Indexed 25 days ago

https://github.com/luxiaoxun/nettyrpc

An RPC framework built on Netty, ZooKeeper, and Spring. It features service discovery, high availability, multiple load balancing strategies, and service versioning. Supports synchronous and asynchronous calls via RpcClient, as well as dependency injection using the @RpcAutowired annotation.

Tokens
969
Snippets
3
Records
4
Agent score
32%

What's inside NettyRpc

  1. Define and implement a NettyRpc service

    master

    To create a service in NettyRpc, first define a standard Java interface. Then, implement that interface using the @NettyRpcService annotation. This annotation allows you to specify the interface class and a service version for versioning support.

    // 1. Define the interface
    public interface HelloService {
        String hello(String name);
        String hello(Person person);
    }
    
    // 2. Implement with @NettyRpcService
    @NettyRpcService(HelloService.class, version = "1.0")
    public class HelloServiceImpl implements HelloService {
        public HelloServiceImpl(){}
    
        @Override
        public String hello(String name) {
            return "Hello " + name;
        }
    
        @Override
        public String hello(Person person) {
            return "Hello " + person.getFirstName() + " " + person.getLastName();
        }
    }
  2. Start a NettyRpc server

    master

    Before calling services, ensure ZooKeeper is running (e.g., at 127.0.0.1:2181). You can start the NettyRpc server in two ways depending on your environment:

    1. With Spring configuration: Use RpcServerBootstrap.
    2. Without Spring configuration: Use RpcServerBootstrap2.
  3. Inject services using @RpcAutowired

    master

    In environments where dependency injection is supported, you can use the @RpcAutowired annotation to inject remote services directly into your fields. You can specify a version to target a specific version of the service implementation.

    public class Baz implements Foo {
        @RpcAutowired(version = "1.0")
        private HelloService helloService1;
           
        @RpcAutowired(version = "2.0")
        private HelloService helloService2;
           
        @Override
        public String say(String s) {
            return helloService1.hello(s);
        }
    }
  4. Call services using RpcClient

    master

    You can interact with services manually by instantiating an RpcClient with your ZooKeeper address. The client supports both synchronous and asynchronous calling patterns.

    • Synchronous Call: Use createService(Class<T> serviceClass, String version) to get a proxy that behaves like a local object.
    • Asynchronous Call: Use createAsyncService(Class<T> serviceClass, String version) to get an RpcService instance. Use the .call(String methodName, Object... args) method, which returns an RPCFuture.
    final RpcClient rpcClient = new RpcClient("127.0.0.1:2181");
    
    // Sync call
    HelloService helloService = rpcClient.createService(HelloService.class, "1.0");
    String result = helloService.hello("World");
    
    // Async call
    RpcService client = rpcClient.createAsyncService(HelloService.class, "2.0");
    RPCFuture helloFuture = client.call("hello", "World");
    String result = (String) helloFuture.get(3000, TimeUnit.MILLISECONDS);