In the previous tutorial, after implementing the gRPC server, I used the Postman tool to call the gRPC server services. If we use code, how should we do it? In this tutorial, I will guide you to do this!
First, I will create a new Maven project as an example:
This example application will consume the HelloService which I implemented in the previous tutorial!
gRPC uses the concept of a channel to create a connection to the gRPC server, client applications will use this channel to create stubs to interact with the gRPC server. When using the Protocol Buffers Maven Plugin to generate Java code from the service contract .proto file, this plugin also generates Stub classes and we can use these Stub classes to communicate with the gRPC server.
For the example of this tutorial, I also use the service contract .proto file and configure the Protocol Buffers Maven plugin as in the previous tutorial.
The Stub classes that the Protocol Buffers Maven Plugin generates are in the HelloServiceGrpc class:
Now, to consume the HelloService of the gRPC server in the previous tutorial, we will initialize the channel object using the host and port information of the gRPC server first:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.huongdanjava.grpc; import io.grpc.ManagedChannel; import io.grpc.ManagedChannelBuilder; public class Application { public static void main(String[] args) { ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 8080) .usePlaintext() .build(); } } |
The ManagedChannel class implements the gRPC Java’s Channel interface and we use the ManagedChannelBuilder class to create an object of the ManagedChannel class.
By default, the gRPC channel will use a secure connection to connect to the gRPC server. You can configure it to use plaintext by using the usePlaintext() method of the ManagedChannelBuilder class.
gRPC supports synchronous and asynchronous calls to the gRPC server. Synchronous means the client will call the gRPC server and wait for a response, while asynchronous means the response will return asynchronously. Corresponding to the 2 call mechanisms, we will have corresponding Stub classes. For my example, you can use the HelloServiceBlockingStub class to call the gRPC server synchronously, and if you want to use asynchronous, you can use the HelloServiceStub class!
I will use synchronous as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.huongdanjava.grpc; import io.grpc.ManagedChannel; import io.grpc.ManagedChannelBuilder; public class Application { public static void main(String[] args) { ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 8080) .usePlaintext() .build(); HelloServiceGrpc.HelloServiceBlockingStub stub = HelloServiceGrpc.newBlockingStub(channel); HelloResponse helloResponse = stub.hello(HelloRequest.newBuilder() .setName("Khanh") .build()); System.out.println(helloResponse.getMessage()); channel.shutdown(); } } |
We will create a synchronous stub using the static method newBlockingStub() of the HelloServiceGrpc class. The parameter of this newBlockingStub() method is the channel to the gRPC server that we need to interact with.
After having the stub object, we can now call the hello operation of the gRPC server using this stub object. We will build a request message and receive a response message from the gRPC server.
The result when I run this example is as follows:
So we have successfully consumed the hello operation of the gRPC server!