Skip to content

Latest commit

 

History

History
67 lines (54 loc) · 3.34 KB

File metadata and controls

67 lines (54 loc) · 3.34 KB

示例程序

定义接口类以及实现类

定义Main类

server端启动主要分两步:

配置RpcServerOptions:

  • ioThreadNum:IO线程数,默认是CPU核数。
  • workThreadNum:工作线程数,默认是CPU核数。
  • protocolType:接收client的协议类型,是ProtocolType枚举值,默认为空,表示接收所有支持的类型。
  • namingServiceUrl:注册中心地址,当不为空时,server会向该地址注册实例,默认为空。
  • globalThreadPoolSharing:多个server实例是否共享线程池,默认否。
  • readerIdleTime:与client连接保持的空闲时间,如果超过空闲时间没有读写时间,连接将被关闭,默认是60s。

初始化RpcServer实例:

所需参数为端口、RpcServerOptions(可选)、以及interceptor(可选)。

注册服务实例。

具体代码如下:

public class RpcServerTest {
    public static void main(String[] args) throws InterruptedException {
        int port = 8002;
        if (args.length == 1) {
            port = Integer.valueOf(args[0]);
        }
 
        RpcServerOptions options = new RpcServerOptions();
        options.setReceiveBufferSize(64 * 1024 * 1024);
        options.setSendBufferSize(64 * 1024 * 1024);
        final RpcServer rpcServer = new RpcServer(port, options);
        rpcServer.registerService(new EchoServiceImpl());
        rpcServer.start();
 
        // make server keep running
        synchronized (RpcServerTest.class) {
            try {
                RpcServerTest.class.wait();
            } catch (Throwable e) {
            }
        }
    }
}

限流

server通过CurrentLimitInterceptor实现限流,目前支持两种限流算法:

如果不能满足需求,业务也可以实现自己的限流算法,只需实现CurrentLimiter.java这个接口即可。

启用限流功能代码示例:

RpcServer server = new RpcServer(8000); 
server.getInterceptors().add(new CurrentLimitInterceptor(new TokenBucketCurrentLimiter(500, 500)));