Skip to content

Service Discovery (Nacos)

**Referenced Files in This Document** - [[README.md]](file/bi-common/registry/nacos/readme.md) - [[client.go]](file/bi-common/registry/nacos/client.go) - [[config.go]](file/bi-common/registry/nacos/config.go) - [[option.go]](file/bi-common/registry/nacos/option.go) - [[loader.go]](file/bi-common/registry/nacos/loader.go) - [[discovery.go]](file/bi-common/registry/nacos/discovery.go) - [[registry.go]](file/bi-common/registry/nacos/registry.go) - [[service_option.go]](file/bi-common/registry/nacos/service-option.go) - [[watcher.go]](file/bi-common/registry/nacos/watcher.go) - [[env.go]](file/bi-common/registry/nacos/env.go) - [[util.go]](file/bi-common/registry/nacos/util.go) - [[nacos-default.yaml]](file/bi-common/registry/nacos/nacos-default.yaml) - [[basic/main.go]](file/bi-common/registry/nacos/examples/basic/main.go) - [[development.yaml]](file/bi-common/registry/nacos/examples/config-files/development.yaml) - [[nacos-dev.yaml]](file/bi-cron/configs/nacos-dev.yaml) - [[nacos-local.yaml]](file/bi-cron/configs/nacos-local.yaml) - [[nacos-prod.yaml]](file/bi-cron/configs/nacos-prod.yaml)

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 Nacos-based service discovery and configuration management. It covers service registration, health checks, load balancing, dynamic configuration loading, service instance management, circuit breaker patterns, graceful shutdown procedures, configuration file management, environment-specific settings, hot-reloading capabilities, service dependency mapping, namespace isolation, security policies, monitoring integration, failure scenarios, and recovery procedures.

Project Structure

The Nacos integration lives under bi-common/registry/nacos and provides:

  • Client and configuration management for Nacos SDK v2
  • Kratos-compatible configuration source and watcher
  • Service discovery and registry adapters for Kratos
  • Environment variable and file-based configuration loading
  • Examples and environment-specific configuration files

Diagram sources

Section sources

Core Components

  • Client: wraps Nacos SDK clients (config and naming), supports lazy initialization, validation, and environment overrides.
  • Config: structured configuration with server, client, config, auth, and advanced settings; merges defaults, file, and environment.
  • Loader: Kratos config.Source and Watcher for single or multiple DataIDs with hot-reload support.
  • Discovery: convenience methods for registering, discovering, subscribing to service instances; plus Kratos Registry adapter.
  • Options: functional options to override defaults and environment variables.
  • Environment: loads configuration from environment variables with legacy compatibility.
  • Utilities: automatic DataID inference from go.mod, YAML/JSON parsing helpers.

Section sources

Architecture Overview

The system integrates Nacos as both configuration center and service registry:

  • Configuration: Kratos config.Source loads one or more DataIDs, watches for changes, and triggers hot-reload.
  • Service Discovery: Kratos Registry registers service instances and discovers healthy endpoints; optional watcher notifies of topology changes.

Diagram sources

Diagram sources

Detailed Component Analysis

Configuration Management

  • DataID resolution: If not set via options or file, automatically derives DataID from the caller’s go.mod module name.
  • Multiple DataIDs: Single ConfigSource can load multiple DataIDs; Kratos merges them with later sources overriding earlier ones.
  • Hot reload: Watcher listens to each DataID and emits changes; Kratos triggers reconfiguration.
  • Environment precedence: Environment variables override options and file-based configuration.

Diagram sources

Section sources

Service Registration and Discovery

  • Registration: Accepts endpoints, parses host/port, sets metadata (kind/version), and registers per endpoint with a service name pattern that includes transport scheme.
  • Health filtering: Discovery returns only healthy and enabled instances.
  • Load balancing: SelectOneHealthyInstance chooses a single instance for outbound calls.
  • Subscription: Watcher subscribes to changes and exposes Next() to receive updates.

Diagram sources

Section sources

Circuit Breaker Patterns and Graceful Shutdown

  • Circuit breaker: Implement via retries, timeouts, and health-aware routing. Use SelectOneHealthyInstance to avoid unhealthy nodes; combine with backoff and jitter in callers.
  • Graceful shutdown: Deregister service instances during shutdown to prevent routing to dead endpoints. Ensure Watchers are stopped to release subscriptions.

[No sources needed since this section provides general guidance]

Monitoring Integration

  • Metrics: Integrate with Kratos metrics and logging; monitor Nacos client logs and cache directories configured via environment.
  • Observability: Expose health endpoints and use Watchers to detect topology changes for alerting.

[No sources needed since this section provides general guidance]

Security Policies

  • Authentication: Support for basic auth and Alibaba Cloud AK/SK via environment variables and options.
  • TLS: Configure scheme and ports; ensure secure contexts for production.

Section sources

Environment-Specific Settings and Hot-Reloading

  • Environments: Separate configuration files for local, dev, and prod namespaces/groups; data_ids can be lists for modular configuration.
  • Hot-reload: Watcher streams changes per DataID; Kratos merges and applies updates.

Section sources

Service Dependency Mapping and Namespace Isolation

  • Namespaces: Use separate namespace_id per environment to isolate configurations and services.
  • Groups: Align groups across configuration and service discovery to simplify cross-cutting management.

Section sources

Dependency Analysis

  • Kratos integration: Loader implements config.Source and Watcher; Registry implements Kratos registry interfaces.
  • Lazy initialization: Config and Naming clients are created on first use.
  • Validation: Validates host, port, and timeout before creating clients.

Diagram sources

Section sources

Performance Considerations

  • Lazy clients: Avoids unnecessary connections until needed.
  • Thread safety: All operations are thread-safe; use shared clients across goroutines.
  • Watchers: Efficiently multiplex multiple DataIDs with a single watcher per source.
  • Heartbeats: Tune BeatInterval to balance liveness detection and overhead.

[No sources needed since this section provides general guidance]

Troubleshooting Guide

  • Configuration not applied: Verify environment variable precedence and file loading; confirm DataID inference matches your module name.
  • No instances found: Ensure services are registered with correct group/cluster and marked healthy.
  • Watcher not triggering: Confirm ListenConfig registrations per DataID and group match.
  • Authentication failures: Check username/password or AK/SK configuration.

Section sources

Conclusion

The Nacos integration provides a robust, zero-configuration baseline with flexible overrides for environments, seamless Kratos integration for configuration and service discovery, and practical utilities for auto-configuration and hot-reload. By leveraging namespaces, groups, and watchers, teams can achieve reliable service mesh-like behavior with minimal boilerplate.

Appendices

Quick Start References

  • Zero-config usage, environment variables, function options, and Kratos integration are documented in the component README.
  • Example usage and environment files demonstrate practical setups.

Section sources