打卡日期(2019-07-18)
学习要点
- 利用grpc完成Server Streaming Rpc例子
步骤
1.配置grpc依赖包2.编写proto文件3.利用gradle generateProto生成java类4.编写Server/Client服务
注:应为服务端只负责启动,所以Server类复用上一章的 《Netty学习打卡–从小白到放弃》----- 15 - netty 之grpc Simple RPC例子
2.编写proto文件
syntax = "proto3";
package study;
option java_package = "com.dragon.study";
option java_outer_classname = "Student";
option optimize_for = SPEED;
option java_multiple_files = true;
message UserRequest{
int32 age = 1;
}
message UserResponse{
string username = 1;
int32 age = 2;
string city = 3;
}
service StudentService{
rpc GetStudentsByAge(UserRequest) returns(stream UserResponse){}
}
3.利用gradle generateProto生成java类
运行命令:
gradle generateProto
4.编写Server/Client服务
Server
Server端跟上一章一样 《Netty学习打卡–从小白到放弃》----- 15 - netty 之grpc Simple RPC例子
package com
.dragon
.study
.server
;
import com
.dragon
.study
.*
;
import io
.grpc
.stub
.StreamObserver
;
import java
.util
.ArrayList
;
import java
.util
.List
;
public class StudentServiceImpl extends StudentServiceGrpc.StudentServiceImplBase {
@Override
public void getStudentsByAge(UserRequest request
, StreamObserver
<UserResponse> responseObserver
) {
System
.out
.println("getStudentsByAge() 接收到客户端请求的参数 , age = " + request
.getAge());
responseObserver
.onNext(UserResponse
.newBuilder().setAge(15).setUsername("张三").setCity("北京").build());
responseObserver
.onNext(UserResponse
.newBuilder().setAge(20).setUsername("李四").setCity("上海").build());
responseObserver
.onNext(UserResponse
.newBuilder().setAge(25).setUsername("王五").setCity("天津").build());
responseObserver
.onNext(UserResponse
.newBuilder().setAge(30).setUsername("赵六").setCity("深圳").build());
responseObserver
.onNext(UserResponse
.newBuilder().setAge(35).setUsername("七爷").setCity("广州").build());
responseObserver
.onCompleted();
}
}
Client
package com
.dragon
.study
.client
;
import com
.dragon
.study
.StudentRequest
;
import com
.dragon
.study
.StudentResponse
;
import com
.dragon
.study
.StudentServiceGrpc
;
import io
.grpc
.ManagedChannel
;
import io
.grpc
.ManagedChannelBuilder
;
import java
.util
.concurrent
.TimeUnit
;
import java
.util
.logging
.Logger
;
public class StudentClient {
private static final Logger logger
= Logger
.getLogger(StudentClient
.class.getName());
private final ManagedChannel channel
;
private final StudentServiceGrpc
.StudentServiceBlockingStub blockingStub
;
public StudentClient(String host
, int port
){
this(ManagedChannelBuilder
.forAddress(host
,port
).usePlaintext().build());
}
public StudentClient(ManagedChannel channel
) {
this.channel
= channel
;
blockingStub
= StudentServiceGrpc
.newBlockingStub(channel
);
}
public void shutdown() throws InterruptedException
{
channel
.shutdown().awaitTermination(100,TimeUnit
.SECONDS
);
}
public void getUsers(int age
){
Iterator
<UserResponse> Iterator
= blockingStub
.getStudentsByAge(UserRequest
.newBuilder().setAge(10).build());
while (Iterator
.hasNext()){
UserResponse response
= Iterator
.next();
System
.out
.println(response
.getUsername() + "," + response
.getAge() + "," + response
.getCity());
}
}
public static void main(String
[] args
) throws InterruptedException
{
StudentClient client
= new StudentClient("localhost",8080);
try{
client
.getUsers(1)
}finally {
client
.shutdown();
}
}
}
分别启动Server 和 Client端
运行结果:
张三,15,北京
李四,20,上海
王五,25,天津
赵六,30,深圳
七爷,35,广州