Netty-gRPC介绍和使用

本文介绍了如何使用gRPC,一个基于Protobuf的RPC框架,并提供了与Netty的整合示例。通过官方Demo展示了gRPC的基本用法,详细阐述了如何在Gradle项目中配置gRPC插件以自动生成代码。文章还涵盖了服务器端和客户端的实现,并讨论了不同传输层实现,包括基于Netty、OkHttp和InProcess。
摘要由CSDN通过智能技术生成

转自:http://www.saily.top/2017/07/23/netty5/

gRPC

Define your service using Protocol Buffers, a powerful binary serialization toolset and language

gRPC是基于Protobuf开发的RPC框架,简化了protobuf的开发,提供了服务端和客户端网络交互这一块的代码。

Demo

照着https://grpc.io/docs/quickstart/java.html测试一下官方的Demo。

记得要把Update a gRPC service部分做了。

gRPC整合Gradle与代码生成

https://github.com/grpc/grpc-java
这个是gRPC-java项目,先引入gRPC的依赖。

1
2
3
compile 'io.grpc:grpc-netty:1.4.0'
compile 'io.grpc:grpc-protobuf:1.4.0'
compile 'io.grpc:grpc-stub:1.4.0'

然后配置gradle的grpc插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
apply plugin: 'java'
apply plugin: 'com.google.protobuf'

buildscript {
repositories {
mavenCentral()
}
dependencies {
// ASSUMES GRADLE 2.12 OR HIGHER. Use plugin version 0.7.5 with earlier
// gradle versions
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.1'
}
}

protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.2.0"
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.4.0'
}
}
generateProtoTasks {
all()*.plugins {
grpc {}
}
}
}

后面直接用gradle的任务就可以生成代码了。

gRPC提供了3种传输层的实现

  • gRPC comes with three Transport implementations:

    1. The Netty-based transport is the main transport implementation based on Netty. It is for both the client and the server.
    2. The OkHttp-based transport is a lightweight transport based on OkHttp. It is mainly for use on Android and is for client only.
    3. The inProcess transport is for when a server is in the same process as the client. It is useful for testing.

https://github.com/google/protobuf-gradle-plugin

The Gradle plugin that compiles Protocol Buffer (aka. Protobuf) definition files (*.proto) in your project. There are two pieces of its job:

  1. It assembles the Protobuf Compiler (protoc) command line and use it to generate Java source files out of your proto files.
  2. It adds the generated Java source files to the input of the corresponding Java compilation unit (sourceSet in a Java project; variant in an Android project), so that they can be compiled along with your Java sources.

实战

配置好后,进行一个演示

src/main/proto新建一个文件Student.proto

gradle插件默认从src/main/proto找proto源文件进行代码生成,这里有提到,而且这个路径的配置是可以修改的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
syntax = "proto3";

package com.sail.proto;

option java_package = "com.sail.proto";
option java_outer_classname = "StudentProto";
option java_multiple_files = true;

service StudentService {
rpc GetRealNameByUsername(MyRequest) returns (MyResponse) {}

rpc GetStudentsByAge(StudentRequest) returns (stream StudentResponse) {}

rpc GetStudentsWrapperByAges(stream StudentRequest) returns (StudentResponseList) {}

rpc BiTalk(stream StreamRequest) returns (stream StreamResponse) {}
}

message MyRequest {
string username = 1;
}

message MyResponse {
string realname = 2;
}

message StudentRequest {
int32 age = 1;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值