6.2K Views
June 17, 22
スライド概要
Server-Side Kotlin Meetup vol.3
サイボウズでインフラエンジニアやってます。
nojima@cybozu Server-Side Kotlin Meetup vol.3
• • • •
• • •
• • •
• •
• • • • •
• • •
AP Store JobWorker cydecc
• • • • • •
• •
• •
Store JobWorker Migrator Service Discovery Database Allocator
• • • • •
• • • • •
// 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; }
// 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
// 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; }
• • • • •
// 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()
}
// 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
// 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
// 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()
❱ grpcurl -plaintext -d '{"name": "Nojima"}' ¥
localhost:8080 example.kotlingrpc1.v1.Greeter.SayHello
{
"message": "Hello, Nojima!!"
}
grpcurl
// 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)
// 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)
• • • • • •
• • • •
• • • •
• • • • • • •
• •
dotcomapis • • • • • dotcomapis-jdk dotcomapis-go
• • • •
• • • • • // ( ) val server = ServerBuilder .forPort(8080) .addService(ProtoReflectionService.newInstance()) //← .addService(GreetingController()) .build()
• •