Try using gRPC in Ruby

Introduction

I would like to practice gRPC while making a simple app using Ruby.

This time we will create a calculator CLI application.

If you enter 1 + 2 etc. on the terminal, the client will send the character string "1 + 2" to the server, and the server will calculate the answer and return it as a response. Implement this with gRPC.

Preparation

Install the grpc and grpc-tools gems.

$ gem install grpc
$ gem install grpc-tools

Create a directory for your app. The name can be anything, but for now let's call it grpc_calc.

$ mkdir grpc_calc
$ cd grpc_calc

Implementation

Define Protocol Buffers

We will create a proto file.

grpc_calc $ mkdir protos
grpc_calc $ vi protos/calc.proto
 // Create this proto file with the proto3 syntax.
syntax = "proto3";

 // Create with the package name example. (* The name can be anything.)
package example;

 // Define the computer service.
service Calc {
 // Define a method called solve.
 // If you pass Input as an argument, it will return Output.
  rpc Solve (Input) returns (Output) {}
}

 // This is the formula passed from the client.
message Input {
 // Define a string with the name question. The tag is 1.
  string question = 1;
}

 // The answer returned by the server.
message Output {
 // Define a number with the name answer. The tag is 1.
  int32 answer = 1;
}

Create a lib directory.

grpc_calc $ mkdir lib

Compile protps/calc.proto and output it under lib.

grpc_calc $ grpc_tools_ruby_protoc -I protos --ruby_out=lib --grpc_out=lib protos/calc.proto

Then, I think that calc_pb.rb and calc_services_pb.rb are created under lib.

Service implementation

We will implement it from the server side.

require 'grpc'
require_relative 'lib/calc_services_pb.rb'


class CalcServer < Example::Calc::Service
  def solve(input, _unused_call)
    answer = eval(input.question)

    return Example::Output.new(answer: answer)
  end
end

def main
  s = GRPC::RpcServer.new
  s.add_http2_port('0.0.0.0:50051', :this_port_is_insecure)
  s.handle(CalcServer)
  s.run_till_terminated_or_interrupted([1, 'int', 'SIGQUIT'])
end

main

We will also implement it on the client side.

require 'grpc'
require_relative 'lib/calc_services_pb.rb'

def main(input)
  stub = Example::Calc::Stub.new('localhost:50051', :this_channel_is_insecure)
  output = stub.solve(Example::Input.new(question: input))
 p "The answer is # {output.answer}"
end

 p "Write the formula"
input = gets.chomp

main(input)

This completes the implementation.

I will actually move it

Server startup

grpc_calc $ ruby calc_server.rb

Client execution

grpc_calc $ ruby calc_client.rb
 "Write the formula"
10 + 20 + 30
 "The answer is 60"

If it looks like the above, you are successful!

Recommended Posts

Try using gRPC in Ruby
Try using RocksDB in Java
Try using Cocoa from Ruby
[Programming Encyclopedia] §2 Try using Ruby
Try to implement Yubaba in Ruby
Implement a gRPC client in Ruby
Try file locking in Ruby directory
Try using libGDX
Try using Maven
Try using powermock-mockito2-2.0.2
Try using GraalVM
Try using the Stream API in Java
Try using jmockit 1.48
Class in Ruby
Try using JSON format API in Java
Try using SwiftLint
Try using Log4j 2.0
Try Ruby Minitest
Heavy in Ruby! ??
Try using Sourcetrail (win version) in Java code
Try using GCP's Cloud Vision API in Java
Try using Sourcetrail (macOS version) in Java code
Try communication using gRPC on Android + Java server
Try using the COTOHA API parsing in Java
Try using Axon Framework
Try using JobScheduler's REST-API
Try using java.lang.Math methods
Try LetCode in Ruby-TwoSum
Try using Talend Part 2
Output triangle in Ruby
Try using Talend Part 1
Try using F # list
Variable type in ruby
Try using each_with_index method
Fast popcount in Ruby
Try using Spring JDBC
Examine the elements in the array using the [Ruby] includes? Method
Try gRPC in Spring Boot & Spring Cloud project (Mac OS)
Try global hooking in Java using the JNativeHook library
Try using the query attribute of Ruby on Rails
ABC177 --solving E in Ruby
Try implementing Yubaba in Kinx
[Ruby] Count an even number in an array using the even? Method
[Ruby] Searching for elements in an array using binary search
Validate JWT token in Ruby
Implemented XPath 1.0 parser in Ruby
Implement Thread in Java and try using anonymous class, lambda
Read design patterns in Ruby
Try using GloVe with Deeplearning4j
Try using view_component with rails
Write class inheritance in Ruby
Try scraping using java [Notes]
Try developing Spresense in Java (1)
Try functional type in Java! ①
Try using letter_opener_web for inquiries
Try gRPC with Java, Maven
Japaneseize using i18n with Rails
Creating a calendar using Ruby
[Swift] Try using Collection View
Update Ruby in Unicorn environment
Integer unified into Integer in Ruby 2.4