美文网首页
gRPC & Android

gRPC & Android

作者: 简简单单敲代码 | 来源:发表于2017-04-28 10:58 被阅读749次

    什么是 RPC

    这个是知乎上的一个关于 rpc 的问题
    谁能用通俗的语言解释一下什么是 RPC 框架?

    gRPC的官方Demo
    http://www.grpc.io/docs/quickstart/android.html#update-a-grpc-service
    git clone -b v1.0.0 https://github.com/grpc/grpc-javacd grpc-java/examples./gradlew installDist./build/install/examples/bin/hello-world-server./build/install/examples/bin/hello-world-client

    Clipboard Image.pngClipboard Image.png
    Clipboard Image.pngClipboard Image.png
    按照APK 方式随意
    目录:examples/android/helloworld
    ./gradlew assembleDebug
    https://developers.google.com/protocol-buffers/docs/proto3
    基于protobuf 3.x,基于Netty 4.x +
    Netty是一个高性能、异步事件驱动的NIO框架,它提供了对TCP、UDP和文件传输的支持,作为一个异步NIO框架,Netty的所有IO操作都是异步非阻塞的,通过Future-Listener机制,用户可以方便的主动获取或者通过通知机制获得IO操作结果。
    Java NIO(New IO)是一个可以替代标准 Java IO API 的 IO API(从 Java 1.4 开始),Java NIO 提供了与标准 IO 不同的 IO 工作方式。
    http://www.ibm.com/developerworks/cn/education/java/j-nio/j-nio.html
    http://ifeve.com/netty-in-action-7/
    Android dependencies
    https://github.com/grpc/grpc-java
    compile 'io.grpc:grpc-okhttp:1.0.3'compile 'io.grpc:grpc-protobuf-lite:1.0.3'compile 'io.grpc:grpc-stub:1.0.3'
    http://square.github.io/retrofit/
    https://github.com/google/protobuf/tree/master/javanano
    -----------------------------------------------------------划重点---------------------------------------------------------------
    1.生成代码
    下载release包
    https://github.com/google/protobuf/releases/download/v3.1.0/protoc-3.1.0-osx-x86_64.zip
    可能需要提前创建好文件夹,如果你不打算写shell脚本的话.
    解压上面下载的文件进入bin执行脚本
    ./protoc --proto_path=src --javanano_out=build/gen src/helloworld.proto
    helloword.proto 文件内容
    syntax = "proto3";
    option java_multiple_files = true;

    option java_package = "io.grpc.examples.helloworld";

    option ignore_services = true

    package helloworld;

    message ProductGpid {

        ProductSource source = 1;
    

    }

    message ProductGpidResp {

        int64 gpid = 1;
    

    }

    enum ProductSource {

        ProductSourceInvalid = 0;
    
        ProductSourceEzbuy = 10;
    
        ProductSourceTaobao = 20;
    

    }

    Clipboard Image.pngClipboard Image.png

    2.增加依赖
    compile 'com.squareup.retrofit2:retrofit:2.1.0'compile 'com.squareup.retrofit2:converter-protobuf:2.1.0'compile 'com.google.protobuf.nano:protobuf-javanano:3.1.0'
    ArrayList<Protocol> protocolArrayList = new ArrayList<>();protocolArrayList.add(Protocol.HTTP_1_1);protocolArrayList.add(Protocol.HTTP_2);OkHttpClient okHttpClient = new OkHttpClient.Builder().protocols(protocolArrayList).build();Retrofit retrofit = new Retrofit.Builder().client(okHttpClient).addConverterFactory(ProtoConverterFactory.create()).baseUrl("http://192.168.199.64:14151").build();SimpleService service = retrofit.create(SimpleService.class);ProductGpid productGpid = new ProductGpid();productGpid.source = Product.ProductSourceEzbuy;Call<ProductGpidResp> call = service.Gpid(productGpid);call.enqueue(new Callback<ProductGpidResp>() {@Override public void onResponse(Call<ProductGpidResp> call, Response<ProductGpidResp> response) {Log.e("asd", response.body().toString()); }@Override public void onFailure(Call<ProductGpidResp> call, Throwable t) {Log.e("asd", "onFailure"); }});
    public interface SimpleService {@POST("/")Call<ProductGpidResp> Gpid(@Body ProductGpid impl);}
    https://github.com/google/protobuf-gradle-plugin/blob/master/testProjectAndroid/build.gradle#L92

    相关文章

      网友评论

          本文标题: gRPC & Android

          本文链接:https://www.haomeiwen.com/subject/ykmyattx.html