You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Spring Boot项目中同时使用REST与gRPC?含pom配置及端口疑问

实现包含gRPC和REST端点的服务配置与多端口部署

一、pom.xml配置(基于Spring Boot)

如果采用Spring Boot框架开发,需要引入REST Web支持、gRPC相关依赖,以及Protobuf代码编译插件。以下是核心配置片段:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.0</version> <!-- 可替换为最新稳定版 -->
        <relativePath/>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>grpc-rest-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <java.version>17</java.version>
        <grpc-spring-boot-starter.version>3.2.0</grpc-spring-boot-starter.version>
        <protobuf.version>3.25.3</protobuf.version>
    </properties>

    <dependencies>
        <!-- REST服务核心依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- gRPC服务与客户端依赖 -->
        <dependency>
            <groupId>net.devh</groupId>
            <artifactId>grpc-spring-boot-starter</artifactId>
            <version>${grpc-spring-boot-starter.version}</version>
        </dependency>
        <dependency>
            <groupId>net.devh</groupId>
            <artifactId>grpc-spring-boot-starter-client</artifactId>
            <version>${grpc-spring-boot-starter.version}</version>
        </dependency>

        <!-- Protobuf基础依赖 -->
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>${protobuf.version}</version>
        </dependency>
    </dependencies>

    <build>
        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.7.1</version>
            </extension>
        </extensions>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <!-- Protobuf编译插件,自动生成gRPC Java代码 -->
            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>0.6.1</version>
                <configuration>
                    <protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}</protocArtifact>
                    <pluginId>grpc-java</pluginId>
                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.59.0:exe:${os.detected.classifier}</pluginId>
                    <protoSourceRoot>${project.basedir}/src/main/proto</protoSourceRoot>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>compile-custom</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

二、多端口(8080 REST + 9000 gRPC)实现

借助grpc-spring-boot-starter,gRPC服务端口与Spring Boot REST端口可独立配置,直接在配置文件中指定即可:

方式1:application.properties配置

# REST服务监听端口
server.port=8080

# gRPC服务监听端口
grpc.server.port=9000

方式2:application.yml配置

server:
  port: 8080

grpc:
  server:
    port: 9000

启动服务后,REST接口会绑定8080端口,gRPC服务会绑定9000端口,二者互不干扰,可分别对外提供服务。

验证方式

  • REST接口:通过浏览器或Postman访问http://localhost:8080/你的接口路径测试。
  • gRPC接口:使用grpcurl工具或自定义gRPC客户端连接localhost:9000,调用对应的服务方法验证。

内容的提问来源于stack exchange,提问作者Kakarot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 05:10:12