Example use case of using Consul KV to retrieve Configuration values to have a stateless micro service.
Python based:
- Consul agent running at <consul_ip>.
- Django based micro service.
- Manage DB configuration in settings.py of the micro service.
Steps:
- Add database configuration key values to Consul:
curl -X PUT -d 'django.db.backends.mysql' http://<consul_ip>:8500/v1/kv/<service_name>/database/engine
curl -X PUT -d '<DB_NAME>' http://<consul_ip>:8500/v1/kv/<service_name>/database/name
curl -X PUT -d '<DB_IP>' http://<consul_ip>:8500/v1/kv/<service_name>/database/ip
curl -X PUT -d 3306 http://<consul_ip>8500/v1/kv/<service_name>/database/port
curl -X PUT -d '<DB_USERNAME>' http://<consul_ip>:8500/v1/kv/<service_name>/database/username
curl -X PUT -d '<DB_PASSWORD>' http://<consul_ip>:8500/v1/kv/<service_name>/database/passwordIn settings.py of Django, retrieve the DB configuration values as:
import envconsul
ENV_CONSUL = envconsul.EnvConsul(
service_name='<service_name>',
host='<consul_ip>',
port=8500,
)DB_ENGINE = ENV_CONSUL.get_str('/database/engine')
DB_NAME = ENV_CONSUL.get_str('/database/name')
DB_IP = ENV_CONSUL.get_str('/database/ip')
DB_PORT = ENV_CONSUL.get('/database/port')
DB_USERNAME = ENV_CONSUL.get_str('/database/username')
DB_PASSWORD = ENV_CONSUL.get_str('/database/password')# Use the values obtained from Consul in DATABASES config of Django.
DATABASES = {
'default': {
'ENGINE': DB_ENGINE,
'NAME': DB_NAME,
'HOST': DB_IP,
'PORT': DB_PORT,
'USER': DB_USERNAME,
'PASSWORD': DB_PASSWORD,
},
}This way, DB configuration can easily be passed to all the micro services.
2.