springboot集成nacos 配置中心
nacos本机需要安装好,未安装时,参考
- 创建一个springboot myapi 项目,使用maven进行依赖包管理,创建两个模块nacosconfig(配置中心)、nacosregister,(注册中心),本方主要介绍配置中心,目录 结构如下:
- 父pom.xml文件如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <modules> <module>nacosconfig</module> <module>nacosregister</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>org.howdy</groupId> <artifactId>myapi</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <name>myapi</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- <dependency>--> <!-- <groupId>org.projectlombok</groupId>--> <!-- <artifactId>lombok</artifactId>--> <!-- <scope>provided</scope>--> <!-- </dependency>--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
- 子模块pom.xml配置信息如下
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 <parent> 4 <artifactId>myapi</artifactId> 5 <groupId>org.howdy</groupId> 6 <version>1.0-SNAPSHOT</version> 7 </parent> 8 <modelVersion>4.0.0</modelVersion> 9 10 <artifactId>nacosconfig</artifactId> 11 <packaging>jar</packaging> 12 13 <name>nacosconfig</name> 14 <url>http://maven.apache.org</url> 15 16 <properties> 17 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 18 </properties> 19 20 <dependencies> 21 <dependency> 22 <groupId>com.alibaba.cloud</groupId> 23 <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> 24 <version>2.2.5.RELEASE</version> 25 </dependency> 26 27 <!-- https://mvnrepository.com/artifact/com.alibaba.nacos/nacos-spring-context --> 28 <dependency> 29 <groupId>com.alibaba.nacos</groupId> 30 <!--NacosPropertySource 注解需要的包--> 31 <artifactId>nacos-spring-context</artifactId> 32 <version>0.3.6</version> 33 </dependency> 34 35 <dependency> 36 <groupId>org.springframework.cloud</groupId> 37 <artifactId>spring-cloud-starter-bootstrap</artifactId> 38 <version>3.1.3</version> 39 </dependency> 40 41 <dependency> 42 <groupId>junit</groupId> 43 <artifactId>junit</artifactId> 44 <version>3.8.1</version> 45 <scope>test</scope> 46 </dependency> 47 </dependencies> 48 </project>
- 创建bootstrap.yml文件,bootstrap文件优先级高于application.yml文件,bootstrap.yml文件配置如下
server: port: 8090 #启动端口号 命令行注入 spring: application: name: mytask-dev cloud: nacos: config: server-addr: 127.0.0.1:8848 #配置中心 file-extension: yaml #配置文件后缀名 dataId = application.name + file-extension namespace: public #开发环境 username: nacos password: nacos group: DEFAULT_GROUP
- 在nacos配置管理,添加mytask-dev.yaml配置,如下
nacos: dev: propertie: 2
- 创建TestController类,测试从配置文件获取信息
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package org.howdy.controller; 2 3 import org.springframework.beans.factory.annotation.Value; 4 import org.springframework.cloud.context.config.annotation.RefreshScope; 5 import org.springframework.web.bind.annotation.GetMapping; 6 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework.web.bind.annotation.RestController; 8 9 @RestController 10 @RequestMapping(value = "test") 11 @RefreshScope //实现动态刷新配置 12 public class TestController { 13 14 //@NacosValue(value = "${nacos.dev.propertie:222}",autoRefreshed = true) 15 @Value("${nacos.dev.propertie}") 16 private String getPropertie; 17 18 @GetMapping("/client") 19 public String client() { 20 return "我是从nacos获取的值:" + getPropertie; 21 } 22 }
- 启动类main函数信息如下
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package org.howdy; 2 3 /** 4 * Hello world! 5 * 6 */ 7 import org.springframework.boot.SpringApplication; 8 import org.springframework.boot.autoconfigure.SpringBootApplication; 9 10 11 /** 12 * Hello world! 13 * 14 */ 15 @SpringBootApplication 16 public class SpringbootNacosConfigApplication 17 { 18 public static void main( String[] args ) 19 { 20 System.out.println( "Hello SpringbootNacosConfigApplication!" ); 21 SpringApplication.run(SpringbootNacosConfigApplication.class, args); 22 } 23 }
- 测试,在浏览器中输入http://localhost:8090/test/client
- 从配置中心,将值改为3后,发布配置,此时不重启服务,从浏览器再在刷新获取后,我们得到的是最新值