Skip to content

Instantly share code, notes, and snippets.

@nakabonne
Last active February 18, 2018 04:13
Show Gist options
  • Save nakabonne/8fc97255b7bc92ec4be66de29526a2b6 to your computer and use it in GitHub Desktop.
Save nakabonne/8fc97255b7bc92ec4be66de29526a2b6 to your computer and use it in GitHub Desktop.
gRPCをRubyで試してみる ref: https://qiita.com/nakabonne/items/6a7cb26c50d3a862f61e
git clone https://github.com/grpc/grpc.git
$ gem install grpc
$ gem install grpc-tools
$ grpc_tools_ruby_protoc -I ../protos --ruby_out=lib --grpc_out=lib ../protos/helloworld.proto
$ ruby examples/ruby/greeter_server.rb
$ ruby examples/ruby/greeter_client.rb
def main(name, count)
stub = Helloworld::Greeter::Stub.new('localhost:50051', :this_channel_is_insecure)
tofu = stub.create_tofu(Helloworld::Daizu.new(name: name, count: count))
p "豆腐の名前は#{tofu.name}"
p "豆腐の大きさは#{tofu.size}"
end
p 豆腐の名前は?
input_name = gets.chomp
p 豆腐の数は?
input_count = gets.to_i
main(input_name, input_count)
class GreeterServer < Helloworld::API::Service
def create_tofu(daizu, _unused_call)
name = "#{daizu.name}_tofu" # リクエストのフィールドはドットで呼び出せる
size = daizu.count * 2
return Helloworld::Tofu.new(name: name, size: size) #レスポンスはメッセージのインスタンス生成をするだけ
end
end
// 大豆から豆腐を作る
service Greeter {
rpc CreateTofu (Daizu) returns (Tofu) {}
}
// 大豆を定義
message Daizu {
string name = 1;
int32 count = 2;
}
// 豆腐を定義
message Tofu {
string name = 1;
int32 size = 2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment