syntax = "proto3";
package grpc.mock;
service Mock {
rpc GetMockInfo(MockRequest) returns (MockResponse) {}
}
---------------------------------------------------
syntax = "proto3";
package grpc.mock;
message MockRequest {
string mock_code = 1;
string mock_name = 2;
}
message MockResponse {
string code = 1;
string name = 2;
string stack_trace = 3;
}
client, server 쪽 모두 이와 같은 형식으로 proto 파일을 만든다.
grpc:
client:
mock: # service -> rpc 메소드로 정의해놓은 proto 파일 명 명시
negotiationType: PLAINTEXT
address: static://localhost:50013 # port는 뭔 기준? 리서치 ㄱㄱ -> 맘대로 해도 됨
@GrpcClient("mock") // service -> rpc 메소드로 정의해놓은 proto 파일 명 명시
public void setMockBlockingStub(MockGrpc.MockBlockingStub mockBlockingStub) {
this.mockBlockingStub = mockBlockingStub;
}
public proto만든클래스이름.MockResponse getMockInfo(String mockCode, String mockName) {
proto만든클래스이름.MockRequest request = proto만든클래스이름.MockRequest.newBuilder()
.setMockCode(mockCode)
.setMockName(mockName)
.build();
log.info("Try Test to get mock info ...");
return mockBlockingStub.getMockInfo(request);
}
@GetMapping("/grpc")
public List<String> getMockGrpc() {
proto만든클래스이름.MockResponse response = clientService.getMockInfo(
"test mock code",
"test grpc"
);
return List.of(response.getCode(), response.getName());
}
@RequiredArgsConstructor
@Slf4j
@GRpcService
public class MockService extends MockGrpc.MockImplBase { // 이거 extends 무조건 받아야 protoc가 컴파일 할 때 인식하고 반영함
public void getMockInfo(MockMessages.MockRequest request, StreamObserver<AssetMessages.MockResponse> responseObserver) {
log.info("Try to get mock info ...");
MockMessages.MockResponse response = MockMessages.MockResponse.newBuilder()
.setCode(request.getMockCode())
.setName(request.getMockName())
.build();
responseObserver.onNext(response);
responseObserver.onCompleted();
log.info("Success to get mock info !!!");
}
}