이전 포스팅을 반드시 보고와주세요!!
https://velog.io/@dinnertime/Spring-Boot%EC%97%90%EC%84%9C-GRPC-%EC%82%AC%EC%9A%A9%ED%95%98%EA%B8%B0-1-GRPC-proto-interface%EB%A7%8C%EB%93%A4%EA%B8%B0
개발환경은 vscode 입니다.
빌드도구는 Maven을 사용합니다.
vscode > F1 > Spring Create 검색 > Create a Maven Project 선택
<dependency>
<groupId>net.devh</groupId>
<artifactId>grpc-server-spring-boot-starter</artifactId>
<version>3.1.0.RELEASE</version>
</dependency>
<!-- Local Repository 설정 -->
<repositories>
<repository>
<id>local-repository</id>
<name>local repository</name>
<url>file://${project.basedir}/lib</url>
</repository>
</repositories>
<!-- Local dependency 추가 -->
<dependencies>
<!-- 기존 dependencies... -->
<dependency>
<groupId>grpc</groupId>
<artifactId>grpc-interface</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
import example.proto.Example.HelloRequest;
import example.proto.Example.HelloResponse;
import example.proto.ExampleProtoServiceGrpc.ExampleProtoServiceImplBase;
import io.grpc.stub.StreamObserver;
import net.devh.boot.grpc.server.service.GrpcService;
@GrpcService
// {proto에 설정된 package}.{proto에 설정된 서비스}Grpc.{proto에 설정된 서비스}ImplBase를 extends
public class GrpcExampleService extends ExampleProtoServiceImplBase {
// 자동완성 기능으로 Override해줍니다.
@Override
public void sayHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
// ... 원하는 코드 작성
// 아래는 예시 코드
HelloResponse reply = HelloResponse.newBuilder()
.setMessage("Hello " + request.getName())
.build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
}
application.properties에 아래와 같이 grpc port 설정하기
grpc.server.port=9090
GRPC Server 처럼 프로젝트를 생성합니다.
Spring Web
을 추가합니다.<dependency>
<groupId>net.devh</groupId>
<artifactId>grpc-client-spring-boot-starter</artifactId>
<version>3.1.0.RELEASE</version>
</dependency>
import org.springframework.stereotype.Service;
import example.proto.Example.HelloRequest;
import example.proto.ExampleProtoServiceGrpc.ExampleProtoServiceBlockingStub;
import net.devh.boot.grpc.client.inject.GrpcClient;
@Service
public class GrpcExampleService {
// @GrpcClient("{client 이름 - properties에서 설정할때 사용}")
@GrpcClient("grpcExample")
// {proto에 설정된 package}.{proto에 설정된 서비스}Grpc.{proto에 설정된 서비스}BlockingStub
private ExampleProtoServiceBlockingStub exampleProtoServiceBlockingStub;
public String runExample(String name) {
HelloRequest request = HelloRequest.newBuilder().setName(name).build();
return exampleProtoServiceBlockingStub.sayHello(request).getMessage();
}
}
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/")
public class GrpcTestController {
// GrpcClient를 구현한 service inject
private final GrpcExampleService grpcExampleService;
public GrpcTestController(GrpcExampleService grpcExampleService) {
this.grpcExampleService = grpcExampleService;
}
@GetMapping("{name}")
public String getMethodName(@PathVariable String name) {
return grpcExampleService.runExample(name);
}
}
application.properties에 아래와 같이 설정해줍니다.
## web server port
server.port=8080
## 호출할 grpc server 주소
## grpc.client.{grpcClientName}.address=static://{host}:{port}
grpc.client.grpcExample.address=static://127.0.0.1:9090
grpc.client.grpcExample.negotiation-type=plaintext
GRPC Server와 GRPC Client를 모두 실행하여 정상동작하는지 확인!!