gRPC with Spring Boot Path II

สืบเนื่องมาจากบทความที่แล้ว https://zengcode.medium.com/grpc-with-spring-boot-889ffc7e3485

เราได้สร้าง gRPC Service ขึ้นมาแล้วก็มีการเรียกใช้ผ่าน Command Line สำหรับตอนนี้เราจะมาสร้าง gRPC Client สำหรับเรียก gRC Sevice ที่เราสร้างจากตอนที่แล้วกันครับ ใช้ Project จากตอนที่แล้วได้เลยนะครับ

เพิ่ม Client ด้วยการเรียกผ่าน Scheduler

package com.zengcode.grpc;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.sql.Timestamp;

@SpringBootApplication
@EnableScheduling

public class GrpcSpringBootExampleApplication {

public static void main(String[] args) {
SpringApplication.run(GrpcSpringBootExampleApplication.class, args);
}


@Scheduled(fixedDelay = 10000)
public void fixedDelayScheduleTask() {
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 9090)
.usePlaintext()
.build();

ZengcodeServiceGrpc.ZengcodeServiceBlockingStub stub = ZengcodeServiceGrpc.newBlockingStub(channel);

System.out.println("Trigger call gRPC");
ZengcodeResponse zengcodeResponse = stub.hello(ZengcodeRequest.newBuilder()
.setMessage("Hi " + new Timestamp(System.currentTimeMillis()))
.build());

System.out.println("Resonse : " + zengcodeResponse.getMessage());

channel.shutdown();
}
}

เมื่อเรารัน Application ก็จะได้ประมาณนี้

โย่วๆ จบปะ

--

--