grpc第一步

This commit is contained in:
eson 2022-06-10 00:08:18 +08:00
parent 55c3491aeb
commit 80df9b3850
2 changed files with 70 additions and 0 deletions

View File

@ -0,0 +1,24 @@
package com.yuandian.dataflow.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import org.springframework.boot.context.properties.bind.DefaultValue;
import com.fasterxml.jackson.databind.ser.std.StdKeySerializers.Default;
/**
*
*/
public class Struct {
@Retention(RetentionPolicy.SOURCE)
public @interface Field {
long size() default 4;
}
}

View File

@ -6,10 +6,16 @@
*/
package com.yuandian.dataflow.grpc;
import com.google.protobuf.InvalidProtocolBufferException;
import com.yuandian.dataflow.proto.CollectPacketsServerGrpc;
import com.yuandian.dataflow.proto.ApmBaseDataFlowOuterClass.ApmBaseDataFlow;
import com.yuandian.dataflow.proto.Base.Request;
import com.yuandian.dataflow.proto.Base.Response;
import com.yuandian.dataflow.proto.CollectPacketsServerGrpc.CollectPacketsServerImplBase;
import com.yuandian.dataflow.proto.CollectPacketsServerGrpc.CollectPacketsServerStub;
import io.grpc.ManagedChannelBuilder;
import io.grpc.netty.shaded.io.netty.handler.codec.compression.CompressionOptions;
import io.grpc.stub.StreamObserver;
/**
@ -21,5 +27,45 @@ import io.grpc.stub.StreamObserver;
public class CollectPackets extends CollectPacketsServerImplBase {
public static void main(String[] args) throws InvalidProtocolBufferException {
// private final ManagedChannelBuilder<?> managedChannelBuilder;
// private final CollectPacketsServerStub blockingStub;
// private final ManagedChannel channel;
// public Client(String name, int port) {
// }
// public void shutdown() throws InterruptedException {
// channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
// }
// public void sayHi(String name){
// MyThing.Request request = MyThing.Request.newBuilder().setName(name).build();
// MyThing.Response response = blockingStub.sayHi(request);
// System.out.println(response.getName());
// }
var managedChannelBuilder = ManagedChannelBuilder.forAddress("localhost", 4444);
var channel = managedChannelBuilder.usePlaintext().build();
var stub = CollectPacketsServerGrpc.newBlockingStub(channel);
stub.withCompression("snappy");
var response = stub.getPackets(Request.newBuilder().setVersion("v1.1.1").build());
while(true){
var iter = response.next();
for(var p : iter.getPacketsList()) {
var flow = p.unpack( ApmBaseDataFlow.class);
}
}
// Client client = new Client("localhost", 2222);
// for (int i = 0; i < 10; i++) {
// Thread.sleep(1000);
// //进行rpc调用的真正逻辑
// client.sayHi("Hello Server -> " + i);
// }
// client.shutdown();
}
}