Consul Configuration Center Example
Description
This directory contains an example demonstrating how to integrate Consul configuration center with GoFrame applications. It shows:
ConsulClient ConfigurationConsulclient setup and initialization- Configuration adapter implementation
- Error handling and logging
Configuration Management
- Configuration loading and parsing
- Dynamic configuration updates
- Configuration value retrieval
Directory Structure
.
├── boot/ # Bootstrap configuration
│ └── boot.go # Consul client initialization
├── main.go # Main application entry
├── go.mod # Go module file
└── go.sum # Go module checksumsRequirements
- Go
1.22or higher - Git
- GoFrame
- GoFrame Consul Config
Features
The example showcases the following features:
ConsulIntegration- Client configuration
- Connection management
- Key-Value store access
- Error handling
Configuration Management
- Configuration loading
- Value retrieval
- Type conversion
- Default values
Dynamic Updates
- Configuration watching
- Change notification
- Hot reload support
Configuration
Consul Server
Server Configuration:
- Default port: 8500
- Default datacenter: dc1
- Default scheme: http
Authentication:
- Token-based authentication
- ACL system support
- Datacenter level isolation
Client Configuration
Basic Settings:
- Address:
Consulserver address - Scheme: HTTP/HTTPS
- Datacenter: Configuration datacenter
- Token: Access token
- Address:
Advanced Options:
- Path customization
- Watch configuration
- Transport settings
- Retry options
Usage
Start
ConsulServer:# Start Consul server using Docker docker run -d --name consul \ -p 8500:8500 -p 8600:8600/udp \ hashicorp/consulConfigure
Consul:- Access
ConsulUI at http://localhost:8500 - Create Key-Value pairs
- Set up ACL tokens if needed
- Access
Run Example:
go run main.go
Implementation Details
Client Setup (
boot/boot.go):- Consul client configuration
- Transport layer setup
- Authentication configuration
- Error handling
Configuration Access (
main.go):- Configuration availability check
- Bulk configuration retrieval
- Single value access
- Error handling
Notes
- The example uses Consul's Key-Value store for configuration management
- Configuration changes in Consul are automatically reflected in the application
- The client supports secure connections via HTTPS
- ACL tokens can be used for secure access
- Configuration paths can be customized based on needs