Add Grpc to Java Application | Java Code

Name Grpc is a high-performance, open source RPC system.
There are various advantages to using gRPC.

  • It makes development easier by providing a client / server number
  • Works in several languages

It all starts with interpreting the .proto file, the .proto files reside on the src / main / proto file.

Note that it is customary to save proto files on repo and have some sort of schema. In this way some group creators can create their own sdk by naming them, even in other languages.

We will make Order Service at 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;

To work with grpc the following binaries need to be installed

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

Using pure white mvn, the classes are built on target / classes.
The course is sufficient to configure the server and run the client to connect to it.

So let’s try to test the server.

We’re going to set up a project

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 larger team will go around the server and support the request.

package com.egkatzioura.order;


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 look for another large class that can connect to the server and make a grpc request to access 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()
                                             .setEmail("[email protected]")

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

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


So we just created a grpc code, we supported grpc and set it up, the server went round and the client received a response from the server.

You can find the code starting at github.

Published on Java Code Geeks courtesy of Emmanouil Gkatziouras, our friend JCG program. See original article here: Add Grpc to Java Application

The ideas expressed by the developers of Java Code Geeks are their own.

Source link