如何在Python中为用Java编写的gRPC服务编写gRPC客户端 [英] How to write a gRPC client in Python for a gRPC service written in Java

查看:97
本文介绍了如何在Python中为用Java编写的gRPC服务编写gRPC客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道该怎么办,因此无法继续前进.Python中的大多数教程都在定义方法的主类名中获取信息,不确定只在编写客户端时该如何做.

I am not able to move forward with this confusion as I don't know what to do. Most of the tutorials in Python get the main Class name where a method is defined, not sure how to do it when you just have to write the client.

推荐答案

我怀疑您是初学者还是中级程序员,所以如果您要承担编写类似 own gRPC客户端.我的猜测是,您实际上只想从Java gRPC服务中读取数据,并且已经阅读了有关使用Python gRPC客户端的信息,但是对于如何做到这一点感到困惑.

I suspect you're a beginner or intermediate programmer, so I'd be very surprised if you're tasked with or attempting something as complex as writing your own gRPC client. My guess is that you actually just want to read data from a Java gRPC service, and you've read about using a Python gRPC client, but you're confused about how to do that.

首先,让我们谈谈 client 的工作方式.试想像写一个像这样的Python类

First, let's talk about how a client might work. Imagine writing a Python class like

class MyClient:
    def __init__(self, address, port):
        self.address = address
        self.port = port
        // create a grpc channel

    def connect(self):
        // connect the channel

    def read(self):
        // read the channel

然后,在您的主要功能中,您将创建此客户端的实例,并使用该实例连接和读取(或写入)消息.因此,客户端只是一个繁琐的工作,它可以连接到某个地方并为您离线读取消息.

Then in your main function you would create an instance of this client, using it to connect and read (or write) messages. So a client is just an object that does the dirty work of connecting to some place and reading messages off the wire for you.

现在,上面的例子是一个糟糕的例子,因为那不是gRPC的真正工作方式.但是我认为一个更传统的即读即读示例将更易于理解.现在,您可以看到主要功能与通用客户端之间的连接.

Now, the above is a poor example, because that's not really how gRPC works. But I figured a more traditional connect-and-read example would be easier to understand. And now you see the connection between your main function and a generic client.

gRPC背后的全部想法是,您不必必须编写客户端!按如下步骤查看它:

The whole idea behind gRPC is that you don't have to write a client! Look at it in steps like this:

  1. 有人开始编写一个"proto"文件来定义"他们的服务.他们定义了该服务将具有哪些端点,例如 getThings() doThat().

然后他们使用程序或插件(grpc-java)来生成一个主要是完成的Java应用程序.未完成的部分是: getThings() doThat()的确切作用.

Then they used a program or plugin (grpc-java) to generate a mostly-finished Java application. The unfinished parts being: exactly what getThings() and doThat() does.

他们填写了该代码...并完成了!grpc-java为他们生成了整个服务器代码,因此它们的服务已经启动并正在运行.

They filled in that code... and done! grpc-java generated the whole server code for them, so their service is already up and running.

现在想从此服务中读取内容,但使用Python.

Now you want to read from this service, but in Python.

只需获取与用于生成Java服务的相同的.proto文件,然后按照

You just need to grab that same .proto file that they used to generate the Java service, and follow these steps to generate a client. So you see, just as they used a program to generate most of their Java service, you're also going to use a program to generate a client for that service! (And not even "most." It generates a fully functioning client.)

python -m grpc_tools.protoc --python_out=. --grpc_python_out=. your_java_service_proto_file.proto

  • 在您的主要功能中,您只需创建该生成的客户端的实例(指定Java服务的IP地址和端口),就可以了!

  • In your main function, you just create an instance of that generated client (specifying an IP address and port to the Java service), and you're good to go!

    channel = grpc.insecure_channel('10.123.123.123:54321')
    stub = the_generated_thing.RouteGuideStub(channel)
    things_from_java_service = stub.getThings()
    

  • 一个摘要,以及一种查看方式,尽管不完全准确:)

    将gRPC视为由 https://grpc.io/会为每个人生成代码.

    有人在.proto文件中定义了一项服务,然后使用这些gRPC程序(在您的情况下为grpc-java)之一生成服务器的 most .然后他们实施了其余的工作,并部署了该服务器.

    Someone defined a service in a .proto file then used one of those gRPC programs (grpc-java in your case) to generate most of a server. Then they implemented the rest and deployed that server.

    现在,像您这样的任何人都可以通过另一个 gRPC程序(在您的情况下为grpc-python)提供相同的.proto文件,从而生成一种功能全面的客户端你的选择.您可以在主要功能中加载此生成的客户端,并准备与另一端的服务器进行交互.

    Now, anyone like yourself can feed that same .proto file through another gRPC program (grpc-python in your case) to generate a fully functioning client in a language of your choice. You load up this generated client in your main function and you're ready to interact with the server on the other side.

    这篇关于如何在Python中为用Java编写的gRPC服务编写gRPC客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆