什么是 RPC
這個(gè)是知乎上的一個(gè)關(guān)于 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


按照APK 方式隨意
目錄:examples/android/helloworld
./gradlew assembleDebug
https://developers.google.com/protocol-buffers/docs/proto3
基于protobuf 3.x,基于Netty 4.x +
Netty是一個(gè)高性能、異步事件驅(qū)動(dòng)的NIO框架,它提供了對(duì)TCP、UDP和文件傳輸?shù)闹С郑鳛橐粋€(gè)異步NIO框架,Netty的所有IO操作都是異步非阻塞的,通過Future-Listener機(jī)制,用戶可以方便的主動(dòng)獲取或者通過通知機(jī)制獲得IO操作結(jié)果。
Java NIO(New IO)是一個(gè)可以替代標(biāo)準(zhǔn) Java IO API 的 IO API(從 Java 1.4 開始),Java NIO 提供了與標(biāo)準(zhǔn) 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
-----------------------------------------------------------劃重點(diǎn)---------------------------------------------------------------
1.生成代碼
下載release包
https://github.com/google/protobuf/releases/download/v3.1.0/protoc-3.1.0-osx-x86_64.zip
可能需要提前創(chuàng)建好文件夾,如果你不打算寫shell腳本的話.
解壓上面下載的文件進(jìn)入bin執(zhí)行腳本
./protoc --proto_path=src --javanano_out=build/gen src/helloworld.proto
helloword.proto 文件內(nèi)容
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;
}

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