安装Nacos
参照Nacos官网操作指南:https://nacos.io/zh-cn/docs/next/v2/quickstart/quick-start
本示例使用的版本:NACOS2.1.2
SpringBoot1.5.12集成Nacos
在应用pom.xml中添加nacos依赖(注意:版本 0.2.x.RELEASE 对应的是 Spring Boot 2.x 版本,版本 0.1.x.RELEASE 对应的是 Spring Boot 1.x 版本。)
<dependencies> ... <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>nacos-config-spring-boot-starter</artifactId> <version>0.1.7</version> </dependency> ... </dependencies>
修改application.yml配置
nacos: dataId: qhd_www.yaml groupId: dev config: # nacos服务地址、账号密码、命名空间 server-addr: 172.30.4.231:8848 username: nacos password: Neuqsoft@2024 namespace: 96b055d5-7913-4617-acf9-64def79853ec
应用启动类增加@NacosPropertySource注解
package com.jiangk; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer; import org.springframework.context.annotation.ImportResource; import com.alibaba.nacos.api.annotation.NacosProperties; import com.alibaba.nacos.api.config.ConfigType; import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource; @SpringBootApplication @ImportResource({"classpath*:/spring/*.xml"}) @NacosPropertySource(dataId = "${nacos.dataId}",groupId="${nacos.groupId}",autoRefreshed=true,type=ConfigType.YAML, properties = @NacosProperties( username="${nacos.config.username}", password="${nacos.config.password}", serverAddr="${nacos.config.server-addr}", namespace="${nacos.config.namespace}" )) public class Application extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
Nacos管理后台增加命名空间
Nacos管理后台增加配置项
启动应用
...... ... 2024-08-05 13:38:20,014 TRACE --- [main] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [VARCHAR] - [tptz_tpdd] 2024-08-05 13:38:20,014 TRACE --- [main] o.h.type.descriptor.sql.BasicBinder : binding parameter [2] as [INTEGER] - [1] 2024-08-05 13:38:20,053 INFO --- [main] c.n.q.c.authsystem.StartupRunner : 数据字典 加载成功 ...... 2024-08-05 13:38:20,055 INFO --- [main] com.neuqsoft.Application : Started Application in 21.422 seconds (JVM running for 23.495)