Şu satırı dahil ederiz
<dependency><groupId>net.devh</groupId><artifactId>grpc-server-spring-boot-starter</artifactId><version>2.14.0.RELEASE</version></dependency>
Örnek
Şu satırı dahil ederiz
spring-boot-starter-grpc
application.properties
Örnek
Şöyle yaparız
grpc.server.port=9090 grpc.server.inProcessName=test
Açıklaması şöyle
This configuration specifies that the gRPC server will run on port 9090 and has an in-process name of ‘test’.
proto Dosyası
src/main/proto dizinine yerleştirilir
Örnek
Şöyle yaparız
syntax = "proto3"; option java_multiple_files = true; option java_package = "com.example.grpc"; option java_outer_classname = "GreetProto"; package greet; // The greeting service definition. service GreetService { // Sends a greeting rpc Greet (GreetRequest) returns (GreetResponse); } // The request message containing the user's name. message GreetRequest { string name = 1; } // The response message containing the greetings. message GreetResponse { string greeting = 1; }
@GrpcService Anotasyonu
2 tane daha endpoint ekler. Bunlar şöyle.
grpc.health.v1.Health grpc.reflection.v1alpha.ServerReflection
grpcurl ile test yapılabilir. Şöyle yaparız
grpcurl --plaintext localhost:9090 list grpcurl --plaintext localhost:9090 describe ch.frankel.blog.grpc.model.HelloService grpcurl --plaintext -d '{"name": "John"}' localhost:9090 \ ch.frankel.blog.grpc.model.HelloService/SayHello
Örnek
Şöyle yaparız
import com.example.grpc.*; import io.grpc.stub.StreamObserver; import net.devh.boot.grpc.server.service.GrpcService; @GrpcService public class GreetingService extends GreetServiceGrpc.GreetServiceImplBase { @Override public void greet(GreetRequest request,StreamObserver<GreetResponse> responseObserver) { String name = request.getName(); String greeting = "Hello, " + name + "!"; GreetResponse response = GreetResponse.newBuilder() .setGreeting(greeting) .build(); responseObserver.onNext(response); responseObserver.onCompleted(); } }
Unit Test için şöyle yaparız
import com.example.grpc.*; import io.grpc.inprocess.InProcessChannelBuilder; import io.grpc.inprocess.InProcessServerBuilder; import io.grpc.testing.GrpcCleanupRule; import org.junit.Rule; import org.junit.Test; import static org.junit.Assert.assertEquals; public class GreetingServiceTest { @Rule public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule(); @Test public void greet_shouldReturnGreeting() throws Exception { // Arrange String name = "World"; GreetingService service = new GreetingService(); String serverName = InProcessServerBuilder.generateName(); grpcCleanup.register(InProcessServerBuilder .forName(serverName) .directExecutor() .addService(service) build() .start()); GreetServiceGrpc.GreetServiceBlockingStub stub = GreetServiceGrpc.newBlockingStub( grpcCleanup.register(InProcessChannelBuilder.forName(serverName) .directExecutor().build())); // Act GreetResponse response = stub.greet(GreetRequest.newBuilder().setName(name) .build()); // Assert assertEquals("Hello, World!", response.getGreeting()); } }
Integration Test için şöyle yaparız
import com.example.grpc.*; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import static org.junit.Assert.assertEquals; @RunWith(SpringRunner.class) @SpringBootTest public class GreetingServiceIntegrationTest { @Autowired private GreetServiceGrpc.GreetServiceBlockingStub greetServiceBlockingStub; @Test public void greet_shouldReturnGreeting() { // Arrange String name = "World"; // Act GreetResponse response = greetServiceBlockingStub.greet(GreetRequest.newBuilder() .setName(name).build()); // Assert assertEquals("Hello, World!", response.getGreeting()); } }
Hiç yorum yok:
Yorum Gönder