spring boot 整合 Grpc 說明

<!-- 用到的 maven 依賴 -->
<dependency>
    <groupId>com.google.protobuf</groupId>
	<artifactId>protobuf-java</artifactId>
	<version>3.5.1</version>
</dependency>

<dependency>
	<groupId>io.grpc</groupId>
	<artifactId>grpc-all</artifactId>
	<version>1.21.0</version>
</dependency>

<!-- grpc 構建成服務的依賴 -->
<dependency>
    <groupId>net.devh</groupId>
    <artifactId>grpc-client-spring-boot-starter</artifactId>
    <version>2.1.0.RELEASE</version>
    <type>pom</type>
</dependency>
<dependency>
    <groupId>net.devh</groupId>
    <artifactId>grpc-server-spring-boot-starter</artifactId>
    <version>2.1.0.RELEASE</version>
    <type>pom</type>
</dependency>

<!-- 須要修改構件方式,用來支持 Grpc 生成服務代碼 -->
<build>
	<extensions>
		<extension>
			<groupId>kr.motd.maven</groupId>
			<artifactId>os-maven-plugin</artifactId>
			<version>1.5.0.Final</version>
		</extension>
	</extensions>
	<plugins>
		<plugin>
			<groupId>org.xolstice.maven.plugins</groupId>
			<artifactId>protobuf-maven-plugin</artifactId>
			<version>0.5.1</version>
			<configuration>
				<protocArtifact>com.google.protobuf:protoc:3.5.1-1:exe:${os.detected.classifier}</protocArtifact>
				<pluginId>grpc-java</pluginId>
				<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.17.1:exe:${os.detected.classifier}</pluginArtifact>
			</configuration>
			<executions>
				<execution>
					<goals>
						<goal>compile</goal>
						<goal>compile-custom</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>

grpc 官網提供的maven/gradle生成代碼的實例  https://www.grpc.io/docs/reference/java/generated-code/ 網址最下方java

建立 proto 包放置 .proto文件 ,如何構件參考官網 :https://developers.google.cn/protocol-buffers/spring

proto 文件的位置與 java / resources 同級maven

建立好以後運行 mvn compile 命令,或者使用 idea 提供的運行工具ide

運行 打包若是沒有問題 能夠在 target 裏面找到咱們生成的 .class 文件spring-boot

相關文章
相關標籤/搜索