Nacos Configuration Center
A configuration center is used for centralized management of configuration information for different environments and services, effectively solving the problem of scattered management of traditional static configurations. Services created by sponge support integration with the Nacos configuration center.
Configuration Steps
Code Integration
To integrate the Nacos configuration center into your Web or gRPC service, please copy the initialization code to the project directory:cmd/xxx/initial/initApp.go
Configuration File Preparation
Create the configuration fileconfigs/xxx_cc.yml
and fill in the Nacos connection information:# nacos settings nacos: ipAddr: "127.0.0.1" # ip port: 8848 # port scheme: "http" # http or https contextPath: "/nacos" # path namespaceID: "your name space id" group: "dev" # group name, e.g., dev, prod, test dataID: "user.yml" # configuration file id format: "yaml" # configuration file type json,yaml,toml
Local Nacos Service Startup
- Start the local Nacos service using the Nacos configuration provided by sponge
- Access the management console: http://localhost:8848/nacos/index.html
- Log in using the default username and password (nacos/nacos)
Configuration Example
In the Nacos console:
- Create namespace
user
- Create new configuration:
- Data ID:
user.yml
- Group:
dev
- Content: Copy the content of the
configs/user.yml
file
- Data ID:
Nacos Configuration Example - Create namespace
Modify project configuration:
- Edit the
configs/user_cc.yml
file and fill in the correct Nacos connection information
- Edit the
Service Startup
# Navigate to the project directory
cd cmd/user
# Compile the project
go build
# Start the service (using configuration center)
./user -enable-cc -c=../../configs/user_cc.yml
Deployment Notes
When deploying with Docker or Kubernetes, simply replace the default startup command with the configuration center startup method. The deployment scripts generated by sponge already include both startup options, which you can choose according to your needs.