Skip to main content

极简 Apollo 配置客户端 - 支持实时更新、自动注入、.env 配置

Project description

apollo-time

极简 Apollo 配置客户端 - Python 版

特性

  • 极简使用:配置像变量一样直接访问,无需 get()
  • 链式类型转换config.PORT.as_int(8080) 简洁优雅
  • 自动更新:后台长轮询,配置实时同步
  • 线程安全:多线程环境下安全访问
  • 变更回调:支持监听配置变更事件
  • 环境变量:支持 .env 文件配置

安装

# 从云端安装
pip install apollo-time

快速开始

基础使用

from apollo_time import apollo_time_config

# 直接使用配置(自动从 .env 读取连接信息)
url = apollo_time_config.RAG_SERVER_URL
db_host = apollo_time_config.DB_HOST

# 链式类型转换(推荐)
port = apollo_time_config.SERVER_PORT.as_int(8080)
debug = apollo_time_config.DEBUG.as_bool(False)
timeout = apollo_time_config.TIMEOUT.as_float(5.0)

# 监听配置变更
apollo_time_config.on_change('API_KEY', lambda old, new: print(f"API Key 已更新"))

.env 配置

# .env 文件
APOLLO_APP_ID=your_app_id
APOLLO_CLUSTER=default
APOLLO_CONFIG_URL=http://your-apollo-server:8080
APOLLO_NAMESPACE=application

手动初始化

from apollo_time import init

config = init(
    app_id="my_app",
    cluster="default",
    server="http://apollo:8080",
    namespace="application"
)

高级用法

链式类型转换 ⭐

推荐使用 - 属性访问 + 链式类型转换:

# 整数转换
port = apollo_time_config.SERVER_PORT.as_int(8080)
max_conn = apollo_time_config.MAX_CONNECTIONS.as_int(100)

# 布尔转换
debug = apollo_time_config.DEBUG.as_bool(False)
enabled = apollo_time_config.FEATURE_ENABLED.as_bool()

# 浮点数转换
timeout = apollo_time_config.TIMEOUT.as_float(1.5)
rate = apollo_time_config.REQUEST_RATE.as_float(0.0)

# JSON 解析
features = apollo_time_config.FEATURES.as_json({})
settings = apollo_time_config.CONFIG.as_json({"key": "default"})

# 列表解析(支持自定义分隔符)
tags = apollo_time_config.TAGS.as_list()           # 默认逗号分隔
paths = apollo_time_config.PATHS.as_list(sep=':')  # 冒号分隔
hosts = apollo_time_config.HOSTS.as_list(sep=';')  # 分号分隔

传统类型转换

# 使用 get_xxx 方法
port = apollo_time_config.get_int("SERVER_PORT", 8080)
debug = apollo_time_config.get_bool("DEBUG", False)
timeout = apollo_time_config.get_float("TIMEOUT", 1.5)
config_dict = apollo_time_config.get_json("FEATURES")
tags = apollo_time_config.get_list("TAGS", sep=",")

Flask 集成

from flask import Flask, jsonify
from apollo_time import apollo_time_config

app = Flask(__name__)

@app.route('/api/config')
def get_config():
    return jsonify({
        'api_url': apollo_time_config.API_URL,
        'port': apollo_time_config.SERVER_PORT.as_int(8080),
        'debug': apollo_time_config.DEBUG.as_bool(False),
        'timeout': apollo_time_config.TIMEOUT.as_float(5.0)
    })

多实例

from apollo_time import Apollo

# 创建多个独立的客户端
config1 = Apollo("app1", server="http://apollo1:8080").start()
config2 = Apollo("app2", server="http://apollo2:8080").start()

配置优先级

参数 > 环境变量 > .env 文件 > 默认值

常见问题

Q: 如何禁用自动初始化?

A: 在导入前设置环境变量:

import os
os.environ['APOLLO_AUTO_INIT'] = 'false'

Q: 配置多久更新一次?

A: 使用长轮询,Apollo 有变更时立即推送,最长 70 秒。

License

MIT

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

apollo_time-0.2.1.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

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

apollo_time-0.2.1-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file apollo_time-0.2.1.tar.gz.

File metadata

  • Download URL: apollo_time-0.2.1.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for apollo_time-0.2.1.tar.gz
Algorithm Hash digest
SHA256 676b3075dd5c314090c5fad95d4025934084c93cca5ca22b69faf067c4e0b7e9
MD5 8b14a87a3a9fa18644c946f5157b68e5
BLAKE2b-256 54d51fcaaea4c7463aebf1d496ef39aa31e50860aa90e2b7ae4d139aa9db1cfe

See more details on using hashes here.

File details

Details for the file apollo_time-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: apollo_time-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for apollo_time-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 aaad5bd1dfc7002cfb9fd0358db2b6ce9c828d1b352265fa23ef3c974e230a09
MD5 bfec9d9e336ba735f60e8cd717744507
BLAKE2b-256 35c71d081704a07353a8b02a34c94804bfbd8d1d9a916998d3c93f963c305744

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