一、概述
soft-bolt提供了同步、Future、CallBack调用,大概调用流程如下图:
二、同步调用
同步调用示例代码:
public class MyClient {
private static RpcClient client;
public static void start() {
// 创建 RpcClient 实例
client = new RpcClient();
// 初始化 netty 客户端:此时还没有真正的与 netty 服务端进行连接
client.init();
}
public static void main(String[] args) throws RemotingException, InterruptedException {
MyClient.start();
// 构造请求体
RaftRequest request = new RaftRequest();
request.setContent("hello, bolt-server");
// 同步调用
RaftResponse response = (RaftResponse) client.invokeSync("127.0.0.1:8888", request, 30 * 1000);
System.out.println("result = " + response);
}
}
在分析soft-bolt的调用过程之前,首先有了解一下InvokeFuture的结构:
public class DefaultInvokeFuture implements InvokeFuture {
// 请求唯一id
private int invokeId;
// listener
private InvokeCallbackListener callbackListener;
// 提供给callback机制
private InvokeCallback callback;
// 响应信息
private volatile ResponseCommand responseCommand;
// 设置响应信息的时候,调用countDown()方法
private final CountDownLatch countDownLatch = new CountDownLatch(1);
// 是否调用过callback
private final AtomicBoolean executeCallbackOnlyOnce = new AtomicBoolean(false);
// 超时任务
private Timeout timeout;
// 异常
private Throwable cause;
// 协议类型
private byte protocol;
// 调用上下文
private InvokeContext invokeContext;
}
从Client端发起同步调用,调用实现如下:
public Object invokeSync(Url url, Object request, InvokeContext invokeContext, int timeoutMillis)throws RemotingException{
// 获取Connection(底层是使用netty框架)
final Connection conn = getConnectionAndInitInvokeContext(url, invokeContext);
this.connectionManager.check(conn);
// 发起远程调用
return this.invokeSync(conn, request, invokeContext, timeoutMillis);
}
client先获取到与server端的连接,获取的连接以后调用invokeSync()发起远程调用,最终调用的BaseRemoting.invokeSync实现如下:
protected RemotingCommand invokeSync(final Connection conn, final RemotingCommand request,
final int timeoutMillis) throws RemotingException,
InterruptedException {
// 将请求相关信息封装为InvokeFuture
final InvokeFuture future = createInvokeFuture(request, request.getInvokeContext());
// 将InvokeFuture添加到请求容器
conn.addInvokeFuture(future);
try {
conn.getChannel().writeAndFlush(request).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture f) throws Exception {
// 如果请求失败,填充InvokeFuture失败响应
if (!f.isSuccess()) {
conn.removeInvokeFuture(request.getId());
future.putResponse(commandFactory.createSendFailedResponse(
conn.getRemoteAddress(), f.cause()));
logger.error("Invoke send failed, id={}", request.getId(), f.cause());
}
}
});
} catch (Exception e) {
// 发生异常,填充InvokeFuture异常响应
conn.removeInvokeFuture(request.getId());
if (future != null) {
future.putResponse(commandFactory.createSendFailedResponse(conn.getRemoteAddress(), e));
}
logger.error("Exception caught when sending invocation, id={}", request.getId(), e);
}
RemotingCommand response = future.waitResponse(timeoutMillis);
// 如果超时以后,响应为空,设置超时响应
if (response == null) {
conn.removeInvokeFuture(request.getId());
response = this.commandFactory.createTimeoutResponse(conn.getRemoteAddress());
logger.warn("Wait response, request id={} timeout!", request.getId());
}
return response;
}
如果请求正常发送到服务端,并且服务端正常处理并响应,那么在客户端收到响应以后,就会将响应信息设置到对应的InvokeFuture中,具体实现如下:
public class RpcResponseRpcResponseProcessorProcessor extends AbstractRemotingProcessor {
public void doProcess(RemotingContext ctx, RemotingCommand cmd) {
// 获取Connection
Connection conn = ctx.getChannelContext().channel().attr(Connection.CONNECTION).get();
// 从容器中,获取到InvokeFuture
InvokeFuture future = conn.removeInvokeFuture(cmd.getId());
ClassLoader oldClassLoader = null;
try {
if (future != null) {
if (future.getAppClassLoader() != null) {
oldClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(future.getAppClassLoader());
}
// 设置响应信息
future.putResponse(cmd);
// 取消超时定时任务
future.cancelTimeout();
try {
// 执行callback逻辑,下面callback调用会进行分析
future.executeInvokeCallback();
} catch (Exception e) {
logger.error("Exception caught when executing invoke callback, id={}", cmd.getId(), e);
}
} else {
logger.warn("Cannot find InvokeFuture, maybe already timeout, id={}, from={} ",
cmd.getId(), RemotingUtil.parseRemoteAddress(ctx.getChannelContext().channel()));
}
} finally {
if (null != oldClassLoader) {
Thread.currentThread().setContextClassLoader(oldClassLoader);
}
}
}
}
soft-bolt中,通过RpcResponseProcessor类来处理响应,主要逻辑是从请求容器中获取InvokeFuture,并调用putResponse方法设置响应信息,取消超时定时任务。如果设置了callback,调用callback逻辑。
三、Future调用
Future调动示例如下:
public class MyClient {
private static RpcClient client;
public static void start() {
// 创建 RpcClient 实例
client = new RpcClient();
client.init();
}
public static void main(String[] args) throws RemotingException, InterruptedException {
MyClient.start();
// 构造请求体
RaftRequest request = new RaftRequest();
request.setContent("hello, bolt-server");
// 发起future调用
RpcResponseFuture future = client.invokeWithFuture("127.0.0.1:8888", request, 30 * 1000);
System.out.println("result = " + future.get());
}
}
Future调用逻辑和同步调用逻辑类似,只不过Future调用是将InvokeFuture对象返回,并将InvokeFuture封装为RpcResponseFuture返回给用户,RpcResponseFuture结构如下:
public class RpcResponseFuture {
private InvokeFuture future;
public boolean isDone() {
return this.future.isDone();
}
// 获取响应,设置超时时间
public Object get(int timeoutMillis) throws InvokeTimeoutException, RemotingException,
InterruptedException {
this.future.waitResponse(timeoutMillis);
if (!isDone()) {
throw new InvokeTimeoutException("Future get result timeout!");
}
ResponseCommand responseCommand = (ResponseCommand) this.future.waitResponse();
responseCommand.setInvokeContext(this.future.getInvokeContext());
return RpcResponseResolver.resolveResponseObject(responseCommand, addr);
}
// 获取响应
public Object get() throws RemotingException, InterruptedException {
ResponseCommand responseCommand = (ResponseCommand) this.future.waitResponse();
responseCommand.setInvokeContext(this.future.getInvokeContext());
return RpcResponseResolver.resolveResponseObject(responseCommand, addr);
}
}
四、Callback调用
callback调用示例如下:
public class MyClient {
private static RpcClient client;
public static void start() {
// 创建 RpcClient 实例
client = new RpcClient();
client.init();
}
public static void main(String[] args) throws RemotingException, InterruptedException {
MyClient.start();
// 构造请求体
RaftRequest request = new RaftRequest();
request.setContent("hello, bolt-server");
// callback调用
client.invokeWithCallback("127.0.0.1:8888", request, new InvokeCallback() {
@Override
public void onResponse(Object result) {
System.out.println("result = " + result);
}
// 处理异常
@Override
public void onException(Throwable e) {
System.out.println(e);
}
// 获取executor,可以为callback设置单独的线程池
@Override
public Executor getExecutor() {
return null;
}
}, 30 * 1000);
Thread.sleep(1000 * 60);
}
}
关于callback的调用逻辑逻辑上与同步操作基本一致,在服务端处理请求以后,客户端的RpcResponseProcessor对响应信息进行处理,并调用InvokeFuture.executeInvokeCallback,具体实现如下:
public void executeInvokeCallback() {
if (callbackListener != null) {
// 通过cas机制,保证callback逻辑只执行一次
if (this.executeCallbackOnlyOnce.compareAndSet(false, true)) {
// 调用 callback逻辑
callbackListener.onResponse(this);
}
}
}
上面的代码中通过cas机制,保证了callback只执行一次,callback调用逻辑具体如下:
public void onResponse(InvokeFuture future) {
InvokeCallback callback = future.getInvokeCallback();
if (callback != null) {
// 封装成任务
CallbackTask task = new CallbackTask(this.getRemoteAddress(), future);
// 获取callback的线程池,如果不为空,在线程池中去执行callback逻辑
if (callback.getExecutor() != null) {
try {
callback.getExecutor().execute(task);
} catch (RejectedExecutionException e) {
logger.warn("Callback thread pool busy.");
}
} else {
// 在netty的work线程中执行callback逻辑
task.run();
}
}
}