溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

gRPC HelloWorld測(cè)試

發(fā)布時(shí)間:2020-06-23 20:48:04 來源:網(wǎng)絡(luò) 閱讀:209637 作者:九月朦朧 欄目:編程語言

一、概述

What's gRPC?
gRPC is a modern open source high performance RPC framework that can run in any environment. It can efficiently connect services in and across data centers with pluggable support for load balancing, tracing, health checking and authentication. It is also applicable in last mile of distributed computing to connect devices, mobile applications and browsers to backend services.(gRPC是可以在任何環(huán)境中運(yùn)行的現(xiàn)代的開源高性能RPC框架。它可以通過可插拔的支持來有效地連接數(shù)據(jù)中心內(nèi)和跨數(shù)據(jù)中心的服務(wù),以實(shí)現(xiàn)負(fù)載平衡,跟蹤,運(yùn)行狀況檢查和身份驗(yàn)證。它也適用于分布式計(jì)算的最后一英里,以將設(shè)備,移動(dòng)應(yīng)用程序和瀏覽器連接到后端服務(wù)。)
我們可以用一句話來概括:A high-performance, open-source universal RPC framework
RPC(remote procedure call 遠(yuǎn)程過程調(diào)用)框架實(shí)際是提供了一套機(jī)制,使得應(yīng)用程序之間可以進(jìn)行通信,而且也遵從server/client模型。使用的時(shí)候客戶端調(diào)用server端提供的接口就像是調(diào)用本地的函數(shù)一樣。

so
在什么情況下需要使用gRPC呢?
需要對(duì)接口進(jìn)行嚴(yán)格約束的情況,比如我們提供了一個(gè)公共的服務(wù),很多人,甚至公司外部的人也可以訪問這個(gè)服務(wù),這時(shí)對(duì)于接口我們希望有更加嚴(yán)格的約束,我們不希望客戶端給我們傳遞任意的數(shù)據(jù),尤其是考慮到安全性的因素,我們通常需要對(duì)接口進(jìn)行更加嚴(yán)格的約束。這時(shí)gRPC就可以通過protobuf來提供嚴(yán)格的接口約束。
對(duì)于性能有更高的要求時(shí)。有時(shí)我們的服務(wù)需要傳遞大量的數(shù)據(jù),而又希望不影響我們的性能,這個(gè)時(shí)候也可以考慮gRPC服務(wù),因?yàn)橥ㄟ^protobuf我們可以將數(shù)據(jù)壓縮編碼轉(zhuǎn)化為二進(jìn)制格式,通常傳遞的數(shù)據(jù)量要小得多,而且通過http2我們可以實(shí)現(xiàn)異步的請(qǐng)求,從而大大提高了通信效率。
但是,通常我們不會(huì)去單獨(dú)使用gRPC,而是將gRPC作為一個(gè)部件進(jìn)行使用,這是因?yàn)樵谏a(chǎn)環(huán)境,我們面對(duì)大并發(fā)的情況下,需要使用分布式系統(tǒng)來去處理,而gRPC并沒有提供分布式系統(tǒng)相關(guān)的一些必要組件。而且,真正的線上服務(wù)還需要提供包括負(fù)載均衡,限流熔斷,監(jiān)控報(bào)jing,服務(wù)注冊(cè)和發(fā)現(xiàn)等等必要的組件。

二、測(cè)試

接下來開始gRPC的Hello World測(cè)試
gRPC的使用通常包括如下幾個(gè)步驟:
1、通過protobuf來定義接口和數(shù)據(jù)類型
2、編寫gRPC server端代碼
3、編寫gRPC client端代碼

1、通過protobuf來定義接口和數(shù)據(jù)類型

我實(shí)在mac中使用的python,mac中自帶python2,因?yàn)樾枰?,自己安裝了python3。
首先pip3 安裝grpc和protobuf

(lxc) liuxuchong:untitled liuxuchong$  pip3 install grpcio
Collecting grpcio
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out. (read timeout=15)")': /packages/0d/27/0413a5dffd7ddca4ea43cffd22f46ec2b26a5ed18c974e4448763e758a9b/grpcio-1.25.0-cp37-cp37m-macosx_10_9_x86_64.whl
  Downloading https://files.pythonhosted.org/packages/0d/27/0413a5dffd7ddca4ea43cffd22f46ec2b26a5ed18c974e4448763e758a9b/grpcio-1.25.0-cp37-cp37m-macosx_10_9_x86_64.whl (2.3MB)
    100% |████████████████████████████████| 2.3MB 8.1kB/s 
Collecting six>=1.5.2 (from grpcio)
  Using cached https://files.pythonhosted.org/packages/65/26/32b8464df2a97e6dd1b656ed26b2c194606c16fe163c695a992b36c11cdf/six-1.13.0-py2.py3-none-any.whl
Installing collected packages: six, grpcio
Successfully installed grpcio-1.25.0 six-1.13.0

(lxc) liuxuchong:untitled liuxuchong$  pip3 install protobuf
Collecting protobuf
  Downloading https://files.pythonhosted.org/packages/a5/c6/a8b6a74ab1e165f0aaa673a46f5c895af8780976880c98934ae82060356d/protobuf-3.10.0-cp37-cp37m-macosx_10_9_intel.whl (1.4MB)
    100% |████████████████████████████████| 1.4MB 83kB/s 
Requirement already satisfied: setuptools in ./venv/lxc/lib/python3.7/site-packages/setuptools-40.8.0-py3.7.egg (from protobuf) (40.8.0)
Requirement already satisfied: six>=1.9 in ./venv/lxc/lib/python3.7/site-packages (from protobuf) (1.13.0)
Installing collected packages: protobuf
Successfully installed protobuf-3.10.0

定義protobuf

下面定義一個(gè)簡(jiǎn)單的protobuf文件,在其中聲明一個(gè)grpc服務(wù)。
創(chuàng)建一個(gè)proto目錄,并在其中創(chuàng)建grpchello.proto文件,如下內(nèi)容。

syntax = "proto3";
package grpcDemo;

message HelloRequest {
   string name = 1;
}

message HelloReply {
   string message = 1;
}

service gRPC {
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

編譯protobuf

使用protobuf的編譯器,為我們生成python版本的Message定義和服務(wù)的架手腳。

lxc) liuxuchong:untitled liuxuchong$  python -m grpc-tools.protoc -I./proto --python_out=. --grpc_python_out=. grpchello.proto
/Users/liuxuchong/PycharmProjects/untitled/venv/lxc/bin/python: Error while finding module specification for 'grpc-tools.protoc' (ModuleNotFoundError: No module named 'grpc-tools')

提示我們沒有安裝grpc-tools,用pip安裝一下

python3 -m pip install --user grpcio-tools

然后在運(yùn)行上面的命令

在當(dāng)前目錄下,生成2個(gè)文件:

  • grpchello_pb2.py
  • grpchello_pb2_grpc.py

查看第一個(gè)文件,消息定義文件:

# -*- coding: utf-8 -*-
# Generated by the protocol buffer compiler.  DO NOT EDIT!
# source: grpchello.proto

import sys
_b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1'))
from google.protobuf import descriptor as _descriptor
from google.protobuf import message as _message
from google.protobuf import reflection as _reflection
from google.protobuf import symbol_database as _symbol_database
# @@protoc_insertion_point(imports)

_sym_db = _symbol_database.Default()

DESCRIPTOR = _descriptor.FileDescriptor(
  name='grpchello.proto',
  package='grpcDemo',
  syntax='proto3',
  serialized_options=None,
  serialized_pb=_b('\n\x0fgrpchello.proto\x12\x08grpcDemo\"\x1c\n\x0cHelloRequest\x12\x0c\n\x04name\x18\x01 \x01(\t\"\x1d\n\nHelloReply\x12\x0f\n\x07message\x18\x01 \x01(\t2B\n\x04gRPC\x12:\n\x08SayHello\x12\x16.grpcDemo.HelloRequest\x1a\x14.grpcDemo.HelloReply\"\x00\x62\x06proto3')
)

_HELLOREQUEST = _descriptor.Descriptor(
  name='HelloRequest',
  full_name='grpcDemo.HelloRequest',
  filename=None,
  file=DESCRIPTOR,
  containing_type=None,
  fields=[
    _descriptor.FieldDescriptor(
      name='name', full_name='grpcDemo.HelloRequest.name', index=0,
      number=1, type=9, cpp_type=9, label=1,
      has_default_value=False, default_value=_b("").decode('utf-8'),
      message_type=None, enum_type=None, containing_type=None,
      is_extension=False, extension_scope=None,
      serialized_options=None, file=DESCRIPTOR),
  ],
  extensions=[
  ],
  nested_types=[],
  enum_types=[
  ],
  serialized_options=None,
  is_extendable=False,
  syntax='proto3',
  extension_ranges=[],
  oneofs=[
  ],
  serialized_start=29,
  serialized_end=57,
)

然后看下grpc服務(wù)定義:

from google.protobuf import symbol_database as _symbol_database
# @@protoc_insertion_point(imports)

_sym_db = _symbol_database.Default()

DESCRIPTOR = _descriptor.FileDescriptor(
  name='grpchello.proto',
  package='grpcDemo',
  syntax='proto3',
  serialized_options=None,
  serialized_pb=_b('\n\x0fgrpchello.proto\x12\x08grpcDemo\"\x1c\n\x0cHelloRequest\x12\x0c\n\x04name\x18\x01 \x01(\t\"\x1d\n\nHelloReply\x12\x0f\n\x07message\x18\x01 \x01(\t2B\n\x04gRPC\x12:\n\x08SayHello\x12\x16.grpcDemo.HelloRequest\x1a\x14.grpcDemo.HelloReply\"\x00\x62\x06proto3')
)

_HELLOREQUEST = _descriptor.Descriptor(
  name='HelloRequest',
from google.protobuf import symbol_database as _symbol_database
# @@protoc_insertion_point(imports)

_sym_db = _symbol_database.Default()

DESCRIPTOR = _descriptor.FileDescriptor(
  name='grpchello.proto',
  package='grpcDemo',
  syntax='proto3',
  serialized_options=None,
  serialized_pb=_b('\n\x0fgrpchello.proto\x12\x08grpcDemo\"\x1c\n\x0cHelloRequest\\
x12\x0c\n\x04name\x18\x01 \x01(\t\"\x1d\n\nHelloReply\x12\x0f\n\x07message\x18\x011
 \x01(\t2B\n\x04gRPC\x12:\n\x08SayHello\x12\x16.grpcDemo.HelloRequest\x1a\x14.grpcc
Demo.HelloReply\"\x00\x62\x06proto3')
)

_HELLOREQUEST = _descriptor.Descriptor(
  name='HelloRequest',
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
import grpc

import grpchello_pb2 as grpchello__pb2

class gRPCStub(object):
  # missing associated documentation comment in .proto file
  pass

  def __init__(self, channel):
    """Constructor.

    Args:
      channel: A grpc.Channel.
    """
    self.SayHello = channel.unary_unary(
        '/grpcDemo.gRPC/SayHello',
        request_serializer=grpchello__pb2.HelloRequest.SerializeToString,
        response_deserializer=grpchello__pb2.HelloReply.FromString,
        )

class gRPCServicer(object):
  # missing associated documentation comment in .proto file
  pass

  def SayHello(self, request, context):
    # missing associated documentation comment in .proto file
    pass
    context.set_code(grpc.StatusCode.UNIMPLEMENTED)
    context.set_details('Method not implemented!')
    raise NotImplementedError('Method not implemented!')

def add_gRPCServicer_to_server(servicer, server):
  rpc_method_handlers = {
      'SayHello': grpc.unary_unary_rpc_method_handler(
          servicer.SayHello,
          request_deserializer=grpchello__pb2.HelloRequest.FromString,
          response_serializer=grpchello__pb2.HelloReply.SerializeToString,
      ),
  }
  generic_handler = grpc.method_handlers_generic_handler(
      'grpcDemo.gRPC', rpc_method_handlers)
  server.add_generic_rpc_handlers((generic_handler,))
  • 在grpc服務(wù)架手腳定義中,定義了gRPCStub,這是給client端使用,調(diào)用grpc服務(wù)的。
  • 定義的服務(wù)類gRPCServicer,方法SayHello需要我們?cè)谧宇愔羞M(jìn)行實(shí)現(xiàn)。定義的add_gRPCServicer_to_server方法,用于把實(shí)現(xiàn)的類和grpc API調(diào)用注冊(cè)起來。

這里使用的幾個(gè)主要方法(類):

  • grpc.server – Creates a Server with which RPCs can be serviced
  • grpc.method_handlers_generic_handler – Creates a GenericRpcHandler from RpcMethodHandlers.
  • grpc.unary_unary_rpc_method_handler – Creates an RpcMethodHandler for a unary-unary RPC method.

實(shí)現(xiàn)服務(wù)

在我們的實(shí)現(xiàn)服務(wù)的類中,使用服務(wù)方法,并在網(wǎng)絡(luò)中暴露出來。

# -*- coding: utf-8 -*-
import grpc
import time
from concurrent import futures 
import grpchello_pb2, grpchello_pb2_grpc

_HOST = 'localhost'
_PORT = '8188'

_ONE_DAY_IN_SECONDS = 60 * 60 * 24

class gRPCServicerImpl(grpchello_pb2_grpc.gRPCServicer):

    def SayHello(self, request, context):
        print ("called with " + request.name)
        return grpchello_pb2.HelloReply(message='Hello, %s!' % request.name)

def serve():
  server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
  grpchello_pb2_grpc.add_gRPCServicer_to_server(gRPCServicerImpl(), server)
  server.add_insecure_port('[::]:'+_PORT)
  server.start()
  try:
    while True:
      time.sleep(_ONE_DAY_IN_SECONDS)
  except KeyboardInterrupt:
    server.stop(0)

if __name__ == '__main__':
    serve()

這里包括2個(gè)實(shí)現(xiàn):

  • 1、在grpc的API的實(shí)現(xiàn)(服務(wù)實(shí)現(xiàn)類)gRPCServicerImpl中,實(shí)現(xiàn)SayHello方法。
  • 2、然后,定義網(wǎng)絡(luò)服務(wù)和端口,把grpc的API注冊(cè)到網(wǎng)絡(luò)服務(wù)的處理上。這里簡(jiǎn)單利用了grpc.server類。

使用客戶端client

在客戶端,調(diào)用grpc的服務(wù)API。

# -*- coding: utf-8 -*-
"""The Python implementation of the gRPC client."""
from __future__ import print_function
import grpc
from grpchello_pb2  import *    ## or import grpchello_pb2
from grpchello_pb2_grpc import *
## No grpcDemo!  from grpcDemo import grpchello_pb2, grpchello_pb2_grpc #error!

_PORT = '8188'

def run():
    conn = grpc.insecure_channel(_HOST + ':' + _PORT)
    client = gRPCStub(channel=conn)
    response = client.SayHello(HelloRequest(name='lxc'))
    print("received: " + response.message)

## 
if __name__ == '__main__':

    if len(sys.argv)== 2:
        print (sys.argv[1])
        _HOST = sys.argv[1]
    else:
        _HOST = 'localhost'

    #    
    run()

說明:

  • 1、 def insecure_channel(target, options=None):
    – Creates an insecure Channel to a server.
  • 2、 客戶端使用服務(wù)的Stub,調(diào)用API。

測(cè)試

分別啟動(dòng)服務(wù),然后再啟動(dòng)客戶端,可以看到調(diào)用結(jié)果。
也可以啟動(dòng)java、c#版本的grpc服務(wù)端、客戶端,都能調(diào)用成功。

然后運(yùn)行

(lxc) liuxuchong:untitled liuxuchong$  python3 grpchello_pb2.py
Traceback (most recent call last):
  File "grpchello_pb2.py", line 7, in <module>
    from google.protobuf import descriptor as _descriptor
ModuleNotFoundError: No module named 'google'

提示沒有g(shù)oogle模版
網(wǎng)上查了一下解決方法如下

pip3 install google
pip3 install protobuf

然后運(yùn)行另一個(gè)py文件

python3 grpchello_pb2_grpc.py
Traceback (most recent call last):
  File "grpchello_pb2_grpc.py", line 2, in <module>
    import grpc

奇怪,剛才明明裝了,還提示沒有模版,又裝了一次

pip3 install grpcio
Collecting grpcio
  Using cached https://files.pythonhosted.org/packages/0d/27/0413a5dffd7ddca4ea43cffd22f46ec2b26a5ed18c974e4448763e758a9b/grpcio-1.25.0-cp37-cp37m-macosx_10_9_x86_64.whl
Requirement already satisfied: six>=1.5.2 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from grpcio) (1.13.0)
Installing collected packages: grpcio
Successfully installed grpcio-1.25.0

果然剛才裝的丟了。。。
然后運(yùn)行python3 get_service.py

gRPC HelloWorld測(cè)試
打開一個(gè)新的窗口運(yùn)行
python3 client.py
gRPC HelloWorld測(cè)試

在原來的窗口可以看到called with lxc
gRPC HelloWorld測(cè)試

參考資料:
https://www.jianshu.co×××c947d98e192
https://blog.csdn.net/whereismatrix/article/details/78595550

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI