gpt4 book ai didi

java - 连接被拒绝 : localhost/0:0:0:0:0:0:0:1:9000 with gRPC

转载 作者:行者123 更新时间:2023-12-02 10:09:05 26 4
gpt4 key购买 nike

我创建了一个 gRPC 服务器,但一切似乎都运行正常,但服务器从未在指定端口上启动,并且应用程序没有抛出任何错误。但是当我在该特定端口上使用 telnet 进行测试时,我从终端得到这个

isaack$ telnet localhost 9000
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
telnet: Unable to connect to remote host

下面是我创建服务器的代码(注意:所有服务都可以使用原型(prototype)生成,并且生成的代码没有错误)

import java.io.File;

import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.ServerInterceptors;
import io.grpc.ServerServiceDefinition;

public class EmployeeServiceServer {

private Server server;

public static void main(String[] args) {
try {
EmployeeServiceServer service = new EmployeeServiceServer();
service.start();
} catch (Exception e) {
System.err.println(e);
}
}

private void start() throws InterruptedException {
File certificate = new File("/Users/i/certificates/cert.pem");
File key = new File("/Users/i/certificates/key.pem");
final int port = 9000;
EmployeeService employeeService = new EmployeeService();
ServerServiceDefinition serverServiceDefinition = ServerInterceptors.interceptForward(employeeService,
new HeaderServerInterceptor());
server = ServerBuilder.forPort(port).useTransportSecurity(certificate, key).addService(serverServiceDefinition)
.build();

System.out.println("Listening on Port " + port);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
System.out.println("Shuttin Down Server");
EmployeeServiceServer.this.stop();
}

});

server.awaitTermination();

}

private void stop() {
if (server != null) {
server.isShutdown();
}
}

}

下面是日志,但是当我 ping 它时,我什么也没得到。

Listening on Port 9000

我的客户也抛出此错误:

Exception in thread "main" io.grpc.StatusRuntimeException: UNAVAILABLE: io exception
at io.grpc.stub.ClientCalls.toStatusRuntimeException(ClientCalls.java:233)
at io.grpc.stub.ClientCalls.getUnchecked(ClientCalls.java:214)
at io.grpc.stub.ClientCalls.blockingUnaryCall(ClientCalls.java:139)
at com.base.services.EmployeeServiceGrpc$EmployeeServiceBlockingStub.getBadgebyNumber(EmployeeServiceGrpc.java:373)
at com.base.client.Client.sendMetaData(Client.java:66)
at com.base.client.Client.main(Client.java:37)
Caused by: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: localhost/0:0:0:0:0:0:0:1:9000

最佳答案

您可能想要start()您的服务器,如build建议:

Builds a server using the given parameters.
The returned service will not been started or be bound a port. You will need to start it with Server.start().

也许server.awaitTermination();行可以变成

server.start().awaitTermination();

虽然我不完全确定。

关于java - 连接被拒绝 : localhost/0:0:0:0:0:0:0:1:9000 with gRPC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55122154/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com