其他分享
首页 > 其他分享> > 第 4 篇 : SpringBoot整合Nacos1.4.2

第 4 篇 : SpringBoot整合Nacos1.4.2

作者:互联网

1. 在redis项目中,增加nacos依赖,刷新maven

官网给出的版本注意说明
注意 : 版本 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>

2. 在application.yml文件中添加nacos配置

nacos:
  config:
    server-addr: 192.168.109.160:8848
    auto-refresh: true

3. 在启动类上nacos注解

@NacosPropertySource(dataId = "redis_nacos", groupId ="hahashou", autoRefreshed = true, type = ConfigType.YAML)

4. 新增TestNacosController,并启动项目

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();
    }
}

5. 测试

5.1 获取配置信息

执行请求
结果
执行结果

5.2 修改nacos配置,动态获取信息

动态结果

标签:Nacos1.4,版本,SpringBoot,nacosFirst,nacos,整合,import,true,annotation
来源: https://blog.csdn.net/qiezi__/article/details/121474438