nacos配置中心的应用

nacos配置中心的应用,第1张

nacos配置中心的应用

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

2.引入依赖:

		
			com.alibaba.boot
			nacos-config-spring-boot-starter
			0.2.1

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。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5717284.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存