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打包,编译、实际业务代码,源码等细节。

相关推荐
Java编程爱好者23 分钟前
1-5 线程池:Thread+阻塞队列+循环
后端
jnrjian25 分钟前
Library Cache Load Lock library cache pins are replaced by mutexes
java·后端·spring
用户94161469336535 分钟前
Python 批量获取 A 股全市场 K 线数据并计算技术指标(附完整代码)
后端
小江的记录本1 小时前
【Kafka核心】Kafka高性能的四大核心支柱:零拷贝、批量发送、页缓存、压缩
java·数据库·分布式·后端·缓存·kafka·rabbitmq
SamDeepThinking1 小时前
程序员过35岁之前,应该完成的三件事
java·后端·程序员
952361 小时前
SpringAOP
java·后端·学习·spring
zx2859634002 小时前
Laravel6.x新特性全解析
java·后端·spring
Jul1en_2 小时前
Claude 迁移 Codex 工作流迁移与更新
java·服务器·前端·后端·ai编程
神奇小汤圆2 小时前
京东二面:假如SQL中join了10张表,如何优化性能?
后端
神奇小汤圆3 小时前
Spring AOP底层黑科技:巧妙破解微服务异步线程池导致事务与链路上下文丢失难题
后端