nacos配置中心的应用

nacos配置中心的使用:
1.环境准备:安装好nacos服务并且可以正常启动

2.引入依赖:

		<dependency>
			<groupId>com.alibaba.boot</groupId>
			<artifactId>nacos-config-spring-boot-starter</artifactId>
			<version>0.2.1</version>

3.配置文件:配置地址和namespace

server:
  port: 38080

task:
  enabled: 1

spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.jdbc.Driver

mybatis:
  mapper-locations: classpath:mapping/*Mapper.xml
  type-aliases-package: com.mengan.utilsProject.task
  configuration:
    map-underscore-to-camel-case: true
#showSql
logging:
  level:
    com:
      example:
        mapper : debug

nacos:
  config:
    server-addr: 127.0.0.1:8848
    namespace: dcf423ec-e78c-4a9e-b7e9-aaa16fcd4acc

#dev: 123

4.springboot项目入口读取nacos:

package com.mengan.utilsProject;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
@NacosPropertySource(dataId = "prod.yaml", autoRefreshed = true, groupId = "DEFAULT_GROUP")
@NacosPropertySource(dataId = "dev", autoRefreshed = true, groupId = "DEFAULT_GROUP")
public class UtilsProjectApplication {


    public static void main(String[] args) {
        SpringApplication.run(UtilsProjectApplication.class, args);
    }

    @NacosValue(value = "${dev}", autoRefreshed = true)
//    @Value("${dev}")
    private String testProperties;

    @GetMapping("/test")
    public String test() {
        return testProperties;
    }


}
//@MapperScan("com.mengan.utilsProject.task")

5.nacos中界面配置:
在这里插入图片描述
6.访问结果:
在这里插入图片描述
7.修改nacos中的值,不用重启springboot项目即可实现动态读取值。
在这里插入图片描述
再次访问:
在这里插入图片描述
至此实现了nacos中的配置中心的demo。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值