proto module

2021. 5. 8. 20:29Spring Micro Services/gRPC

반응형

결제서버에서 결제승인 요청에 대한 input, output, service 메소드 정의 및 코드 생성을 위한 Maven Module 이다.

 

01.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-msa-with-grpc</artifactId>
        <groupId>com.roopy</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>proto</artifactId>

    <properties>
        <maven.compiler.source>9.0.4</maven.compiler.source>
        <maven.compiler.target>9.0.4</maven.compiler.target>
        <jdk.version>9.0.4</jdk.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-protobuf</artifactId>
            <version>1.32.1</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-stub</artifactId>
            <version>1.32.1</version>
        </dependency>
        <dependency> <!-- necessary for Java 9+ -->
            <groupId>org.apache.tomcat</groupId>
            <artifactId>annotations-api</artifactId>
            <version>6.0.53</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.6.2</version>
            </extension>
        </extensions>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>0.5.1</version>
                <configuration>
                    <!--suppress UnresolvedMavenProperty -->
                    <protocArtifact>
                        com.google.protobuf:protoc:3.6.1:exe:${os.detected.classifier}
                    </protocArtifact>
                    <pluginId>grpc-java</pluginId>
                    <!--suppress UnresolvedMavenProperty -->
                    <pluginArtifact>
                        io.grpc:protoc-gen-grpc-java:1.22.1:exe:${os.detected.classifier}
                    </pluginArtifact>
                    <protoSourceRoot>
                        ${basedir}/src/main/proto/
                    </protoSourceRoot>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>compile-custom</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

 

02.approval.proto

syntax = "proto3";

package payment;

option java_multiple_files = true;
option java_package = "com.roopy.payment.approval";

message ApprovalRequest {
  string transaction_id = 1;
  int32 amt = 2;
}

message ApprovalResponse {
  string transaction_id = 1;
  string aproval_no = 2;
  int32 amt = 3;
}

service ApprovalService {
  // 결제승인
  rpc approvalPayment(stream ApprovalRequest) returns (stream ApprovalResponse) {};
}

- ApprovalRequest   : input parameter

- ApprovalResponse : output parameter

- ApprovalService    : 서비스 메소드 정의

 

03.코드생성

 

compile 실행

proto 파일에 대한 코드생성

생성코드위치

특별하게 output 폴더를 지정하지 않았기 때문에 target 폴더 아래로 소스코드가 생성된다.

 

04.Content Root 추가

생성된 소스 코드를 공유하기 위해 File > Project Structure... 선택을 한다.

설정 화면에서 proto module을 선택한다.

화면에서 + Add Content Root 에서 생성코드위치 그림에서 보면 target > generated-sources > protobuf 밑의 두개의 폴더를 추가해준다.

 

관련사이트

https://developers.google.com/protocol-buffers/docs/javatutorial

 

Protocol Buffer Basics: Java  |  Protocol Buffers  |  Google Developers

This tutorial provides a basic Java programmer's introduction to working with protocol buffers. By walking through creating a simple example application, it shows you how to Define message formats in a .proto file. Use the protocol buffer compiler. Use the

developers.google.com

 

www.vinsguru.com/grpc-introduction-guide/

 

gRPC - An Introduction Guide | Vinsguru

This tutorial introduces you to the modern RPC framework from google called gRPC. It is one of the hottest technologies right now!

www.vinsguru.com

 

github.com/roopy1210/spring-msa-with-grpc/tree/master/proto

 

roopy1210/spring-msa-with-grpc

Contribute to roopy1210/spring-msa-with-grpc development by creating an account on GitHub.

github.com

 

반응형

'Spring Micro Services > gRPC' 카테고리의 다른 글

gRPC VS REST 성능 테스트II  (0) 2021.05.20
gRPC VS REST 성능 테스트I  (0) 2021.04.15
Spring Micro Service with gRPC 프로젝트 설정  (0) 2021.04.13