Last active
February 18, 2018 04:13
-
-
Save nakabonne/8fc97255b7bc92ec4be66de29526a2b6 to your computer and use it in GitHub Desktop.
gRPCをRubyで試してみる ref: https://qiita.com/nakabonne/items/6a7cb26c50d3a862f61e
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
git clone https://github.com/grpc/grpc.git |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ gem install grpc | |
$ gem install grpc-tools |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ grpc_tools_ruby_protoc -I ../protos --ruby_out=lib --grpc_out=lib ../protos/helloworld.proto |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ruby examples/ruby/greeter_server.rb |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ruby examples/ruby/greeter_client.rb |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 大豆から豆腐を作る | |
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