gRPC简单封装调用

目的:减少本地调用的代码编写
调用方式就这样。
在调用 api之前,我们需要和 jsonrpc一样在 app 里面初始化一些东西。

TGrpc grpc = new TGrpc.builder() .setGrpcUrl("xx.xx.xx") .setHandler(App.handler) .setPort(21080) .setCustomerCookie(getCustomerCookie()) .build();

这样为了方便我们做拓展。
没有达到我想象中那么好,因为我没有去改生成代码的插件。
但是我去改了生成代码要用到的类,然后把东西封装了下。
改的类:
io.grpc.stub.AbstractStub

我们生产的 grpc 里面用的类都是继承AbstractStub所以我这样封装了下
public AbstractStub(Channel channel, CallOptions callOptions) { if (channel == null) { this.channel = OkHttpChannelBuilder.forAddress(TGrpc.getInstance().getGrpcUrl(), TGrpc.getInstance().getPort()) .usePlaintext(true) .build(); } else { this.channel = channel; } this.callOptions = checkNotNull(callOptions, "callOptions"); setMatadata(this); }

最终每次调用其实我都有这个
调用的例子:
GrpcRequest.executeProtocol(new GrpcRequest.RequestListener() { @Override public Common.ResultResp request() { return FavouriteGrpc.newBlockingStub(null).userAddFavouriteTag(AddFavouriteTagReq.newBuilder().build()); }@Override public void onResponse(Common.ResultResp resp) { } });

【gRPC简单封装调用】注意:
FavouriteGrpc.newBlockingStub(null)

这里要传 null就用默认的.如果你有需要别的的 address 可以自己传一个对象进去。
通常不需要,这里如果要改需要改生成代码的插件。
public interface RequestListener { T request(); void onResponse(T t); }

调用的时候传入的接口RequestListener封装了两个方法。
T request();

这个方法里面执行你需要请求网络的代码,在 io 线程。
并且返回你得到的值,这个值是你在onResponse得到的。
void onResponse(T t);

这个方法是当接口请求完后回调。无论成功失败,都会回调。(目前适用于我们的场景,如果有额外需求可以再考虑修改)
GrpcRequest
package com.daigou.grpc; import com.daigou.model.RequestLifecycle; /** * Created by PengChunxiao on 2017/6/15. */ public class GrpcRequest extends XThread {private final RequestListener tRequestListener; private boolean canceled = false; private T t; private GrpcRequest(RequestListener listener) { tRequestListener = listener; }public static void executeProtocol(RequestListener listener) { if (listener == null) return; GrpcRequest thread = new GrpcRequest(listener); thread.start(); }@Override public void run() { t = null; try { checkInterrupted(); t = tRequestListener.request(); checkInterrupted(); } catch (Exception e) { e.printStackTrace(); } finally { TGrpc.getInstance().getHandler().post(new Runnable() { @Override public void run() { tRequestListener.onResponse(t); } }); } }private void checkInterrupted() throws InterruptedException { if (isCancelled()) { throw new InterruptedException(); } }@Override public void interrupt() { cancel(); super.interrupt(); }public void cancelProtocol(final XThread thread) { if (thread == null) return; Thread.State state = thread.getState(); if (state != Thread.State.TERMINATED) { thread.interrupt(); } }public void bindTo(RequestLifecycle lifecycle) { if (lifecycle != null) { lifecycle.onBind(this); } }public void cancel() { canceled = true; }public boolean isCanceled() { return canceled; }public interface RequestListener { T request(); void onResponse(T t); } }

GrpcRequest这个思路是模仿 volley 中的 request。
但是他是继承 Thread,并且同样在 RequestLifeycle 中加入相关代码。可以减少内存溢出。
方法和之前的 jsonRpc的保持一致。bindTo
public void bindTo(RequestLifecycle lifecycle) { if (lifecycle != null) { lifecycle.onBind(this); } }

大致就这样

    推荐阅读