第 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配置,动态获取信息

动态结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哈哈兽0026

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值