From 53d881b07d5bec0b651471d58666fff7e7f2b7bb Mon Sep 17 00:00:00 2001 From: cuiweizhi <560397@gree.com.cn> Date: Sat, 4 Nov 2023 14:37:34 +0800 Subject: [PATCH] Update project --- application/libs/config/consul.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/application/libs/config/consul.py b/application/libs/config/consul.py index 3eee5c2..769d07a 100644 --- a/application/libs/config/consul.py +++ b/application/libs/config/consul.py @@ -18,12 +18,31 @@ import yaml class ConsulConfig: def __init__(self, host='localhost', port=8500, token=None, dc='dc1'): + """ + Initialize a ConsulConfig instance. + + :param host: The host of the Consul server. Default is 'localhost'. + :param port: The port of the Consul server. Default is 8500. + :param token: The Consul token. Default is None. + :param dc: The datacenter to use. Default is 'dc1'. + """ self.token = token self.dc = dc + # Create a Consul client instance self.client = consul.Consul(host=host, port=port, token=token, dc=dc) def get(self, key: str) -> Any: + """ + Retrieve the value of a key from the Consul KV store. + + :param key: The key to retrieve. + :return: The value of the key. + :raises KeyError: If the key is not found in Consul. + """ + # Retrieve the key-value pair from Consul _, data = self.client.kv.get(key=key, token=self.token, dc=self.dc) + # If the key-value pair is not found, raise an exception if data is None: raise KeyError(f'Key {key} not found in Consul.') + # Load the value from the key-value pair as YAML return yaml.load(data['Value'], Loader=yaml.FullLoader) -- GitLab