LoginSignup
1
0

More than 5 years have passed since last update.

異なる言語間でgRPCを使う(server:golang、client:java)

Posted at

公式のチュートリアルを参考に試す
https://grpc.io/docs/quickstart/

環境

$ go version
go version go1.12.4 darwin/amd64
$ java -version
java version "1.8.0_131"

サーバ

greeter_server/main.go
func (s *server) SayTestRpc(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
  log.Printf("Received: %v", in.Name)
  return &pb.HelloReply{Message: "This is go server rpc" + in.Name}, nil
}

クライアント

src/main/java/io/grpc/examples/helloworld/HelloWorldClient.java
public void checkrpc(String name) {
  HelloRequest request = HelloRequest.newBuilder().setName(name).build();
  HelloReply response;
  try {
    response = blockingStub.sayTestRpc(request);
  } catch (StatusRuntimeException e) {
    logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
    return;
  }
  logger.info("Greeting: " + response.getMessage());
}

動作確認

# server
$ go run greeter_server/main.go
# client
$ ./build/install/examples/bin/hello-world-client
〜省略〜 Greeting: This is go server rpcworld

http2でPOSTしている
スクリーンショット 2019-05-02 9.48.17.png

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0