我的GRPC客户端无法连接到我在同一台计算机上托管的集装箱的GRPC服务器

发布于 2025-02-08 01:12:33 字数 2998 浏览 3 评论 0 原文

我正在实现我的GRPC服务器的GRPC客户端和服务器应用程序。这个想法是使用UNIX域插座在客户端和服务器之间进行通信。我的应用程序正常工作,直到我的服务器没有被容器化。

当我容忍GRPC服务器时,出现了问题。在这种情况下,我的客户端无法到达GRPC服务器。以下是我收到的错误消息。

   raise _InactiveRpcError(state)
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
        status = StatusCode.UNAVAILABLE
        details = "failed to connect to all addresses"
        debug_error_string = "{"created":"@1655378324.763238318","description":"Failed to pick subchannel","file":"src/core/ext/filters/client_channel/client_channel.cc","file_line":3217,"referenced_errors":[{"created":"@1655378324.763237121","description":"failed to connect to all addresses","file":"src/core/lib/transport/error_utils.cc","file_line":165,"grpc_status":14}]}"

我正在寻求一些帮助,以了解我在GRPC客户端或服务器代码中需要进行的修改以进行此工作。

GRPC客户端代码:

    import grpc
    import greeting_pb2 as pb2
    import greeting_pb2_grpc as pb2_grpc

class HelloWorldClient(object):
def __init__(self):
    self.host = 'localhost'
    self.server_port = 50051

    #self.channel = grpc.insecure_channel('{}:{}'.format(self.host, self.server_port), options=(('grpc.enable_http_proxy', 0),))
    #self.channel = grpc.insecure_channel('unix:///var/run/test.sock', options=(('grpc.enable_http_proxy', 0),))
    self.channel = grpc.insecure_channel('unix:///service/test.sock', options=(('grpc.enable_http_proxy', 0),))
    self.stub = pb2_grpc.GreeterStub(self.channel)

def get_url(self, message):
    message = pb2.Request(name=message)
    return self.stub.SayHello(message)

if __name__ == '__main__':
    client = HelloWorldClient()
    result = client.get_url(message="Billi")
    print(f'{result}')

GRPC服务器代码:

import grpc
from concurrent import futures
import time
import greeting_pb2_grpc as pb2_grpc
import greeting_pb2 as pb2


import sys
print("Python version")
print (sys.version)

class HelloWorldService(pb2_grpc.GreeterServicer):
    def __init__(self, *args, **kwargs):
        pass

    def SayHello(self, request, context):
        message = request.name
        print("Python version")
        result = f'Hello {message}! Glad to hear from you!'
        result = {'message':result}

        return pb2.Response(**result)

def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    pb2_grpc.add_GreeterServicer_to_server(HelloWorldService(), server)
    server.add_insecure_port('unix:///service//test.sock')
    print("Ready To Start")
    server.start()
    print("Server Started")
    server.wait_for_termination()


if __name__ == '__main__':
    serve()
    

客户端和服务器都在同一主机上运行,​​但是服务器在Docker容器中运行。我已经在主机和Docker容器中创建了一个名为“服务”的文件夹,并且

docker run -v /service/test.sock:/service/test.sock:rw grpc_sock_new

根据我的理解运行Docker容器时,我正在将此路径安装到套接字上,解决问题的关键是以下代码行。

server.add_insecure_port('unix:///service//test.sock')

请建议。

I am implementing a gRPC client and server application where my gRPC server is containerised. The idea is to use Unix domain sockets to communicate between the client and the server. My application was working fine until I had my server was not containerised.

The issue arises when I containerise my gRPC server. In this case, my client is unable to reach the gRPC server. The following is the error message that I received.

   raise _InactiveRpcError(state)
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
        status = StatusCode.UNAVAILABLE
        details = "failed to connect to all addresses"
        debug_error_string = "{"created":"@1655378324.763238318","description":"Failed to pick subchannel","file":"src/core/ext/filters/client_channel/client_channel.cc","file_line":3217,"referenced_errors":[{"created":"@1655378324.763237121","description":"failed to connect to all addresses","file":"src/core/lib/transport/error_utils.cc","file_line":165,"grpc_status":14}]}"

I am seeking some help to understand what the modifications I need in my gRPC client or server code to make this work.

gRPC Client Code:

    import grpc
    import greeting_pb2 as pb2
    import greeting_pb2_grpc as pb2_grpc

class HelloWorldClient(object):
def __init__(self):
    self.host = 'localhost'
    self.server_port = 50051

    #self.channel = grpc.insecure_channel('{}:{}'.format(self.host, self.server_port), options=(('grpc.enable_http_proxy', 0),))
    #self.channel = grpc.insecure_channel('unix:///var/run/test.sock', options=(('grpc.enable_http_proxy', 0),))
    self.channel = grpc.insecure_channel('unix:///service/test.sock', options=(('grpc.enable_http_proxy', 0),))
    self.stub = pb2_grpc.GreeterStub(self.channel)

def get_url(self, message):
    message = pb2.Request(name=message)
    return self.stub.SayHello(message)

if __name__ == '__main__':
    client = HelloWorldClient()
    result = client.get_url(message="Billi")
    print(f'{result}')

gRPC Server Code:

import grpc
from concurrent import futures
import time
import greeting_pb2_grpc as pb2_grpc
import greeting_pb2 as pb2


import sys
print("Python version")
print (sys.version)

class HelloWorldService(pb2_grpc.GreeterServicer):
    def __init__(self, *args, **kwargs):
        pass

    def SayHello(self, request, context):
        message = request.name
        print("Python version")
        result = f'Hello {message}! Glad to hear from you!'
        result = {'message':result}

        return pb2.Response(**result)

def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    pb2_grpc.add_GreeterServicer_to_server(HelloWorldService(), server)
    server.add_insecure_port('unix:///service//test.sock')
    print("Ready To Start")
    server.start()
    print("Server Started")
    server.wait_for_termination()


if __name__ == '__main__':
    serve()
    

The client and the server are both running on the same host, but the server is running inside a Docker container. I have created a folder named "service" in the host as well as the docker container and I am mounting this path to the socket when running the Docker container

docker run -v /service/test.sock:/service/test.sock:rw grpc_sock_new

As per my understanding, the key to fixing the issue is the following line of code.

server.add_insecure_port('unix:///service//test.sock')

Please advice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清醇 2025-02-15 01:12:33

您可能使用的斜线数量超过要求
在GRPC服务器代码中的此行中: server.add_insecure_port('unix:///service//test.sock')

尝试使用此改音: server.add_insecure_port('unix:// unix:// /service/test.sock')

编辑:1

  1. 客户端,您上面介绍的服务器代码在系统中在本地运行时应正常工作,因为两者都基本上在同一机器上运行可以轻松到达引用的套接字文件。
  2. 如果服务器和客户端都包装在一个容器中运行,则很有可能会工作,因为两者都可以访问容器范围本地的套接字。
  3. 如果您要在2个不同的容器中运行服务器和客户端 - &gt;试图参考客户端 /服务器的套接字是指将其各自的本地套接字示为容器环境,而不是远程容器中的套接字。< / strong>

您应该使用Docker Bind Mounts 在两个容器中在Docker主机中共享一个插座。请参阅 在容器之间共享主机插座。

ex: docker run -d -name“ server” -v/var/run/test.sock:/var/run/test.sock server_image:version

>

covureences:

  1. grpc中的名称分辨率。
  2. In-with-with-unix-domain-socket“> stackoverflow
  3. 在容器之间共享主机插座。
  4. docker bind onter官方文档

You may be using more number of slashes than required
in this line in gRPC Server Code: server.add_insecure_port('unix:///service//test.sock')

Try Using this instead : server.add_insecure_port('unix:///service/test.sock')

Edit : 1

  1. The client, server code you presented above should work fine when you run it locally in your system because both are essentially running on same machine and can reach to the referred socket file easily.
  2. If both the Server and client are packed to run in a single container, there is a good chance that it will work, as both can access a socket that is local to the container scope.
  3. If you are running the server and client in 2 different containers --> trying to refer to a socket from client / server refers to their respective local sockets scoped to container environment, but not to the socket in remote container.

You should share a socket in your docker host to both containers using docker bind mounts. Refer Sharing Host Sockets between containers.

Ex: docker run -d --name "server" -v /var/run/test.sock:/var/run/test.sock server_image:version

References :

  1. Name resolution in gRPC.
  2. This Answer in StackOverflow
  3. Sharing Host Sockets between containers.
  4. Docker Bind Mounts Official Documentation
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文