server端启动主要分两步:
- ioThreadNum:IO线程数,默认是CPU核数。
- workThreadNum:工作线程数,默认是CPU核数。
- protocolType:接收client的协议类型,是ProtocolType枚举值,默认为空,表示接收所有支持的类型。
- namingServiceUrl:注册中心地址,当不为空时,server会向该地址注册实例,默认为空。
- globalThreadPoolSharing:多个server实例是否共享线程池,默认否。
- readerIdleTime:与client连接保持的空闲时间,如果超过空闲时间没有读写时间,连接将被关闭,默认是60s。
所需参数为端口、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)));