### Run Simple Configuration Update Example Source: https://github.com/outercloud/pyapollo/blob/main/README.md Execute a simple example script for configuration updates. ```bash python examples/simple_update_examples.py ``` -------------------------------- ### Run Custom Configuration Server Example Source: https://github.com/outercloud/pyapollo/blob/main/README.md Execute the example script for using a custom configuration server. ```bash python examples/custom_config_server_demo.py ``` -------------------------------- ### Run Asynchronous Client Custom Configuration Server Example Source: https://github.com/outercloud/pyapollo/blob/main/README.md Execute the example script for an asynchronous client with a custom configuration server. ```bash python examples/async_custom_config_server_demo.py ``` -------------------------------- ### Run Configuration Update Examples Source: https://github.com/outercloud/pyapollo/blob/main/README.en.md Demonstrates various methods for updating configurations, including detailed, simple, and custom server configurations. ```bash # Detailed configuration update demonstration python examples/update_config_demo.py ``` ```bash # Simple configuration update examples python examples/simple_update_examples.py ``` ```bash # Custom config server examples python examples/custom_config_server_demo.py ``` ```bash # Async client custom config server examples python examples/async_custom_config_server_demo.py ``` -------------------------------- ### Run Detailed Configuration Update Example Source: https://github.com/outercloud/pyapollo/blob/main/README.md Execute the script for a detailed demonstration of configuration updates. ```bash python examples/update_config_demo.py ``` -------------------------------- ### Run Synchronous Client Example Source: https://github.com/outercloud/pyapollo/blob/main/README.md Execute the synchronous client demonstration script. ```bash python examples/sync_demo.py ``` -------------------------------- ### Configure .env File for PyApollo Source: https://github.com/outercloud/pyapollo/blob/main/README.md Run the .env file configuration example, specifying configuration keys as needed. Ensure a .env file exists in the project root or examples directory. ```bash # 确保项目根目录有 .env 文件或 examples/test.env 文件 # 使用默认键名 python examples/dotenv_demo.py # 指定配置键名 python examples/dotenv_demo.py --key your_config_key # 指定不同的文本配置键和JSON配置键 python examples/dotenv_demo.py --key your_config_key --json-key your_json_key ``` -------------------------------- ### Run Asynchronous Client Example Source: https://github.com/outercloud/pyapollo/blob/main/README.md Execute the asynchronous client demonstration script. ```bash python examples/async_demo.py ``` -------------------------------- ### Configure Environment Variables for PyApollo Source: https://github.com/outercloud/pyapollo/blob/main/README.md Set necessary environment variables before running the environment variable configuration example. This example demonstrates using default and specified configuration keys. ```bash # 先设置必要的环境变量 export APOLLO_META_SERVER_ADDRESS=http://localhost:8080 export APOLLO_APP_ID=your-app-id # 运行示例(使用默认键名 sample_key) python examples/env_demo.py # 运行示例(指定配置键名) python examples/env_demo.py --key your_config_key # 运行示例(指定不同的文本配置键和JSON配置键) python examples/env_demo.py --key your_config_key --json-key your_json_key ``` -------------------------------- ### Install pyapollo via pip Source: https://github.com/outercloud/pyapollo/blob/main/README.en.md Install the pyapollo library using pip. Ensure you are using a version greater than or equal to 0.2.0. ```bash pip install pyapollo-zenkilan ``` -------------------------------- ### Example .env file for pyapollo configuration Source: https://github.com/outercloud/pyapollo/blob/main/README.en.md Configure pyapollo settings by creating a .env file in your project root. This includes required Apollo server details, application ID, and optional authentication and cluster settings. ```dotenv # Required Apollo Settings APOLLO_META_SERVER_ADDRESS=http://localhost:8080 APOLLO_APP_ID=your-app-id # Authentication Settings APOLLO_USING_APP_SECRET=true APOLLO_APP_SECRET=your-app-secret # Optional Settings APOLLO_CLUSTER=default APOLLO_ENV=DEV APOLLO_NAMESPACES=application,common,database APOLLO_TIMEOUT=10 APOLLO_CYCLE_TIME=30 ``` -------------------------------- ### PyApollo Parameter Validation Error Handling Source: https://github.com/outercloud/pyapollo/blob/main/README.md Illustrates how PyApollo handles invalid parameters during configuration updates by raising a ValueError. Examples include negative timeouts, empty namespaces, and empty app IDs. ```python try: # 无效参数会抛出 ValueError client.update_config(timeout=-1) # 负数超时 client.update_config(namespaces=[]) # 空命名空间列表 client.update_config(app_id="") # 空应用ID except ValueError as e: print(f"参数验证失败: {e}") ``` -------------------------------- ### Initialize Synchronous Apollo Client Source: https://github.com/outercloud/pyapollo/blob/main/README.en.md Demonstrates initializing the synchronous ApolloClient with direct parameters for meta server address, app ID, and app secret, or without authentication. It also shows loading from environment variables. ```python from pyapollo.client import ApolloClient # Method 1: Direct parameters meta_server_address = "https://your-apollo/meta-server-address" app_id="your-apollo-app-id" app_secret="your-apollo-app-secret" # Apollo client with authentication apollo = ApolloClient( meta_server_address=meta_server_address, app_id=app_id, app_secret=app_secret, ) # Apollo client without authentication apollo = ApolloClient( meta_server_address=meta_server_address, app_id=app_id, ) # Method 2: Load from environment variables or .env file apollo = ApolloClient() # Automatically loads from environment or .env file ``` -------------------------------- ### Synchronous Apollo Client with Custom Config Server Source: https://github.com/outercloud/pyapollo/blob/main/README.md Shows how to initialize a synchronous Apollo client by directly specifying the configuration server host and port, bypassing the meta-server discovery. Also demonstrates runtime switching between custom and meta-server configurations. ```python from pyapollo.client import ApolloClient # 直接连接到已知配置服务器,无需元服务器 client = ApolloClient( app_id="my-app", config_server_host="http://config-server.example.com", config_server_port=8080 ) # 运行时切换配置服务器 client.update_config( config_server_host="http://backup-config.example.com", config_server_port=9090 ) # 切换回使用元服务器发现(需要提供 meta_server_address) client.update_config( meta_server_address="http://meta-server.example.com", config_server_host=None, # 清除自定义配置 config_server_port=None ) ``` -------------------------------- ### Async Apollo Client with Authentication Source: https://github.com/outercloud/pyapollo/blob/main/README.md Use this snippet to initialize an asynchronous Apollo client with authentication credentials. It demonstrates fetching JSON and text configurations. ```python import asyncio from pyapollo.async_client import AsyncApolloClient async def main(): meta_server_address = "https://your-apollo/meta-server-address" app_id="your-apollo-app-id" app_secret="your-apollo-app-secret" # 有鉴权的 Apollo 异步客户端 async with AsyncApolloClient( meta_server_address=meta_server_address, app_id=app_id, app_secret=app_secret, ) as client: # 获取 text 格式配置项 val = await client.get_json_value("json_key") print(val) # 获取 JSON 格式配置项 json_val = await client.get_value("text_key") print(json_val) # 无鉴权的 Apollo 异步客户端 async with AsyncApolloClient( meta_server_address=meta_server_address, app_id=app_id, ) as client: # 获取 text 格式配置项 val = await client.get_json_value("json_key") print(val) # 获取 JSON 格式配置项 json_val = await client.get_value("text_key") print(json_val) # 使用自定义配置服务器的异步客户端 async with AsyncApolloClient( app_id=app_id, config_server_host="http://config-server.example.com", config_server_port=8080 ) as client: # 动态更新配置 await client.update_config( timeout=120, namespaces=["application", "cache"] ) val = await client.get_value("sample_key") print(val) if __name__ == "__main__": asyncio.run(main()) ``` -------------------------------- ### Fetch Configurations with Synchronous Apollo Client Source: https://github.com/outercloud/pyapollo/blob/main/README.en.md Shows how to retrieve configuration values using the synchronous ApolloClient. Supports fetching values in text format and JSON format. ```python # Get text format configuration val = apollo.get_value("text_key") print(val) # Get JSON format configuration json_val = apollo.get_json_value("json_key") print(json_val) ``` -------------------------------- ### Initialize and Use Asynchronous Apollo Client Source: https://github.com/outercloud/pyapollo/blob/main/README.en.md Illustrates initializing and using the asynchronous AsyncApolloClient within an async context. Supports authentication, no authentication, and custom config server hosts. ```python import asyncio from pyapollo.async_client import AsyncApolloClient async def main(): meta_server_address = "https://your-apollo/meta-server-address" app_id="your-apollo-app-id" app_secret="your-apollo-app-secret" # Apollo async client with authentication async with AsyncApolloClient( meta_server_address=meta_server_address, app_id=app_id, app_secret=app_secret, ) as client: # Get text format configuration val = await client.get_json_value("json_key") print(val) # Get JSON format configuration json_val = await client.get_value("text_key") print(json_val) # Apollo async client without authentication async with AsyncApolloClient( meta_server_address=meta_server_address, app_id=app_id, ) as client: # Get text format configuration val = await client.get_json_value("json_key") print(val) # Get JSON format configuration json_val = await client.get_value("text_key") print(json_val) # Async client with custom config server async with AsyncApolloClient( app_id=app_id, config_server_host="http://config-server.example.com", config_server_port=8080 ) as client: # Dynamically update configuration await client.update_config( timeout=120, namespaces=["application", "cache"] ) val = await client.get_value("sample_key") print(val) if __name__ == "__main__": asyncio.run(main()) ``` -------------------------------- ### Connect Directly to Custom Config Server Source: https://github.com/outercloud/pyapollo/blob/main/README.en.md Initialize the ApolloClient by directly specifying the `config_server_host` and `config_server_port` to bypass meta server discovery. You can also switch to a custom config server or back to meta server discovery at runtime. ```python from pyapollo.client import ApolloClient # Connect directly to known config server, no meta server needed client = ApolloClient( app_id="my-app", config_server_host="http://config-server.example.com", config_server_port=8080 ) # Switch config server at runtime client.update_config( config_server_host="http://backup-config.example.com", config_server_port=9090 ) # Switch back to meta server discovery (need to provide meta_server_address) client.update_config( meta_server_address="http://meta-server.example.com", config_server_host=None, # Clear custom config config_server_port=None ) ``` -------------------------------- ### Synchronous Apollo Client Dynamic Configuration Updates Source: https://github.com/outercloud/pyapollo/blob/main/README.md Demonstrates how to dynamically update configuration parameters for a synchronous Apollo client without re-instantiating it. Covers updating single/multiple parameters, namespaces, authentication, and custom servers. ```python from pyapollo.client import ApolloClient # 创建客户端 client = ApolloClient( meta_server_address="http://localhost:8080", app_id="my-app", timeout=30, cycle_time=60 ) # 动态更新单个参数 client.update_config(timeout=120) # 批量更新多个参数 client.update_config( timeout=60, cycle_time=30, cluster="production" ) # 更新命名空间 client.update_config( namespaces=["application", "redis", "database"] ) # 环境切换 client.update_config( meta_server_address="http://prod-apollo:8080", env="PROD", cluster="production" ) # 添加认证 client.update_config( app_secret="your-secret-key" ) # 自定义配置服务器(绕过元服务器发现) client.update_config( config_server_host="http://config.example.com", config_server_port=8080 ) # 查看当前配置 config = client.get_current_config() print(config) ``` -------------------------------- ### Configure PyApollo with Environment Variables Source: https://github.com/outercloud/pyapollo/blob/main/README.en.md Set environment variables for APOLLO_META_SERVER_ADDRESS and APOLLO_APP_ID, then run the environment variable demonstration script. Supports specifying configuration keys. ```bash # First set the required environment variables export APOLLO_META_SERVER_ADDRESS=http://localhost:8080 export APOLLO_APP_ID=your-app-id # Run the example (with default key name 'sample_key') python examples/env_demo.py # Run the example (with specified configuration key) python examples/env_demo.py --key your_config_key # Run the example (with different text and JSON configuration keys) python examples/env_demo.py --key your_config_key --json-key your_json_key ``` -------------------------------- ### Configure PyApollo with .env File Source: https://github.com/outercloud/pyapollo/blob/main/README.en.md Use a .env file for configuration. The script supports specifying configuration keys for text and JSON values. ```bash # Make sure you have a .env file in the project root or examples/test.env # Using default key names python examples/dotenv_demo.py # Specify configuration key python examples/dotenv_demo.py --key your_config_key # Specify different text and JSON configuration keys python examples/dotenv_demo.py --key your_config_key --json-key your_json_key ``` -------------------------------- ### Configure pyapollo using a custom .env file path Source: https://github.com/outercloud/pyapollo/blob/main/README.en.md Load Apollo client settings from a custom .env file path using ApolloSettingsConfig. This allows for flexible configuration management. ```python from pyapollo.settings import ApolloSettingsConfig from pyapollo.client import ApolloClient # Load from custom .env file path settings = ApolloSettingsConfig.from_env_file("path/to/custom.env") client = ApolloClient(settings=settings) ``` -------------------------------- ### Set pyapollo configuration using environment variables Source: https://github.com/outercloud/pyapollo/blob/main/README.en.md Configure pyapollo by exporting environment variables. This method is useful for deployment environments where direct file manipulation is not preferred. ```bash # Required settings export APOLLO_META_SERVER_ADDRESS=http://localhost:8080 export APOLLO_APP_ID=your-app-id # Authentication settings (optional) export APOLLO_USING_APP_SECRET=true export APOLLO_APP_SECRET=your-app-secret # Other optional settings export APOLLO_CLUSTER=default export APOLLO_ENV=DEV export APOLLO_NAMESPACES=application,common,database export APOLLO_TIMEOUT=10 export APOLLO_CYCLE_TIME=30 ``` -------------------------------- ### Configure pyapollo using ApolloSettingsConfig object Source: https://github.com/outercloud/pyapollo/blob/main/README.en.md Instantiate ApolloSettingsConfig with direct parameters and pass it to the ApolloClient. This provides a programmatic way to configure the client. ```python from pyapollo.settings import ApolloSettingsConfig from pyapollo.client import ApolloClient # Create settings object settings = ApolloSettingsConfig( meta_server_address="http://localhost:8080", app_id="your-app-id", using_app_secret=True, app_secret="your-app-secret" ) # Pass settings to client client = ApolloClient(settings=settings) ``` -------------------------------- ### Add pyapollo to requirements.txt Source: https://github.com/outercloud/pyapollo/blob/main/README.en.md Include pyapollo in your project's requirements.txt file for dependency management. Specify a version of 0.2.0 or higher. ```text pyapollo-zenkilan>=0.2.0 ``` -------------------------------- ### Handle Parameter Validation Errors Source: https://github.com/outercloud/pyapollo/blob/main/README.en.md The `update_config` method performs parameter validation and raises a `ValueError` for invalid inputs. Ensure parameters like timeout, namespaces, and app_id meet the required criteria. ```python try: # Invalid parameters will raise ValueError client.update_config(timeout=-1) # Negative timeout client.update_config(namespaces=[]) # Empty namespace list client.update_config(app_id="") # Empty app ID except ValueError as e: print(f"Parameter validation failed: {e}") ``` -------------------------------- ### Update pyapollo Client Configuration Dynamically Source: https://github.com/outercloud/pyapollo/blob/main/README.en.md Use `update_config` to modify single or multiple client parameters at runtime. This method supports updating timeouts, namespaces, environment settings, authentication, and custom config server addresses. ```python from pyapollo.client import ApolloClient # Create client client = ApolloClient( meta_server_address="http://localhost:8080", app_id="my-app", timeout=30, cycle_time=60 ) # Update single parameter client.update_config(timeout=120) # Batch update multiple parameters client.update_config( timeout=60, cycle_time=30, cluster="production" ) # Update namespaces client.update_config( namespaces=["application", "redis", "database"] ) # Environment switching client.update_config( meta_server_address="http://prod-apollo:8080", env="PROD", cluster="production" ) # Add authentication client.update_config( app_secret="your-secret-key" ) # Custom config server (bypass meta server discovery) client.update_config( config_server_host="http://config.example.com", config_server_port=8080 ) # View current configuration config = client.get_current_config() print(config) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.