Skip to main content

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

Metadata

Release files for apollo-time 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for apollo-time 0.2.1
File Size Uploaded
apollo_time-0.2.1.tar.gz 7.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for apollo-time 0.2.1
File Interpreter ABI Platform
apollo_time-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 15.7 kB

Release files / apollo_time-0.2.1.tar.gz

Download URL apollo_time-0.2.1.tar.gz
Size 7.5 kB
Tags Source
SHA-256 checksum
How to use checksums
676b3075dd5c314090c5fad95d4025934084c93cca5ca22b69faf067c4e0b7e9
BLAKE2b-256 checksum
How to use checksums
54d51fcaaea4c7463aebf1d496ef39aa31e50860aa90e2b7ae4d139aa9db1cfe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.9

Release files / apollo_time-0.2.1-py3-none-any.whl

Download URL apollo_time-0.2.1-py3-none-any.whl
Size 8.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
aaad5bd1dfc7002cfb9fd0358db2b6ce9c828d1b352265fa23ef3c974e230a09
BLAKE2b-256 checksum
How to use checksums
35c71d081704a07353a8b02a34c94804bfbd8d1d9a916998d3c93f963c305744
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.9

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page