Skip to content

Service Discovery and Configuration Management

**Referenced Files in This Document** - [[client.go]](file/bi-common/registry/nacos/client.go) - [[discovery.go]](file/bi-common/registry/nacos/discovery.go) - [[config.go]](file/bi-common/registry/nacos/config.go) - [[loader.go]](file/bi-common/registry/nacos/loader.go) - [[watcher.go]](file/bi-common/registry/nacos/watcher.go) - [[option.go]](file/bi-common/registry/nacos/option.go) - [[env.go]](file/bi-common/registry/nacos/env.go) - [[nacos-default.yaml]](file/bi-common/registry/nacos/nacos-default.yaml) - [[nacos-dev.yaml]](file/bi-cron/configs/nacos-dev.yaml) - [[nacos-prod.yaml]](file/bi-cron/configs/nacos-prod.yaml) - [[nacos-config.yaml]](file/bi-chat-asktable/configs/nacos-config.yaml) - [[nacos.go (bi-basic)]](file/bi-basic/internal/server/nacos.go) - [[nacos.go (bi-notify)]](file/bi-notify/internal/server/nacos.go) - [[nacos.go (bi-sys)]](file/bi-sys/internal/server/nacos.go)

Table of Contents

  1. Introduction
  2. Project Structure
  3. Core Components
  4. Architecture Overview
  5. Detailed Component Analysis
  6. Dependency Analysis
  7. Performance Considerations
  8. Troubleshooting Guide
  9. Conclusion
  10. Appendices

Introduction

This document explains how the project implements centralized configuration management and service discovery using Nacos. It covers dynamic configuration reloading, environment-specific settings, configuration validation, service registration and discovery, health checks, automatic failover, load balancing, service instance management, graceful shutdown, configuration versioning and rollback, security considerations for sensitive data, and monitoring integration. Concrete examples illustrate service startup sequences, configuration injection patterns, and operational guidance for distributed systems under network partitions and availability constraints.

Project Structure

The Nacos integration is primarily implemented in the bi-common module under registry/nacos, with usage demonstrated across several services:

  • Centralized Nacos client, configuration loader, and registry adapter
  • Environment-specific configuration overlays for development and production
  • Example configuration payload stored in YAML for a microservice
  • Service adapters that integrate Nacos with Kratos registry interfaces

Diagram sources

Section sources

Core Components

  • Nacos Client: Provides unified access to both configuration center and service discovery via lazily initialized clients. Supports publishing/deleting/listening to configurations and registering/deregistering service instances.
  • Configuration Loader: Implements Kratos config.Source and Watcher to fetch remote configuration and receive live updates across one or multiple DataIDs.
  • Service Discovery: Offers convenience methods for registering/deregistering instances, discovering healthy/all instances, selecting one healthy instance for load balancing, subscribing to service topology changes, and listing all services.
  • Registry Adapter: Bridges Nacos with Kratos registry interfaces, enabling seamless integration with Kratos servers and clients.
  • Options and Defaults: Comprehensive configuration builder with embedded defaults, environment variable overrides, and validation.

Key capabilities:

  • Dynamic configuration reloading via Kratos Watcher
  • Environment-specific settings via overlay files
  • Validation of essential connection parameters
  • Health-aware instance selection and subscription
  • Graceful shutdown via watcher cancellation and deregistration

Section sources

Architecture Overview

The system integrates Nacos across two primary domains: configuration management and service discovery.

Diagram sources

Detailed Component Analysis

Centralized Configuration Management

  • Configuration source creation: The loader exposes NewConfigSourceWithOptions to construct a Kratos config.Source backed by Nacos, supporting single or multiple DataIDs.
  • Loading and scanning: LoadConfig loads remote configuration and scans into a target struct, while MustLoadConfig panics on failure.
  • Watching and reloading: Watch registers listeners per DataID and emits changes through a channel; Next returns updated key-value pairs ready for consumption by Kratos.
  • Environment-specific overlays: Environment files (e.g., dev/prod) override defaults and set DataIDs for the target service.
  • Example payload: A sample YAML configuration demonstrates how service settings are modeled and stored in Nacos.

Diagram sources

Practical usage patterns:

  • Startup sequence: Build a Nacos client with environment-specific options, create a Kratos config.Source, load initial configuration, and start watching for changes.
  • Configuration injection: Use LoadConfig to populate typed configuration structs during application initialization.

Section sources

Service Registration and Discovery

  • Registration/Deregistration: Convenience methods register or remove ephemeral instances with metadata, weight, and cluster assignment.
  • Discovery: Retrieve healthy or all instances, select one healthy instance for load balancing, and subscribe to topology changes.
  • Registry integration: Adapters expose Kratos registry interfaces for discovery and registration, mapping Nacos instances to Kratos service instances.

Diagram sources

Operational highlights:

  • Health checks: Discovery filters unhealthy instances by default; Subscribe callbacks surface topology changes.
  • Automatic failover: Clients select one healthy instance; watchers refresh lists upon changes.
  • Load balancing: SelectOneHealthyInstance provides a simple strategy; higher-level clients can implement weighted or round-robin policies.

Section sources

Configuration Validation and Environment-Specific Settings

  • Validation: The configuration validates server address, port, and timeout to prevent misconfiguration.
  • Environment overrides: Environment variables can override all fields, with legacy keys supported for backward compatibility.
  • Embedded defaults: A YAML file defines sane defaults for local development and CI.

Diagram sources

Section sources

Graceful Shutdown Procedures

  • Watcher lifecycle: Watcher supports Stop/Close to cancel listening and unsubscribe from Nacos.
  • Service deregistration: Services should deregister instances on shutdown to prevent routing to unavailable endpoints.
  • Client lifecycle: The client initializes clients lazily; ensure proper teardown of watchers and naming subscriptions.

Diagram sources

Section sources

Configuration Versioning, Rollback, and Security

  • Versioning and metadata: Service instances can carry metadata including version; clients can use this to route requests to compatible versions.
  • Rollback: While the loader does not implement rollback, Nacos supports publishing older content to DataIDs; applications can reload to revert to previous configuration sets.
  • Security: Authentication credentials can be supplied via configuration or environment variables; avoid hardcoding secrets and prefer secure secret stores.

Diagram sources

Section sources

Monitoring Integration

  • Logging: Configure log level and directories via client settings to capture client behavior and errors.
  • Observability: Combine Nacos watcher events with application metrics to track configuration reload latency and success rates.

Section sources

Dependency Analysis

The Nacos integration is cohesive within bi-common and consumed by multiple services. The loader depends on the client and Kratos config interfaces; the watcher depends on the naming client; discovery depends on the naming client; options and environment utilities support configuration construction.

Diagram sources

Section sources

Performance Considerations

  • Client configuration: Tune BeatInterval, TimeoutMs, and thread counts for high-throughput scenarios.
  • Caching: Adjust NotLoadCacheAtStart, UpdateCacheWhenEmpty, and DisableUseSnapShot according to consistency needs.
  • Watcher throughput: Use buffered channels and efficient parsing to handle frequent updates.

[No sources needed since this section provides general guidance]

Troubleshooting Guide

Common issues and resolutions:

  • Invalid server address or port: Ensure Validate passes; check environment overrides.
  • Authentication failures: Verify username/password or access key/secret key.
  • No healthy instances: Confirm service registration and health checks; inspect Subscribe callbacks.
  • Watcher not receiving updates: Confirm DataIDs and group match; ensure listeners are registered for each DataID.

Section sources

Conclusion

The project’s Nacos integration provides a robust foundation for configuration management and service discovery. It supports dynamic reloading, environment-specific overlays, validation, health-aware discovery, and graceful shutdown. With metadata-driven versioning and secure credential handling, it scales to multi-environment deployments while maintaining operational simplicity.

[No sources needed since this section summarizes without analyzing specific files]

Appendices

Example: Service Startup Sequence

  • Build Nacos client with environment-specific options and optional file overlays.
  • Create Kratos config.Source and load initial configuration.
  • Start watching for configuration changes.
  • Register service instance with metadata and cluster.
  • Use discovery to resolve upstream services and implement load balancing.
  • On shutdown, deregister instance and stop watchers.

Section sources

Distributed System Guidance

  • Network partitions: Use Subscribe callbacks and periodic SelectOneHealthyInstance to adapt to transient unavailability.
  • Availability: Maintain multiple clusters and weights; leverage metadata to route to newer versions gradually.
  • Consistency: Prefer Watcher-based reloads for near-real-time updates; batch reloads for stability.

[No sources needed since this section provides general guidance]