django-nacos-microservice
A Django tool is designed to simplify the usage of Nacos in a microservices system architecture.
Nacos OpenAPI see: https://nacos.io/docs/latest/guide/user/open-api/
Supported Nacos version:
Nacos 0.8.0+ Nacos 1.x Nacos 2.x with http protocol
Supported Django version:
Django 2.2+
Installation
pip install django-nacos-microservice
Unified Configuration Management
Getting Started
# django project settings.py
# Get it through django-nacos-microservice
NACOS_SERVER_ADDRESSES = 'server addresses split by comma'
NACOS_SERVER_NAMESPACE = 'namespace id'
NACOS_SERVER_USERNAME = 'username'
NACOS_SERVER_PASSWORD = 'password'
NACOS_SERVER_GROUP = 'group id' # default group is DEFAULT_GROUP
CONFIG_SERVER_DATA_ID = "data id"
from django_microservice_nacos.nacos import get_config
config = get_config() # return Dict
print(config)
# Example
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': config.get('DATABASES_NAME'),
'USER': config.get('DATABASES_USER'),
'PASSWORD': config.get('DATABASES_PASSWORD'),
'HOST': config.get('DATABASES_HOST')
}
}
In Django projects, use a more elegant configuration method.
-
Step 1: Update the
settings.pyfile.# django project settings.py # Get it through django-nacos-microservice NACOS_SERVER_ADDRESSES = 'server addresses split by comma' NACOS_SERVER_NAMESPACE = 'namespace id' NACOS_SERVER_USERNAME = 'username' NACOS_SERVER_PASSWORD = 'password' CONFIG_SERVER_DATA_ID = "data id" from .env import env # Example DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': env('DATABASES_NAME'), 'USER': env('DATABASES_USER'), 'PASSWORD': env('DATABASES_PASSWORD'), 'HOST': env('DATABASES_HOST') } }
-
Step 2: Create a file named
env.pyin the project, in the same directory assettings.py, and put the following content in the file.from django_microservice_nacos.nacos import get_config from .env_default import DEFAULT_SETTINGS NACOS_SETTINGS = get_config() def env(key: str): return NACOS_SETTINGS.get(key) or DEFAULT_SETTINGS.get(key)
-
Step 3: Create a file named
env_default.pyin the project as the default settings, in the same directory asenv.py, and put the following content in the file.# if not django-nacos-microservice config, use default settings DEFAULT_SETTINGS = { 'DATABASES_NAME': 'db_name', 'DATABASES_USER': 'db_user', 'DATABASES_PASSWORD': 'db_password', 'DATABASES_HOST': 'db_host', # ... }
Service Liveness Probe Http Url
Note:This method is the same as the regular Django path, and may block and wait when serving high concurrency.
Beat Check
- Step 1: Update the
urls.pyfile in the project.from django.urls import path, include urlpatterns = [ # ... # add the path path('check/', include('django_nacos_microservice.urls')), # ... ]
- Step 2: Run django server, test beat check.
curl -X GET http://127.0.0.1:8000/check/beat/
Liveness Response: http status code 200
Metadata
Release files for django-nacos-microservice 1.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django_nacos_microservice-1.2.0.tar.gz | 7.8 kB | Details |
Release files / django_nacos_microservice-1.2.0.tar.gz
| Download URL | django_nacos_microservice-1.2.0.tar.gz |
|---|---|
| Size | 7.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e820cb60de36c166d6636858e92ee569fac62e2939977bddf66cc4da9381f67a
|
|
BLAKE2b-256 checksum How to use checksums |
7fba62f69f6429bfeb6bb9bce0c580adc6fe3c672ad755673c317a3cb8cb49ca
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.8.19
|