Skip to main content

a zookeeper registration center manager for python grpcio

Project description

zk_grpc

a zookeeper registration center manager for python grpcio

Requires: Python 3.5, grpcio, kazoo

install

pip install zk-grpc

####How to update 0.0.1 to 0.1.0

1. Update the Client server which use with ZKGrpc or AIOZKGrpc to v0.1.0 zk-grpc first.
2. Then update the server which use with ZKRegister or AIOZKRegister.

Notice: Can not use V0.0.1 ZKGrpc class with v0.1.0 ZKRegister class

More Eaxmples

Service Register

import signal
from example_pb2 import HelloRequest, HelloResponse
from example_pb2_grpc import HelloServiceServicer, add_HelloServiceServicer_to_server

from kazoo.client import KazooClient
from zk_grpc import ZKRegister


class HelloService(HelloServiceServicer):

    def hello_world(self, request: HelloRequest, context):
        hello = request.hello
        return HelloResponse(hello=hello)


def run(host, port):

    from grpc import server
    from concurrent.futures import ThreadPoolExecutor

    server = server(ThreadPoolExecutor(50))
    add_HelloServiceServicer_to_server(HelloService(), server)

    server.add_insecure_port("{}:{}".format(host, port))
    server.start()


    kz = KazooClient(hosts="127.0.0.1:2181")
    kz.start()

    zk_register = ZKRegister(kz_client=kz)
    # register all servicers on gprc server obj, do not support aio grpc server
    zk_register.register_grpc_server(server, host, port)
    # or register servicer one by one
    # zk_register.register_server(HelloServiceServicer, host, port)

    def shutdown(*args, **kwargs):
        zk_register.stop()
        # close kazoo client after zk_register stoped
        kz.stop()
        kz.close()

        server.stop(0.5)

    signal.signal(signal.SIGTERM, shutdown)

    try:
        server.wait_for_termination()
    except KeyboardInterrupt:
        shutdown()



if __name__ == '__main__':

    host = "127.0.0.1"
    port = 50052
    run(host, port)

Service Discovery

from example_pb2 import HelloRequest
from example_pb2_grpc import HelloServiceStub
from kazoo.client import KazooClient
from zk_grpc import ZKGrpc

def run():
    # before useing
    kz = KazooClient(hosts="127.0.0.1:2181")
    kz.start()

    zk_g = ZKGrpc(kz_client=kz)

    # get stub
    stub = zk_g.wrap_stub(HelloServiceStub)

    # call grpc api
    resp = stub.hello_world(HelloRequest(hello="hello"))
    print(resp.hello)

    # before exit
    zk_g.stop()
    kz.stop()
    kz.close()

if __name__ == '__main__':
    run()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zk-grpc-0.1.0.tar.gz (8.7 kB view details)

Uploaded Source

File details

Details for the file zk-grpc-0.1.0.tar.gz.

File metadata

  • Download URL: zk-grpc-0.1.0.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.6.8

File hashes

Hashes for zk-grpc-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4a891359ce842449114b65fa23352caec106fc528d59d29698842ed9b7c73bb1
MD5 29d98f6998fe39f2293cae004d58f56b
BLAKE2b-256 53358261094f8a01ed73426c0373565ac6e3aeb13733793a6dcdaf35b45e3c97

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page