Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the gRPC tool

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Today I will show you how to use the gRPC tool. The content of the article is good. Now I would like to share it with you. Friends who feel in need can understand it. I hope it will be helpful to you. Let's read it along with the editor's ideas.

Recently, when I was writing an automated test script, I found that it was a problem to get the return value when I executed it with am from an Android device. I couldn't get this value, it was all a pile of TestRunner information. How to solve the problem? Then see if gRPC has the ability to handle it, and charge yourself with 5.2mA by the way.

What is gRPC?

Cpact S model, produced by google, forced GE Gao! Nonsense, show me the code! No, it should be a model diagram.

Insert a picture here to describe the usage scene

I found an article in the brief book, which I briefly summarized, that is, there are high requirements for security and performance.

When we need to strictly restrict the interface, for example, we provide a public service, and many people, even people outside the company, can access this service. Then we want to have more stringent restrictions on the interface. We do not want the client to pass arbitrary data to us, especially considering the security factors, we usually need to impose more stringent constraints on the interface. At this point, gRPC can provide strict interface constraints through protobuf.

When there are higher requirements for performance. Sometimes our services need to transfer a large amount of data, but do not want to affect our performance, we can also consider gRPC services, because through protobuf we can convert data compression into binary format, usually the amount of data transferred is much smaller, and through http2 we can achieve asynchronous requests, thus greatly improving the efficiency of communication.

However, usually we do not use gRPC alone, but use gRPC as a component, because in the production environment, we need to use a distributed system to deal with large concurrency, while gRPC does not provide some necessary components related to the distributed system. Moreover, real online services also need to provide necessary components, including load balancing, current-limiting circuit breakers, monitoring alarms, service registration and discovery, and so on. However, this is not the topic of this article, so let's move on to how to use gRPC.

ShowMeTheCode

There is a very comprehensive step-by-step doc, including installation and everything, so it is very comprehensive.

Https://grpc.io/docs/quickstart/python/

If it's just for demonstration and understanding. You can poke the link directly and look at the code.

This is the server code.

Https://github.com/grpc/grpc/blob/master/examples/python/helloworld/greeter_server.py

From concurrent import futures

Import logging

Import grpc

Import helloworld_pb2

Import helloworld_pb2_grpc

Class Greeter (helloworld_pb2_grpc.GreeterServicer):

Def SayHello (self, request, context):

Return helloworld_pb2.HelloReply (message='Hello,% slots'% request.name)

Def serve ():

Server = grpc.server (futures.ThreadPoolExecutor (max_workers=10))

Helloworld_pb2_grpc.add_GreeterServicer_to_server (Greeter (), server)

Server.add_insecure_port ('[:]: 50051')

Server.start ()

Server.wait_for_termination ()

If _ _ name__ = ='_ _ main__':

Logging.basicConfig ()

Serve ()

This is the client code.

From _ _ future__ import print_function

Import logging

Import grpc

Import helloworld_pb2

Import helloworld_pb2_grpc

Def run ():

# NOTE (gRPC Python Team): .close () is possible on a channel and should be

# used in circumstances in which the with statement does not fit the needs

# of the code.

With grpc.insecure_channel ('localhost:50051') as channel:

Stub = helloworld_pb2_grpc.GreeterStub (channel)

Response = stub.SayHello (helloworld_pb2.HelloRequest (name='you'))

Print ("Greeter client received:" + response.message)

If _ _ name__ = ='_ _ main__':

Logging.basicConfig ()

Run ()

These are all the contents of how to use the gRPC tool. For more information about how to use the gRPC tool, you can search the previous articles or browse the following articles to learn! I believe the editor will add more knowledge to you. I hope you can support it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report