Skip to main content

A Django tool is designed to simplify the usage of Nacos in a microservices system.

Project description

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/

Pypi Version License

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.py file.

    # 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.py in the project, in the same directory as settings.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.py in the project as the default settings, in the same directory as env.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.py file 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_nacos_microservice-1.1.0.tar.gz (7.8 kB view details)

Uploaded Source

File details

Details for the file django_nacos_microservice-1.1.0.tar.gz.

File metadata

File hashes

Hashes for django_nacos_microservice-1.1.0.tar.gz
Algorithm Hash digest
SHA256 c0beb26bc39711c5525bab9fa52219ff52145fd3d120654d96305e1b785cabcd
MD5 6605b66dd3df0d4b9a2e5f679005e28a
BLAKE2b-256 07f4d29c724c74549222ab2244a34b7beb694f6fc6d3409e5d0b26c64573af59

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page