java 不可用:网络因未知原因关闭

polhcujo  于 2023-03-28  发布在  Java
关注(0)|答案(1)|浏览(185)

我遇到了一个问题。我想在客户端接收一个http get请求,grpc将向服务器发送一条消息,在那里它将被记录并显示在控制台中,然而,当我收到一个get请求时,我得到了这个:io.grpc.StatusRuntimeException:不可用:网络因未知原因关闭
在客户端和服务器中,我有一个proto文件夹,在proto文件中它说:

syntax = "proto3";
package com.example;
option java_multiple_files = true;
message Hello{
  string message = 1;
}
message Greet{
  string message = 1;
}
service Greeting {
  rpc greeting(Hello) returns (Greet){}
}

现在我在服务中组织了服务器,如下所示:

@Service
@Slf4j
public class GreetingService extends GreetingGrpc.GreetingImplBase {

    @Override
    public void greeting(Hello request, StreamObserver<Greet> responseObserver) {
      log.info("I receive message : " + request);

      Greet grret = Greet.newBuilder()
                      .setMessage("Hello to you too!")
                              .build();
      responseObserver.onNext(grret);
      responseObserver.onCompleted();
    }
}

在我的理解,我们写的回报,为事件:greeting.现在,我想在这里发送一个请求,为此我在客户端编写了一个控制器:

@RestController
@RequestMapping("/api")
public class GreetingController {

    @GetMapping
    public void greeting(){
        ManagedChannel channel = ManagedChannelBuilder
                .forAddress("localhost", 9000)
                .usePlaintext().build();

        GreetingGrpc.GreetingBlockingStub stub = GreetingGrpc.newBlockingStub(channel);
        Greet greet = stub.greeting(Hello.newBuilder()
                        .setMessage("Hello, World!")
                .build());
        channel.shutdown();
    }

}

但不幸的是,当我向localhost:9010(我的客户端)发送GET请求时,我得到了一个错误。我真的希望得到你的帮助。
我的pom.xml在两个模块中(服务器,客户端)

<?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.0.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>server</name>
    <description>server</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-netty</artifactId>
            <version>1.16.1</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-protobuf</artifactId>
            <version>1.16.1</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-stub</artifactId>
            <version>1.16.1</version>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>

    <build>
        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.6.1</version>
            </extension>
        </extensions>
        <plugins>
            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>0.6.1</version>
                <configuration>
                    <protocArtifact>
                        com.google.protobuf:protoc:3.3.0:exe:${os.detected.classifier}
                    </protocArtifact>
                    <pluginId>grpc-java</pluginId>
                    <pluginArtifact>
                        io.grpc:protoc-gen-grpc-java:1.4.0:exe:${os.detected.classifier}
                    </pluginArtifact>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>compile-custom</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
7kqas0il

7kqas0il1#

我的解决方案:我没有gRpc服务器,我试图捕获与Spring服务器的连接,结果我需要编写它:

public class GrpcRunner {
    public static void main(String[] args) throws IOException, InterruptedException {
        Server server = ServerBuilder
                .forPort(9020)
                .addService(new GrpcServer()).build();

        System.out.println("мы запускаемся");
        server.start();
        System.out.println("мы запустились");
        server.awaitTermination();
    }
}

似乎存在一个依赖项,名为:grpc-spring-boot-starter它将允许您使用spring运行gRpc服务器,但不幸的是,它不是为我安装的。

相关问题