There are four ways to send data to the GPC server and client, and the startup code of the client startup server is described in the previous article, which will only list the code for the key implementation. 1. The client sends an object, and the server returns an object
This approach is similar to the traditional way of HTTP request data, in the previous article there is a simple implementation example, which is no longer described here. 2. The client sends an object, and the server returns a Stream object
The stream object is treated as a collection during transmission and is traversed with iterator. Consider an implementation example:
Proto file:
RPC Getstudentsbyage (studentrequest) returns (Stream studentresponse) {}
GRPC and thrift are different in that the requested and returned objects must be a message, and declaring the object as a stream will be streamed in the same way.
Server-side implementations:
@Override public
void Getstudentsbyage (studentrequest request, streamobserver<studentresponse> Responseobserver) {
System.out.println ("Accept client Information:" + request.getage ());
Responseobserver.onnext (Studentresponse.newbuilder (). SetName ("Zhang San"). Setage ("Beijing"). Build ());
Responseobserver.onnext (Studentresponse.newbuilder (). SetName ("John Doe"). Setage ("Tianjin"). Build ());
Responseobserver.onnext (Studentresponse.newbuilder (). SetName ("Harry"). Setage ("Wuhan"). Build ());
Responseobserver.onnext (Studentresponse.newbuilder (). SetName ("Zhao Liu"). Setage ("Shenzhen"). Build ());
Responseobserver.oncompleted ();
}
In the first way, this method assembles multiple messages by calling OnNext multiple times, which finally returns a Stream object.
Client implementations:
iterator<studentresponse> iterable = Blockingstub.getstudentsbyage (Studentrequest.newbuilder (). SetAge (20). Build ());
while (Iterable.hasnext ()) {
Studentresponse studentresponse = Iterable.next ();
System.out.println (Studentresponse.getname () + "," + studentresponse.getage () + "," + studentresponse.getcity ());
}
3. The client sends a Stream object, and the server returns a Simple object
Proto file:
RPC Getstudentswrapperbyages (Stream studentrequest) returns (Studentresponselist) {}
Server-side implementations:
@Override public streamobserver<studentrequest> getstudentswrapperbyages (streamobserver< Studentresponselist> responseobserver) {return new streamobserver<studentrequest> () {@Overr IDE public void OnNext (studentrequest value) {System.out.println ("OnNext:" + value.getage ())
; } @Override public void OnError (Throwable t) {System.out.println (T.getmessage ())
; } @Override public void oncompleted () {Studentresponse studentresponse = Student
Response.newbuilder (). SetName ("Zhang San"). Setage (). Setcity ("Xian"). Build ();
Studentresponse StudentResponse2 = Studentresponse.newbuilder (). SetName ("John Doe"). Setage ("Guangzhou"). Build (); Studentresponselist studentresponselist = Studentresponselist.newbuilder (). Addstudentresponse (studentResponse). Ad
Dstudentresponse (STUDENTRESPONSE2). build (); Responseobserver.onnext (studentresponselist);
Responseobserver.oncompleted ();
}
}; }
The client calls the OnNext method once per message, and when the client has finished sending it, it executes oncompleted to return an object to the client.
Client implementations:
streamobserver<studentresponselist> studentresponseliststreamobserver = new streamobserver<
Studentresponselist> () {@Override public void OnNext (studentresponselist value) { Value.getstudentresponselist (). ForEach (Studentresponse, {System.out.println (studentresponse.
GetName ());
System.out.println (Studentresponse.getage ());
System.out.println (Studentresponse.getcity ());
System.out.println ("***********");
}); } @Override public void OnError (Throwable t) {System.out.println (T.getmessage ())
;
} @Override public void oncompleted () {System.out.println ("completed!");
}
}; streamobserver<studentrequest> Studentrequeststreamobserver = Studentservicestub.getstudentswrapperbyages ( Studentresponseliststreamobserver);
Studentrequeststreamobserver.onnext (Studentrequest.newbuilder (). Setage (). build ());
Studentrequeststreamobserver.onnext (Studentrequest.newbuilder (). Setage (). build ());
Studentrequeststreamobserver.onnext (Studentrequest.newbuilder (). Setage (+). Build ());
Studentrequeststreamobserver.onnext (Studentrequest.newbuilder (). Setage (). build ()); Studentrequeststreamobserver.oncompleted ();
Studentresponseliststreamobserver This section is a response to the data returned by the service side, Studentrequeststreamobserver This part is used to send a stream object to the server. When execution does not have the results we expect, there is a key point to note:
The client sends a stream to the server and cannot use the blocked Stu to send the request, but instead uses the asynchronous STB to handle it:
Studentservicegrpc.studentservicestub studentservicestub = studentservicegrpc.newstub (ManagedChannel);
The source code is as follows:
/**
* Creates a new async stub that supports all call types for the service
*/public
static Studentservicestu b newstub (Io.grpc.Channel channel) {
return new studentservicestub (channel);
}
Create an asynchronous stub to invoke the service, even when the message sent by OnNext is not sent to the server, the entire program executes, because it is asynchronous and there is no blocking wait, you can add a thread.sleep (seconds) at the end of the program. Then the program executes normally and can verify the asynchronous problem. 4. The Stream object is transmitted by both the client and the server
Proto file:
RPC Bitalk (Stream streamrequest) returns (Stream streamresponse) {}
Server-side implementations:
@Override public
streamobserver<streamrequest> Bitalk (streamobserver<streamresponse> Responseobserver) {
return new streamobserver<streamrequest> () {
@Override public
void OnNext ( Streamrequest value) {
System.out.println ("onnest:" + value.getrequestinfo ());
Responseobserver.onnext (Streamresponse.newbuilder (). Setresponseinfo (Uuid.randomuuid (). toString ()). build ());
}
@Override public
void OnError (Throwable t) {
System.out.println (t.getmessage ());
}
@Override public
void oncompleted () {
responseobserver.oncompleted ();
}
};
}
Client implementations:
@Override public
void OnNext (streamresponse value) {
System.out.println (Value.getresponseinfo ());
}
@Override public
void OnError (Throwable t) {
System.out.println (t.getmessage ());
}
@Override public
void oncompleted () {
System.out.println ("oncompleted!");
}
});
for (int i = 0; I < 10;i + +) {
Requeststreamobserver.onnext (Streamrequest.newbuilder (). Setrequestinfo ( Localdatetime.now (). toString ()). build ());
try {
thread.sleep;
} catch (Interruptedexception e) {
e.printstacktrace ()}
}
Source code Address:
Https://github.com/huiGod/netty_lecture
Main.java.com.huiGod.grpc