简体   繁体   English

如何在 Python 中为使用 Java 编写的 gRPC 服务编写 gRPC 客户端

[英]How to write a gRPC client in Python for a gRPC service written in Java

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. Python 中的大多数教程都在定义方法的地方获取主要的 Class 名称,当您只需要编写客户端时不知道该怎么做。

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.我怀疑您是初学者或中级程序员,所以如果您的任务是或尝试像编写自己的gRPC 客户端这样复杂的事情,我会感到非常惊讶。 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.我的猜测是,您实际上只是想从 Java gRPC 服务读取数据,并且您已经阅读了有关使用 Python gRPC 客户端的信息,但您对如何执行此操作感到困惑。

Simplified example of a client.客户端的简化示例。

First, let's talk about how a client might work.首先,让我们谈谈客户端的工作方式。 Imagine writing a Python class like想象一下写一个 Python class 之类的

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.然后在您的主 function 中,您将创建此客户端的一个实例,使用它来连接和读取(或写入)消息。 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.因此,客户端只是一个 object,它完成了连接到某个地方并为您从线路上读取消息的肮脏工作。

Now, the above is a poor example, because that's not really how gRPC works.现在,上面是一个糟糕的例子,因为 gRPC 并不是这样工作的。 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.现在您可以看到主 function 和通用客户端之间的连接。

So, how does gRPC fit into this?那么,gRPC 是如何融入其中的呢?

The whole idea behind gRPC is that you don't have to write a client: Look at it in steps like this: gRPC 背后的整个想法是您不必编写客户端:按如下步骤查看它:

  1. Someone began by writing a.proto file that "defined" their service.有人首先编写了一个“定义”他们的服务的 .proto 文件。 They defined what kinds of endpoints this service would have, eg getThings() , doThat() .他们定义了该服务将具有哪些类型的端点,例如getThings()doThat()

  2. Then they used a program or plugin (grpc-java) to generate a mostly -finished Java application.然后他们使用程序或插件 (grpc-java)生成了一个基本完成的 Java 应用程序。 The unfinished parts being: exactly what getThings() and doThat() does.未完成的部分是: getThings()doThat()的作用。

  3. They filled in that code... and done, grpc-java generated the whole server code for them.他们填写了该代码......完成后,grpc-java 为他们生成了整个服务器代码。 so their service is already up and running.所以他们的服务已经启动并运行。

  4. Now you want to read from this service, but in Python.现在想从这个服务中读取,但是在 Python 中。

  5. 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 .只需要获取他们用于生成 Java 服务的相同 .proto 文件,然后按照以下步骤生成客户端 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.所以你看,正如他们使用程序生成大部分 Java 服务一样,你也将使用程序为该服务生成客户端。 (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
  6. 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!在您的主要 function 中,您只需创建该生成客户端的实例(指定 IP 地址和端口到 Java 服务),然后您就可以了!

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

A summary, and one way of looking at it, though not exact:)一个总结,一种看待它的方式,虽然不准确:)

Think of gRPC as a suite of programs written by https://grpc.io/ that generates code for everyone.将 gRPC 视为由https://grpc.io/编写的一套程序,可为每个人生成代码。

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.有人在 a.proto 文件中定义了一项服务,然后使用其中一个 gRPC 程序(在您的情况下为 grpc-java)来生成大部分服务器。 Then they implemented the rest and deployed that server.然后他们实施了 rest 并部署了该服务器。

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.现在,像您这样的任何人都可以通过另一个gRPC 程序(在您的情况下为 grpc-python)提供相同的.proto 文件,以使用您选择的语言生成功能齐全的客户端。 You load up this generated client in your main function and you're ready to interact with the server on the other side.你在你的主 function 中加载这个生成的客户端,你就可以与另一端的服务器交互了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM