官网给出的版本注意说明
注意 : 版本 0.2.x.RELEASE 对应的是 Spring Boot 2.x 版本,版本 0.1.x.RELEASE 对应的是 Spring Boot 1.x 版本
<!-- Nacos配置中心 --> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>nacos-config-spring-boot-starter</artifactId> <version>0.2.3</version> </dependency>
nacos: config: server-addr: 192.168.109.160:8848 auto-refresh: true
@NacosPropertySource(dataId = "redis_nacos", groupId ="hahashou", autoRefreshed = true, type = ConfigType.YAML)
package com.hahashou.test.redis.controller; import com.alibaba.nacos.api.config.annotation.NacosValue; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @description: 测试Nacos * @author: 哼唧兽 * @date: 9999/9/21 **/ @RestController @RequestMapping("/nacos") @Api(tags = "测试Nacos") @Slf4j public class TestNacosController { @NacosValue(value = "${nacos.first}",autoRefreshed = true) private Integer nacosFirst; @NacosValue(value = "${nacos.second}",autoRefreshed = true) private String nacosSecond; @GetMapping("/nacosValue") @ApiOperation(value = "获取Nacos配置中的值") public String nacosValue() { StringBuilder result = new StringBuilder("nacosFirst的值 : "+ nacosFirst); result.append(" ; nacosSecond的值 : "+ nacosSecond); return result.toString(); } }
结果