ลองสร้าง gRPC Service ด้วย Spring Boot

Phayao Boonon
3 min readDec 23, 2018

--

หลังจากที่ได้ลองใช้ gRPC framework สร้าง Server และ Client ด้วย Java ไปแล้วก็เลยสงสัยว่าถ้าเราใช้ Spring framework มาสร้าง gRPC Service (Server) บ้างละ สามารถทำได้ไหม ผมก็ไปเจอ grpc-spring-boot-starter ของ LogNet ได้สร้างโมดูล gRPC สำหรับ Spring Boot แล้ว ถ้างั้นลองไปสร้าง gRPC Service ด้วย Spring Boot กันบ้างดีกว่า

เริ่มกันเลย

จากการสร้าง gRPC ด้วย Java เราจะต้องเพิ่ม grpc-netty, grpc-protobuf และ grpc-stub dependency แต่ว่าสำหรับ Spring Boot แล้วใช้เพียง grpc-spring-boot-starter เท่านั้น ดังนี้ (version 3.0.0 เป็นต้นมา จะเปลี่ยนชื่อ package จาก org.lognet เป็น io.github.lognet)

<dependency>
<groupId>io.github.lognet</groupId>
<artifactId>grpc-spring-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>

Protocal Buffer config

สำหรับกำหนด configuration พื้นฐานของ protocal buffers สำหรับ Service ใช้ syntax เวอร์ชั่น proto3 และ java option แบบแยกไฟล์ สุดท้ายกำหนด package ของ Service ที่จะสร้าง Java classes

กำหนด data type ของ request/response ของ Message เป็น HelloRequest / HelloResponse, กำหนดชื่อของ service เป็น HelloService และ operation เป็น hello() ในกับ service contract

syntax = "proto3";
option java_multiple_files = true;
package com.iphayao.grpc;

message HelloRequest {
string fistName = 1;
string lastName = 2;
}

message HelloResponse {
string greeting = 1;
}

service HelloService {
rpc hello(HelloRequest) returns (HelloResponse);
}

Application Config

ซึ่ง gRPC Spring Boot จะ config ให้กับ Spring application โดยอัตโนมัตด้วย ดังนั้นจะกำหนดค่า port ของ gRPC Service เป็น 8080 (ค่าตั้งต้นเป็น 6565)

grpc:
port: 8080

Generate Code

หลังจากที่สร้างไฟล์ .proto แล้วจะต้อง generate code จากไฟล์นี้ ด้วย protocal buffers compiler ซึ่งในบทความนี้จะใช้ maven plugin เพื่อ generate code

<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.1</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<protocArtifact>
com.google.protobuf:protoc:3.3.0:exe:${os.detected.classifier}
</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>
io.grpc:protoc-gen-grpc-java:1.4.0:exe:${os.detected.classifier}
</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>

Create Service

สำหรับ Service จะใช้ @GRpcService annotation ในการสร้าง Server ซึ่ง extends จาก generate code ที่ได้จาก protocal buffers compiler เพื่อส่ง HelloResponse กลับไปยัง client ที่ request มา ซึ่งไม่ต้องสร้าง GrpcServer class อีก

@GRpcService
public class HelloGrpcServiceImpl extends HelloServiceGrpc.HelloServiceImplBase {
@Override
public void hello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
String greeting = new StringBuffer()
.append("Hello, ")
.append(request.getFistName())
.append(" ")
.append(request.getLastName())
.toString();

HelloResponse response = HelloResponse.newBuilder()
.setGreeting(greeting)
.build();

responseObserver.onNext(response);
responseObserver.onCompleted();
}
}

ทดลอง Run

สำหรับทดลอง run ก็จะใช้คำสั่ง mvn spring-boot:run ปรกติ จะได้ gRPC Service (Server) ที่รอรับ request จาก client

ทดลองใช้ client จาก gRPC ด้วย Java มาทดสอบ request ไปยัง gRPC Service ที่เราได้สร้างขึ้นมาใหม่ จะเห็นได้ว่ามีการตอบกลับจาก Service กลับมาเป็น Hello, Demo gRPC

สรุป

จากการใช้ grpc-spring-boot-starter มาสร้าง gRPC Service ด้วย Java และ maven นี้จะเห็นได้ว่าลดขั้นตอนของการสร้าง Server ออกไป ซึ่งโมดูลนี้จะทำหน้าที่สร้าง server ให้เราเองและนี้ก็เป็นเพียงตัวอย่างง่ายๆ ของการใช้โมดูลนี้ที่มีความสามารถอีกมาก ซึ่งก็สามารถอ่านและลองทำตามได้จาก readme ของ project

--

--

Phayao Boonon
Phayao Boonon

Written by Phayao Boonon

Software Engineer 👨🏻‍💻 Stay Hungry Stay Foolish

Responses (1)