Add Grpc to Your Java Application | Java code geeks

Grpc is a high performance open source universal RPC framework.
The use of gRPC has various advantages.

  • It simplifies development by providing client / server code
  • It supports multiple languages

It all starts with the definition of a .proto file, the .proto files reside on the src / main / proto file.

Be aware that it is a good practice to keep proto files on a repository and have some schema version control. This way, developers from other teams could generate their sdks by referencing them, even for other languages.

We will create an order service on src / main / proto / Order.proto

syntax = "proto3";

option java_multiple_files = true;
option java_package = "com.egkatzioura.order.v1";

service OrderService {
    rpc ExecuteOrder(OrderRequest) returns (OrderResponse) {};

message OrderRequest {
    string email = 1;
    string product = 2;
    int32 amount = 3;

message OrderResponse {
    string info = 1;

In order to work with grpc, the following binaries must be set

        <dependency> <!-- necessary for Java 9+ -->

By running mvn clean install, the classes will be generated on the target (s).
These classes are more than enough to run a server and run a client to communicate with it.

So let’s try to run the server.

We are going to create an Implementation service

package com.egkatzioura.order.impl;

import com.egkatzioura.order.v1.Order;
import com.egkatzioura.order.v1.OrderServiceGrpc;

import io.grpc.stub.StreamObserver;

public class OrderServiceImpl extends OrderServiceGrpc.OrderServiceImplBase {

    public void executeOrder(Order.OrderRequest request, StreamObserver&amp;lt;Order.OrderResponse&amp;gt; responseObserver) {

        Order.OrderResponse response = Order.OrderResponse.newBuilder()
                                                          .setInfo("Hi "+request.getEmail()+", you order has been executed")


Then our main class will run the server and serve the request.

package com.egkatzioura.order;

import java.io.IOException;

import com.egkatzioura.order.impl.OrderServiceImpl;
import io.grpc.Server;
import io.grpc.ServerBuilder;

public class Application {

    public static void main(String[] args) throws IOException, InterruptedException {
        Server server = ServerBuilder
                .addService(new OrderServiceImpl()).build();



While the server is running we can run another main class which will communicate with the server and execute a grpc request to the server

package com.egkatzioura.order;

import com.egkatzioura.order.v1.Order;
import com.egkatzioura.order.v1.OrderServiceGrpc;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;

public class ApplicationClient {
    public static void main(String[] args) {
        ManagedChannel managedChannel = ManagedChannelBuilder.forAddress("localhost", 8080)

        OrderServiceGrpc.OrderServiceBlockingStub orderServiceBlockingStub
                = OrderServiceGrpc.newBlockingStub(managedChannel);

        Order.OrderRequest orderRequest = Order.OrderRequest.newBuilder()

        Order.OrderResponse orderResponse = orderServiceBlockingStub.executeOrder(orderRequest);

        System.out.println("Received response: "+orderResponse.getInfo());


So we just auto generated some grpc code, we supported a grpc service with an implementation, a server started, and a client got a response from the server.

You can find the source code at github.

Posted on Java Code Geeks with the permission of Emmanuel Gkatziouras, partner of our JCG program. See the original article here: Add Grpc to your Java application

The opinions expressed by contributors to Java Code Geeks are their own.

Source link