apolloconfig / apollo

Apollo is a reliable configuration management system suitable for microservice configuration management scenarios.
https://www.apolloconfig.com
Apache License 2.0
28.94k stars 10.19k forks source link

apollo python 客户端 #5089

Closed onecer closed 3 months ago

onecer commented 5 months ago

包地址: https://pypi.org/project/apollo-python/ 仓库:https://github.com/onecer/apollo-python 协议:MIT

公司用到,但是发现官网上提供的几个都不能用过了,参考了他们的代码,撸了一个可用的。

支持环境变量,方便 k8s 部署的服务从环境传递变量,不用挨个服务代码兼容。

以下是文档,如果适合,可以收录到官网。

安装


pip install apollo-python

使用


导入包

from apollo_python import ApolloClient
app_id = 'demo-service'
config_url = 'http://127.0.0.1:8080'
cluster = 'default'
secret = ''
env = 'DEV'

client = ApolloClient(app_id=app_id, config_url=config_url, cluster=cluster, secret=secret, env=env)
lm_API_KEY = client.get_value("lm_API_KEY")

也默认支持通过环境变量来传递值,基本和 Java 客户端保持一致

客户端初始化参数

参数 说明 默认值 环境变量
app_id 应用名 APP_ID
config_url 配置中心地址 http://127.0.0.1:8080 ${ENV}_META or APOLLO_META
cluster 集群名 default IDC
secret 访问密钥 APOLLO_ACCESS_KEY_SECRET
env 环境 DEV ENV
client_ip 客户端ip 获取当前 IP CLIENT_IP
cache_path 配置缓存路径 tmp/apollo/cache APOLLO_CACHE_PATH
need_hot_update 是否需要热更新 True -
change_listener 配置变更监听器(回调函数) None -
log_level 日志级别 INFO LOG_LEVEL
notification_map 通知配置 (dict) None -

配置变更监听器 change_listener

"""
接受 4 个参数 
action:delete \ add  \ update
namespace:namespace
key:key
old_value:old_value
"""
def change_listener(action, namespace, key, old_value):
    print(f"action:{action} namespace: {namespace} key: {key} old_value: {old_value}")

通知配置 notification_map

notification_map = {
    "application": ["application"],
    "application.yml": ["application.yml"]
}

配置优先级

环境变量 > 代码配置

如果环境变量存在,则优先使用环境变量的值。

如果环境中存在 ENV的环境变量, 如 ENV=DEV。则优先组合出 DEV_META 这个环境变量名称来获取 config url。 如果该环境变量不存在,则取 APOLLO_META 环境变量的值。如果 APOLLO_META 也不存在,则使用代码定义的 config_url 的值。

其它环境变量同理,以此类推。

热更新

默认会启动一个线程来定时更新本地缓存的配置,所以,如果每次用的是 get_value 来获取配置,可以实现配置热更新。

dosubot[bot] commented 5 months ago

🤖

根据我对Apollo仓库的了解,Apollo确实支持Python客户端。目前有三个Python客户端可供选择:

  1. pyapollo@filamoon提供
  2. BruceWW-pyapollo@BruceWW提供
  3. xhrg-product/apollo-client-python@xhrg-product提供

这些信息可以在Python SDK用户指南中找到。

关于环境变量的使用,Apollo也支持。在apollo-env.properties文件中,环境变量被用来设置不同配置的值。例如,dev.meta=${dev_meta}就是使用dev_meta环境变量来设置dev.meta的值。这个文件可以在这里找到。

至于您提供的apollo-python客户端是否与Apollo的最新版本,Apollo 2.2.0 Release兼容,以及是否支持与Apollo仓库中列出的其他Python客户端相同的功能,或者与其他Python客户端相比是否有任何已知的问题或限制,我在仓库中没有找到相关信息。这些答案可能在其他地方可以找到,或者我可能遗漏了。

如果您能提供更多关于您的apollo-python客户端的信息,我可能能更好地回答您的问题。

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

nobodyiam commented 4 months ago

感谢,可以提个 PR 更新到 Python 客户端指南(zh, en

stale[bot] commented 3 months ago

This issue has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in 7 days unless it is tagged "help wanted" or other activity occurs. Thank you for your contributions.

stale[bot] commented 3 months ago

This issue has been automatically closed because it has not had activity in the last 7 days. If this issue is still valid, please ping a maintainer and ask them to label it as "help wanted". Thank you for your contributions.