Installation and Startup
Download the Nacos binary from the official GitHub releases page and extract it to a convenient location. Inside the extracted directory, conf/application.properties holds default settings, such as the web port (default 8848) wich typically does not need to be changed.
Navigate to the bin folder, open a terminal, and run:
startup.cmd -m standalone
Once the server starts, access the console at http://localhost:8848/nacos.
Service Registration
Add the Nacos discovery starter to your project:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
Specify the application name and Nacos address in your application.yml:
spring:
application:
name: orderserver
cloud:
nacos:
server-addr: localhost:8848
discovery:
cluster-name: HZ
Start the service, and it will appear under the service list in the Nacos dashboard.
Dynamic Configuration with Hot Reload
To enable configuration management, include the config starter:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
Create bootstrap.yml (loaded before application.yml) with the Nacos connection details and1671 file extension:
spring:
application:
name: userservice
profiles:
active: dev
cloud:
nacos:
server-addr: localhost:8848
config:
file-extension: yaml
The data id in Nacos follows the pattern <service>-<profile>.<extension>. For the dev profile, it is userservice-dev.yaml.
acción In the Nacos console, go to Configuration Management > Configurations, create a new configuration with that data id, select YAML format, and add the content:
pattern:
formatPattern: yyyy/MM/dd HH:mm:ss
In your codde, define a configuration properties class:
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "pattern")
@Data
@Component
public class PatternConfig {
private String formatPattern;
}
Inject it wherever needed to use the format:
@Autowired
private PatternConfig dateConfig;
@GetMapping("/now")
public String getNow() {
return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateConfig.getFormatPattern()));
}
When you change pattern.formatPattern in the Nacos console, the service reflects the new value instantly without restarting.