In this article, we are going to see the implementation of the gRPC client interceptor with java. we already discuss the client interceptor and server interceptor and also see the implementation of the server interceptor in the previous post https://thecodedata.com/grpc-interceptors-with-java/ . In this article, our main task is the implement gRPC client interceptor with java.
Here we assuming that you already go through the previous gRPC posts and as well as you created the gRPC project with server interceptor.
Here we are going to create a gRPC client in the same project where we implemented gRPC Server interceptor. gRPC Server is running on port 8000 with server interceptor.
gRPC Client
With the help of the gRPC Client, we can make a call to the server and perform our desired operations. for the gRPC client, a ‘ManagedChannel’ channel is required, and this channel is used for making calls to the server. If you are creating a new microservice for gRPC client, you have to add the same proto file and generate proto Stubbs to communicate with the gRPC server microservice.
GRPCClient.java
package client;
import TCD.helloGRPC.helloGRPCGrpc;
import TCD.helloGRPC.request;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
public class GRPCClient {
public static void main(String[] args) {
ManagedChannel channel = ManagedChannelBuilder
.forAddress("localhost",8000)
.usePlaintext().build();
helloGRPCGrpc.helloGRPCBlockingStub blockingStub = helloGRPCGrpc.newBlockingStub(channel);
request req = request.newBuilder().setName("Author").build();
System.out.println(blockingStub.hello(req));
}
}
Here we first created a ManagedChannel with the server port number. After that, we created blocking stubs for making a call to the server. After that, we prepared a request message and make a call to the hello end point of the gRPC server.
first, run the gRPC server and after that run the gRPC client and see the response from the server. This is the response from server –
For adding meta data with channel we will use client interceptor and add proper token with the help of this grp client interceptor.
let’s first create a JWT token generator to generate desired JWT token after that we will implement Client Interceptor.
JWT token generator with Java
Here we will generate a JWT token with the same signing key as expected at the Server.
JWTTokenGenerator.java
package jwt;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import java.util.Date;
public class JWTTokenGenerator {
public String jwtToken()
{
return Jwts.builder()
.setSubject("test")
.setIssuedAt(new Date(new Date().getTime()))
.signWith(SignatureAlgorithm.HS256,"grpc server with java").compact();
}
}
gRPC Client Interceptor Implementation Using Java
GRPCClientInterceptor.java
package client;
import io.grpc.*;
import jwt.JWTTokenGenerator;
public class GRPCClientInterceptor implements ClientInterceptor {
JWTTokenGenerator tokenGenerator = new JWTTokenGenerator();
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall
(MethodDescriptor<ReqT, RespT> methodDescriptor, CallOptions callOptions, Channel channel) {
return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(channel.newCall(methodDescriptor,callOptions)) {
@Override
public void start(Listener<RespT> responseListener, Metadata headers) {
headers.put(Metadata.Key.of("Token",Metadata.ASCII_STRING_MARSHALLER),"Bearer "+tokenGenerator.jwtToken());
super.start(responseListener, headers);
}
};
}
}
Here we are implementing the ClientInterceptor interface and overriding the interceptCall method for adding a JWT token with gRPC calls.
Now everything is done, only we have to add this gRPC Client interceptor on the channel as we added the gRPC server interceptor on the server.
updated GRPCClient.java
package client;
import TCD.helloGRPC.helloGRPCGrpc;
import TCD.helloGRPC.request;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
public class GRPCClient {
public static void main(String[] args) {
ManagedChannel channel = ManagedChannelBuilder
.forAddress("localhost",8000)
.intercept(new GRPCClientInterceptor())
.usePlaintext().build();
helloGRPCGrpc.helloGRPCBlockingStub blockingStub = helloGRPCGrpc.newBlockingStub(channel);
request req = request.newBuilder().setName("Author").build();
System.out.println(blockingStub.hello(req));
}
}
Now if you the client then, you will get proper response from the server because with every call proper JWT token is added by Client interceptor