Commit 53d881b0 authored by 崔为之's avatar 崔为之 💪🏽

Update project

parent 7298d3cb
......@@ -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)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment