ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

nacos配置中心的应用

2022-01-29 10:00:33  阅读:122  来源: 互联网

标签:com 配置 boot nacos springframework 应用 import true


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。

标签:com,配置,boot,nacos,springframework,应用,import,true
来源: https://blog.csdn.net/upordown6263/article/details/122739550

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有