一切顺势而行 2025-07-07 17:57 采纳率: 75%
浏览 14

springboot 整合grpc报错

springboot 整合grpc报错
pom 文件如下

<build>
        <plugins>
            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>0.5.0</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:${grpc.version}:exe:${os.detected.classifier}
                    </pluginArtifact>
                    <!--默认值-->
                    <protoSourceRoot>${project.basedir}/src/main/proto</protoSourceRoot>
                    <outputDirectory>${project.basedir}/src/main/java</outputDirectory>
                    <clearOutputDirectory>false</clearOutputDirectory>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>compile-custom</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

img

  • 写回答

8条回答 默认 最新

  • 阿里嘎多学长 2025-07-07 17:57
    关注

    阿里嘎多学长整理AIGC生成,因移动端显示问题导致当前答案未能完全显示,请使用PC端查看更加详细的解答过程

    Spring Boot 整合 GRPC 报错

    根据你提供的 pom 文件,可能是 GRPC 的依赖项没有正确配置导致的报错。下面是一些可能的解决方案:

    1. 检查 GRPC 的依赖项是否正确
    <dependencies>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-stub</artifactId>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-protobuf</artifactId>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-netty</artifactId>
        </dependency>
    </dependencies>
    
    1. 检查 Spring Boot 的版本是否支持 GRPC Spring Boot 2.3 及更高版本支持 GRPC,低版本可能需要使用插件来支持 GRPC。

    2. 检查 GRPC 服务端和客户端的配置是否正确 服务端:

    @GRpcService
    public class GreeterImpl extends Greeter {
        @Override
        public void sayHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
            HelloResponse response = HelloResponse.newBuilder().setMessage("Hello " + request.getName()).build();
            responseObserver.onNext(response);
            responseObserver.onCompleted();
        }
    }
    

    客户端:

    public class GreeterClient {
        public static void main(String[] args) {
            ManagedChannel channel = ManagedChannelBuilder.forTarget("localhost:50051").usePlaintext().build();
            GreeterGrpc.GreeterBlockingStub blockingStub = GreeterGrpc.newBlockingStub(channel);
            HelloRequest request = HelloRequest.newBuilder().setName("John").build();
            HelloResponse response = blockingStub.sayHello(request);
            System.out.println(response.getMessage());
        }
    }
    

    如果你已经检查了这些配置,仍然报错,可以提供更多的错误信息和代码,以便更好地帮助你解决问题。

    评论

报告相同问题?

问题事件

  • 创建了问题 7月7日