gRPC springBoot 项目搭建

源码地址

github.com/Fotile/foti...

client包

因为client包一般都需要给多个工程共享,所以最好独立一个项目/工程。这采用的是独立一个工程。

同时在pom.xml文件里 的 build设置参数上需要注意使用:

xml 复制代码
<configuration>
    <classifier>proto</classifier>  <!-- point:mark as proto source package -->
    <includes>
        <include>**/*.proto</include>  <!-- only include proto files -->
    </includes>
    <!-- exclude other files ,like java class files -->
    <excludes>
        <exclude>**/*.class</exclude>
        <exclude>META-INF/maven/**</exclude>
    </excludes>
</configuration>

完成后,使用命令打包并放到本地maven仓库 mvn clean install

server 部分

server端,如果需要引用client,需要注意client需要声明classifier为proto 如图:

完整pom依赖:

xml 复制代码
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>net.devh</groupId>
        <artifactId>grpc-server-spring-boot-starter</artifactId>
    </dependency>

    <!-- Protobuf 核心依赖 -->
    <dependency>
        <groupId>com.google.protobuf</groupId>
        <artifactId>protobuf-java</artifactId>
    </dependency>
    <!-- gRPC 依赖 -->
    <dependency>
        <groupId>io.grpc</groupId>
        <artifactId>grpc-stub</artifactId>
    </dependency>
    <dependency>
        <groupId>io.grpc</groupId>
        <artifactId>grpc-protobuf</artifactId>
    </dependency>

    <!-- 共享 proto 依赖 -->
    <dependency>
        <groupId>com.fotile.grpc</groupId>
        <artifactId>fotile-grpc-client</artifactId>
        <classifier>proto</classifier>
        <scope>provided</scope>
    </dependency>
</dependencies>

在构建模块时,需要注意,通过jar引入的共享proto,需要通过下列插件先解压

xml 复制代码
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>

然后对解压的内容进行编译

xml 复制代码
<plugin>
    <groupId>org.xolstice.maven.plugins</groupId>
    <artifactId>protobuf-maven-plugin</artifactId>
</plugin>   

最后把编译后的java代码加入到编译的路径里

xml 复制代码
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
</plugin>    

关于具体对于解压的proto文件怎么配置编译的,我们后面单独讲解。

具体server的具体java代码,其实就是一个普通的springboot应用,对于client实现,只需要在这个service类上注解@GrpcService,同时继承 *Grpc.*ImplBase ,如下面代码

scala 复制代码
@GrpcService
public class GreeterServiceGrpcImpl extends GreeterServiceGrpc.GreeterServiceImplBase {
}

一般来说,.proto 文件都是在 src/main/proto文件夹下, 参考我们的java文件都是在 src/main/java目录下。

最后需要注意的是需要配置下grpc服务端口:

yaml 复制代码
grpc:
  server:
    port: 9092

这个端口和一般的java应用端口(server.port)不一样

invoker 调用gRPC

因为这里为了测试gRPC的调用,暴露了Http接口,所以引用了

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

如果要自动装配gRPC client,直接引用

xml 复制代码
<dependency>
    <groupId>net.devh</groupId>
    <artifactId>grpc-client-spring-boot-starter</artifactId>
</dependency>

我们需要使用的业务client

xml 复制代码
<dependency>
    <groupId>com.fotile.grpc</groupId>
    <artifactId>fotile-grpc-client</artifactId>
    <classifier>proto</classifier>
    <scope>provided</scope>
</dependency>

然后build部分,因为要解压和把.proto文件编译成java代码,和server部分是类似的,只是没有自己的proto文件部分,所以少一些,完整代码可以拉取github里的代码工程。 需要注意,yml配置文件里需要配置client对应的server信息

yaml 复制代码
grpc.client:
  grpc-server:
    address: static://localhost:9092
    enableKeepAlive: true
    keepAliveWithoutCalls: true
    # 因为gRPC默认是走的https,所以这里为了能支持不用TLS,加上这个配置
    negotiationType: plaintext

这里的 grpc-server需要与代码里的

kotlin 复制代码
@GrpcClient("grpc-server")

注解里的value值对应上,不然会有问题。 注意,注入的client其实都是 *Grpc.*BlockingStub 这样的形式,剩下的就是正常的类方法调用。

总结

本文主要提供了一个gRPC的server和invoker应用的基本调用示范,并演示如何利用公用jar里包含.proto文件来实现公用gRPC接口的定义。

后续会有专门章节来讲述关于maven打包,编译、实际业务代码,源码等细节。

相关推荐
编程修仙26 分钟前
第九篇 Spring中的代理思想
java·后端·spring
aiopencode29 分钟前
iOS CPU 使用率深度分析,多工具协同定位高占用瓶颈的工程化方法
后端
I'm Jie38 分钟前
告别重复编码!SpringBoot 字段变更(新旧值)日志工具类的规范化设计与优雅实现
java·spring boot·后端
开心猴爷1 小时前
Bundle Id 创建与管理的工程化方法,一次团队多项目协作中的流程重构
后端
databook1 小时前
用样本猜总体的秘密武器,4大抽样分布总结
后端·python·数据分析
小坏讲微服务1 小时前
SpringBoot4.0整合Scala完整使用
java·开发语言·spring boot·后端·scala·mybatis
泉城老铁2 小时前
windows服务器mysql数据库备份脚本
java·后端·mysql
却尘2 小时前
用 25 个概念彻底看懂SQL多维分析的底层逻辑
后端·sql·mysql
白衣鸽子2 小时前
JsonUtils:打造企业级的序列化与反序列化瑞士军刀
后端·开源
计算机学姐2 小时前
基于Python的校园美食推荐系统【2026最新】
开发语言·vue.js·后端·python·mysql·django·推荐算法