Skip to main content

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

Project description

apollo-time

极简 Apollo 配置客户端 - Python 版

特性

  • 极简使用:配置像变量一样直接访问,无需 get()
  • 自动更新:后台长轮询,配置实时同步
  • 线程安全:多线程环境下安全访问
  • 变更回调:支持监听配置变更事件
  • 环境变量:支持 .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

# 监听配置变更
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.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
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,
        'timeout': apollo_time_config.get_int('TIMEOUT')
    })

多实例

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.0.tar.gz (7.2 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.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: apollo_time-0.2.0.tar.gz
  • Upload date:
  • Size: 7.2 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.0.tar.gz
Algorithm Hash digest
SHA256 76e4341b406d41bcfce37361938c8d4f5c3609256b617098affe8c02653a04c9
MD5 51374dab827248c5e91f5ec8c6d7ca70
BLAKE2b-256 c6843a3e0c9fb3212df803c9a0766438ec1243b0d621d5ffaef5d8ef626d73c9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: apollo_time-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9340c0246cc4ba5e482e2035e706ddb8d8a5477af970954b65b4913235955143
MD5 e546cbe71474c006d357552e7ec85e7a
BLAKE2b-256 9e6dbc9542937316730c4d8a7f520fea4c7d223e50a432a6ed6a98d6395ff5de

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