配置服务客户端
config-client的Python项目详细描述
配置客户端
为spring cloud config配置客户端包。
安装
使用pip安装和更新:
pip install -U config-client
依赖关系
设置
默认的url模式是:
CONFIGSERVER_ADDRESS
/BRANCH
/APP_NAME
-PROFILE
.json
# expected environment variables:#CONFIGSERVER_ADDRESS=http://localhost:8888/configurationBRANCH=masterPROFILE=developmentAPP_NAME=myapp
将导致:
http://localhost:8888/configuration/master/myapp-development.json
可以使用参数url
在构造函数上自定义url模式。
fromconfigimportspringc=spring.ConfigClient(app_name='myapp',url="{address}/{branch}/{profile}-{app_name}.json")c.url# output: 'http://localhost:8888/configuration/master/development-myapp.json'
默认值
如果没有为下面的环境变量调整值,则将假定默认值为:
CONFIGSERVER_ADDRESS=http://localhost:8888/configurationBRANCH=masterPROFILE=developmentAPP_NAME=
支持的响应格式
- json
只需将.json
扩展名添加到url参数的末尾。例如:
c=ConfigClient(app_name='foo',profile='development',address='http://localhost:8888',branch='master',url='{address}/{branch}/{app_name}-{profile}.json'# <)
它将生成url:http://localhost:8888/master/foo-development.json
。
注意.yaml
不支持,所有扩展都将在内部转换为.json
。
用法示例
使用标准客户端
fromconfig.springimportConfigClientconfig_client=ConfigClient(app_name='my_app')config_client.get_config()# option 1: dict like with direct accessconfig_client.config['spring']['cloud']['consul']['host']# option 2: dict like using getconfig_client.config.get('spring').get('cloud').get('consul').get('port')# option 3: using get_attribute methodconfig_client.config.get_attribute('spring.cloud.consul.port')
带有@decorator
的标准客户端用于设置环境变量的用例。
fromconfigimportspring@spring.config_client()defmy_test(config_client=None):config_client.config['spring']['cloud']['consul']['host']config_client.config.get('spring').get('cloud').get('consul').get('port')config_client.config.get_attribute('spring.cloud.consul.port')
对于未设置环境变量的用例,可以使用decorator参数,如下所示:
fromconfigimportspring@spring.config_client(app_name='myapp',branch="dev")defmy_test(config_client=None):config_client.config['spring']['cloud']['consul']['host']config_client.config.get('spring').get('cloud').get('consul').get('port')config_client.config.get_attribute('spring.cloud.consul.port')
与烧瓶整合。
fromconfig.springimportConfigClientfromflaskimportFlaskconfig_client=ConfigClient(app_name="myapp")config_client.get_config()app=Flask(__name__)app.run(host='0.0.0.0',port=config_client.config['app']['port']
使用异步
使用异步的客户端
importasynciofromconfig.springimportConfigClientloop=asyncio.get_event_loop()config_client=ConfigClient(app_name='myapp')config_client.get_config()asyncdefservice_discovery():awaitdiscovery_client.register(config_client.config['app']['name'],config_client.config.get('app').get('port'))discovery_client=Consul(config_client.config['spring']['cloud']['consul']['host'],config_client.config['spring']['cloud']['consul']['port'],loop)loop.run_until_complete(service_discovery)
开发
运行测试
安装开发依赖项。
pip install -r requirements-dev.txt
要执行测试,请运行:
python -m pytest -v --cov-report term --cov=config tests
链接
- 许可证:Apache License
- 代码:https://github.com/amenezes/config-client
- 问题跟踪器:https://github.com/amenezes/config-client/issues