背景:接上一篇JMeter二次开发之gRPC插件开发(一)
目录
1.创建拦截器
package grpc.client;
import io.grpc.*;
import io.grpc.ForwardingClientCall.SimpleForwardingClientCall;
import io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener;
public class HeaderClientInterceptor implements ClientInterceptor{
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next){
return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)){
@Override
public void start(Listener<RespT> respTListener, Metadata headers){
if(headers != null){
Metadata fixedHeaders = new Metadata();
Metadata.Key<String> key = Metadata.Key.of("header", Metadata.ASCII_STRING_MARSHALLER);
fixedHeaders.put(key, "test");
headers.merge(fixedHeaders);
}
super.start(new SimpleForwardingClientCallListener<RespT>(respTListener){
@Override
public void onHeaders(Metadata headers){
super.onHeaders(headers);
}
}, headers);
}
};
}
}
2.gRPC Server端
package grpc.server;
import com.java.grpc.stub.*;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;
import java.io.IOException;
public class GrpcServer{
private int port = 50052; // 服务器端端口
private Server server; // 服务器
// 启动服务
private void start() throws IOException{
System.out.println("start service...");
server = ServerBuilder.forPort(port).addService(new GreeterImpl()).build().start();
System.out.println("service started, listening on " + port);
// 程序退出时关闭资源
Runtime.getRuntime().addShutdownHook({
@Override
public void run(){
System.err.println("*** shutting down gRPC server since JVM is shutting dow");
GrpcServer.this.stop();
System.err.println("*** server shut down");
}
});
}
// 关闭服务
private void stop(){
if(server != null){
server.shutdown();
}
}
// 使server一直处于运行状态
private void blockUntilShutdown() throws InterruptedException{
if(server != null){
server.awaitTermination();
}
}
// 实现定义一个实现服务接口的类
private class GreeterImp extends GreeterGrpc.GreeterImplBase{
public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver){
System.out.println("From Service: " + req.getName());
HelloReply helloReply = HelloReply.nameBuilder().setMessage("Hello: " + req.getName()).build();
responseObserver.onNext(helloReply);
responseObserver.onCompleted();
}
}
// 该类的自我调试
// public static void main(String[] args) throws IOException, InterruptedException{
// final GrpcServer grpcServer = new GrpcServer();
// grpcServer.start();
// grpcServer.blockUntilShutdown();
// }
}
3.gRPC Client端
package grpc.client;
import com.java.grpc.stub.*;
import io.grpc.ManagedChannel;
import io.grpc.netty.NettyChannelBuilder;
import java.util.concurrent.TimeUnit;
public class GrpcClient{
public ManagedChannel channel; // gRPC信道,需要指定端口和地址
public GreeterGrpc.GreeterBlockingStub stub; // 阻塞/同步存根
public HelloRequest helloRequest; // HelloRequest为proto3文件中定义的请求
// 客户端构造函数。连接服务端的host和port
public GrpcClient(String host, int port){
this.channel = createChannel(host, port);
this.stub = createBlockingStub(this.channel);
}
// 创建信道
public ManagedChannel createChannel(String host, int port){
channel = NettyChannelBuilder.forAddress(host, port).userPlaintext().build();
return channel;
}
// 创建存根
public GreeterGrpc.GreeterBlockingStub createBlockingStub(ManagedChannel channel){
stub = GreeterGrpc.newBlockingStub(channel);
return stub;
}
// 发送请求的方法
public GrpcClient createHelloRequest(String name){
helloRequest = HelloRequest.newBuilder().setName(name).build();
return this;
}
// 接收响应的方法。HelloReply为proto3文件中定义的响应
public HelloReply sendReadRequestToService(GrpcClient client, String name){
client = client.createHelloRequest(name);
HelloReply helloReply = stub.withInterceptors(new HeaderClientInterceptor()).sayHello(helloRequest); // sayHello为proto3文件中定义的rpc方法名
System.out.println(helloReply.getMessage());
return helloReply;
}
// 关闭连接
public void shutdown() throws InterruptedException{
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}
// 该类的自我调试,需先开启server端
// public static void main(String args[]){
// GrpcClient client = new GrpcClient("127.0.0.1", 50052); // 与服务器端的IP和port一致
// client.createHelloRquest("zhangsan");
// client.sendReadRequestToService(client, "zhangsan");
// System.out.println("send over");
// }
}