Skip to main content

Polaris Python Client

Project description

Polaris python 客户端

构建方式

rm -fr ceres/polaris
poetry build
# poetry publish
# twine upload dist/*

获取方式

  • 安装依赖的方式
pip install polaris-python-bzl==0.0.2

命令行工具

# python -m ceres -h 获取更多用法
# 注册实例(-r参数): 注册一个固定实例(关闭健康检查), 注意 token 必须
python -m ceres -e rd -r -s "my-service" -n "bzl-infra" -i "127.0.0.2" -p 123 -t "***"

# 发现实例(token可选)
python -m ceres -e rd -s "my-service" -n "bzl-infra" -c 100

# python -m ceres -h 获取更多用法
# 反注册实例(-d参数): 反注册一个固定实例(id 或namespace+service+host+port都可以反注册),注意 token 必须
python -m ceres -e rd -d -s "my-service" -n "bzl-infra" -id "********************" -t "***"
python -m ceres -e rd -d -s "my-service" -n "bzl-infra" -i "127.0.0.2" -p 123 -t "***"

示例代码

import json
from ceres.polaris import CeresClient, RegisterInstance, Instance, InstanceRequest
import sys
from time import sleep


def provide():
    ### 注册实例,注意填写正确的token:
    try:
        cc = CeresClient(_host='127.0.0.1', _token='********')
        instance = RegisterInstance(namespace='bzl-infra', service='my-service', host='1.1.1.1', port=1234)
        response = cc.register_instance(instance)
    except Exception as e:
        print("request error:", e)
        sys.exit(1)
    # ...
    print(response.__dict__ if instance is not None else None)
    # 注册id
    instance.id = response.id

    # ...
    sleep(10)

    # 程序退出前反注册
    print(cc.deregister_instance(instance).__dict__ if instance is not None else None)


def consume():
    ### 发现(获取所有健康实例):
    try:
        cc = CeresClient(_host='127.0.0.1')
        instances = cc.get_healthy_instances(InstanceRequest(namespace='bzl-infra', service='my-service'))
        print(json.dumps([instance.__dict__ for instance in instances]) if instances is not None else None)
    except Exception as e:
        print(e)
        sys.exit(1)


def consume_one():
    ### 发现(根据权重获取单个实例):
    try:
        cc = CeresClient(_host='127.0.0.1')
        for i in range(0, 100):
            instance = cc.get_one_instance(InstanceRequest(namespace='bzl-infra', service='my-service'))
            print('weight', instance.__dict__ if instance is not None else None)
    except Exception as e:
        print(e)
        sys.exit(1)


def consume_one_by_hash():
    ### 发现(根据hash-key获取列表内的固定实例):
    try:
        cc = CeresClient(_host='127.0.0.1')
        for i in range(0, 100):
            instance = cc.get_one_instance(InstanceRequest(namespace='bzl-infra', service='my-service', hash_key='1'))
            print('hash', instance.__dict__ if instance is not None else None)
    except Exception as e:
        print(e)
        sys.exit(1)

def consume_one_by_filter():
    ### 发现(根据filter获取列表内的实例)

    def filter_http(_instance:Instance):
        """仅获取协议为http的实例"""
        return _instance.protocol == 'http'

    def filter_metadata(_instance:Instance):
        """获取metadata中 dubbo-version 以 2.7 开头的实例"""
        return _instance.metadata.get('dubbo-version', '').startswith('2.7') if _instance.metadata is not None else False

    try:
        cc = CeresClient(_host='127.0.0.1')
        for i in range(0, 100):
            instance = cc.get_one_instance(InstanceRequest(namespace='bzl-infra', service='my-service', filter_func=filter_http))
            print('filter http', instance.__dict__ if instance is not None else None)

            instance = cc.get_one_instance(InstanceRequest(namespace='bzl-infra', service='my-service', filter_func=filter_metadata))
            print('filter metadata', instance.__dict__ if instance is not None else None)
    except Exception as e:
        print(e)
        sys.exit(1)


if __name__ == '__main__':
    # provide()
    consume()
    consume_one()
    consume_one_by_hash()
    consume_one_by_filter()

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

polaris_python_bzl-0.0.3.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

polaris_python_bzl-0.0.3-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file polaris_python_bzl-0.0.3.tar.gz.

File metadata

  • Download URL: polaris_python_bzl-0.0.3.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.9

File hashes

Hashes for polaris_python_bzl-0.0.3.tar.gz
Algorithm Hash digest
SHA256 38f471b43119a746416d52a98a05d20005bad7a2ce4514921008614d30479844
MD5 6b07783329e3615187a21a2728ba357a
BLAKE2b-256 06892a86a036145a4ddafcd0476001b8e4540392fce935a4ca87f9898b5cd95e

See more details on using hashes here.

File details

Details for the file polaris_python_bzl-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for polaris_python_bzl-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 12f0911cb76066bd42019fcd939aca6c6012911eb8f283bfdeea91b699adb2e3
MD5 5581775666523d191a2ac95e8392e164
BLAKE2b-256 f4000cd5f54ab70832c1a6325419caf6d965690f793a4c43abbf23dd29dc141b

See more details on using hashes here.

Supported by

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