0
1

More than 1 year has passed since last update.

gRPCを使ってあそんでみる クライアント編

Last updated at Posted at 2022-10-21

gRPCを使ってあそんでみるの目的

ログデータを永続化するgRPCサービスを立ち上げログを記録する。

今回のお題

ログを記録するgRPCサービスに対してメッセージを送信しログに記録されるか確認します。

■事前環境

・Windows10 バージョン21H2
・IntelliJ IDEA 2022.1.4(Community Edition)
・OpenJDK Runtime Environment Zulu17.36+17-CA (build 17.0.4.1+1-LTS)
・Gradle

1.クライアントクラスを作成

サービス起動編の続きとなります。
サービスに対してメッセージを送信するクライアントクラスを作成します。
サービスに対してステータスをINFO、メッセージにhello world.のリクエストを送信するクラスです。
レスポンスステータスを標準出力に表示させています。

ClientMain.java
package logging;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import logging.proto.Logging;
import logging.proto.LoggingServiceGrpc;

public class ClientMain {
    public static void main(String[] args) {
        final String server = "localhost";
        final int port = 8080;
        ManagedChannel channel = ManagedChannelBuilder.forAddress(server, port)
                .usePlaintext()
                .build();

        LoggingServiceGrpc.LoggingServiceBlockingStub stub = LoggingServiceGrpc.newBlockingStub(channel);

        Logging.LoggingRequest request = Logging.LoggingRequest.newBuilder()
                .setStatus("INFO")
                .setMessage("hello world.").build();

        Logging.LoggingResponse response = stub.write(request);

        System.out.println("response status: " + response.getStatus());
    }
}

2.クライアントクラスの実行

クライアントを実行して状態を確認します。
標準出力とログファイルに以下のように出力されます。

> Task :ClientMain.main()
response status: OK
10月 21, 2022 5:10:08 午後 logging.LoggingService write
情報: hello world.

参考

今回、利用したソース一式です。

0
1
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
0
1