インフラ移行プロジェクトで Kotlin + gRPC を導入してみた話

6.3K Views

June 17, 22

スライド概要

Server-Side Kotlin Meetup vol.3

profile-image

サイボウズでインフラエンジニアやってます。

シェア

またはPlayer版

埋め込む »CMSなどでJSが使えない場合

関連スライド

各ページのテキスト
1.

nojima@cybozu Server-Side Kotlin Meetup vol.3

2.

• • • •

3.

• • •

5.

• • •

6.

• •

7.

• • • • •

8.

• • •

9.

AP Store JobWorker cydecc

10.

• • • • • •

11.

• •

12.

• •

13.

Store JobWorker Migrator Service Discovery Database Allocator

15.

• • • • •

16.

• • • • •

17.

// src/main/proto/com/example/kotlingrpc1/v1/HelloWorld.proto syntax = "proto3"; package example.kotlingrpc1.v1; option java_package = "com.example.kotlingrpc1.v1"; service Greeter { rpc SayHello(HelloRequest) returns (HelloResponse) {} } message HelloRequest { string name = 1; } message HelloResponse { string message = 1; }

18.

// src/main/proto/com/example/kotlingrpc1/v1/HelloWorld.proto syntax = "proto3"; package example.kotlingrpc1.v1; option java_package = "com.example.kotlingrpc1.v1"; service Greeter { rpc SayHello(HelloRequest) returns (HelloResponse) {} } message HelloRequest { string name = 1; } message HelloResponse { string message = 1; } SayHello SayHello HelloRequest Greeter HelloResponse

19.

// src/main/proto/com/example/kotlingrpc1/v1/HelloWorld.proto syntax = "proto3"; package example.kotlingrpc1.v1; option java_package = "com.example.kotlingrpc1.v1"; service Greeter { rpc SayHello(HelloRequest) returns (HelloResponse) {} } message HelloRequest { string name = 1; } message HelloResponse { string message = 1; }

20.

• • • • •

22.
[beta]
// src/main/kotlin/GreeterController.kt
import com.example.kotlingrpc1.v1.GreeterGrpcKt
import com.example.kotlingrpc1.v1.HelloWorld.HelloRequest
import com.example.kotlingrpc1.v1.HelloWorld.HelloResponse
class GreeterController : GreeterGrpcKt.GreeterCoroutineImplBase() {

override suspend fun sayHello(request: HelloRequest): HelloResponse =
HelloResponse.newBuilder()
.setMessage("Hello, ${request.name}!!")
.build()
}

23.
[beta]
// src/main/kotlin/GreeterController.kt
import com.example.kotlingrpc1.v1.GreeterGrpcKt
import com.example.kotlingrpc1.v1.HelloWorld.HelloRequest
import com.example.kotlingrpc1.v1.HelloWorld.HelloResponse
class GreeterController : GreeterGrpcKt.GreeterCoroutineImplBase() {

override suspend fun sayHello(request: HelloRequest): HelloResponse =
HelloResponse.newBuilder()
.setMessage("Hello, ${request.name}!!")
.build()
}

GreeterCoroutineImplBase

24.
[beta]
// src/main/kotlin/GreeterController.kt
import com.example.kotlingrpc1.v1.GreeterGrpcKt
import com.example.kotlingrpc1.v1.HelloWorld.HelloRequest
import com.example.kotlingrpc1.v1.HelloWorld.HelloResponse
class GreeterController : GreeterGrpcKt.GreeterCoroutineImplBase() {

override suspend fun sayHello(request: HelloRequest): HelloResponse =
HelloResponse.newBuilder()
.setMessage("Hello, ${request.name}!!")
.build()
}

Greeting.SayHello

HelloRequest

HelloRequest

25.

// gRPC val server = ServerBuilder .forPort(8080) .addService(ProtoReflectionService.newInstance()) .addService(GreeterController()) .build() Runtime.getRuntime().addShutdownHook( Thread { logger.info("Shutting down gRPC server...") server.shutdown() } ) server.start() logger.info("Server started: port={}", port) server.awaitTermination()

26.
[beta]
❱ grpcurl -plaintext -d '{"name": "Nojima"}' ¥
localhost:8080 example.kotlingrpc1.v1.Greeter.SayHello
{
"message": "Hello, Nojima!!"
}

grpcurl

28.

// val channel = ManagedChannelBuilder .forAddress("localhost", server.port) .usePlaintext() .build() val stub = GreeterGrpcKt.GreeterCoroutineStub(channel) // runBlocking { val request = HelloWorld.HelloRequest.newBuilder() .setName("Nojima") .build() val response = stub.sayHello(request) assertEquals(response.message, "Hello, Nojima!!") } // channel.shutdown().awaitTermination(5, TimeUnit.SECONDS)

29.

// val channel = ManagedChannelBuilder .forAddress("localhost", server.port) .usePlaintext() .build() val stub = GreeterGrpcKt.GreeterCoroutineStub(channel) // runBlocking { val request = HelloWorld.HelloRequest.newBuilder() .setName("Nojima") .build() val response = stub.sayHello(request) assertEquals(response.message, "Hello, Nojima!!") } // channel.shutdown().awaitTermination(5, TimeUnit.SECONDS)

31.

• • • • • •

32.

• • • •

33.

• • • •

34.

• • • • • • •

35.

• •

36.

dotcomapis • • • • • dotcomapis-jdk dotcomapis-go

37.

• • • •

38.

• • • • • // ( ) val server = ServerBuilder .forPort(8080) .addService(ProtoReflectionService.newInstance()) //← .addService(GreetingController()) .build()

39.

• •