首页 > 其他分享 >springboot集成nacos 配置中心

springboot集成nacos 配置中心

时间:2023-01-15 22:11:06浏览次数:56  
标签:集成 springboot spring nacos springframework import org cloud

  

  nacos本机需要安装好,未安装时,参考

  • 创建一个springboot myapi 项目,使用maven进行依赖包管理,创建两个模块nacosconfig(配置中心)、nacosregister,(注册中心),本方主要介绍配置中心,目录 结构如下:

  • 父pom.xml文件如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <modules>
        <module>nacosconfig</module>
        <module>nacosregister</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>org.howdy</groupId>
    <artifactId>myapi</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>myapi</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

<!--        <dependency>-->
<!--            <groupId>org.projectlombok</groupId>-->
<!--            <artifactId>lombok</artifactId>-->
<!--            <scope>provided</scope>-->
<!--        </dependency>-->

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
  • 子模块pom.xml配置信息如下
 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3     <parent>
 4         <artifactId>myapi</artifactId>
 5         <groupId>org.howdy</groupId>
 6         <version>1.0-SNAPSHOT</version>
 7     </parent>
 8     <modelVersion>4.0.0</modelVersion>
 9 
10     <artifactId>nacosconfig</artifactId>
11     <packaging>jar</packaging>
12 
13     <name>nacosconfig</name>
14     <url>http://maven.apache.org</url>
15 
16     <properties>
17         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18     </properties>
19 
20     <dependencies>
21         <dependency>
22             <groupId>com.alibaba.cloud</groupId>
23             <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
24             <version>2.2.5.RELEASE</version>
25         </dependency>
26 
27         <!-- https://mvnrepository.com/artifact/com.alibaba.nacos/nacos-spring-context -->
28         <dependency>
29             <groupId>com.alibaba.nacos</groupId>
30             <!--NacosPropertySource 注解需要的包-->
31             <artifactId>nacos-spring-context</artifactId>
32             <version>0.3.6</version>
33         </dependency>
34 
35         <dependency>
36             <groupId>org.springframework.cloud</groupId>
37             <artifactId>spring-cloud-starter-bootstrap</artifactId>
38             <version>3.1.3</version>
39         </dependency>
40 
41         <dependency>
42             <groupId>junit</groupId>
43             <artifactId>junit</artifactId>
44             <version>3.8.1</version>
45             <scope>test</scope>
46         </dependency>
47     </dependencies>
48 </project>
View Code
  • 创建bootstrap.yml文件,bootstrap文件优先级高于application.yml文件,bootstrap.yml文件配置如下
server:
  port: 8090   #启动端口号  命令行注入
spring:
  application:
    name: mytask-dev
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848  #配置中心
        file-extension: yaml   #配置文件后缀名   dataId = application.name  +  file-extension
        namespace: public  #开发环境
        username: nacos
        password: nacos
        group: DEFAULT_GROUP
  • 在nacos配置管理,添加mytask-dev.yaml配置,如下

nacos:
  dev:
   propertie: 2
  • 创建TestController类,测试从配置文件获取信息
 1 package org.howdy.controller;
 2 
 3 import org.springframework.beans.factory.annotation.Value;
 4 import org.springframework.cloud.context.config.annotation.RefreshScope;
 5 import org.springframework.web.bind.annotation.GetMapping;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.RestController;
 8 
 9 @RestController
10 @RequestMapping(value = "test")
11 @RefreshScope //实现动态刷新配置
12 public class TestController {
13 
14     //@NacosValue(value = "${nacos.dev.propertie:222}",autoRefreshed = true)
15     @Value("${nacos.dev.propertie}")
16     private String getPropertie;
17 
18     @GetMapping("/client")
19     public String client() {
20         return "我是从nacos获取的值:" + getPropertie;
21     }
22 }
View Code
  • 启动类main函数信息如下
 1 package org.howdy;
 2 
 3 /**
 4  * Hello world!
 5  *
 6  */
 7 import org.springframework.boot.SpringApplication;
 8 import org.springframework.boot.autoconfigure.SpringBootApplication;
 9 
10 
11 /**
12  * Hello world!
13  *
14  */
15 @SpringBootApplication
16 public class SpringbootNacosConfigApplication
17 {
18     public static void main( String[] args )
19     {
20         System.out.println( "Hello SpringbootNacosConfigApplication!" );
21         SpringApplication.run(SpringbootNacosConfigApplication.class, args);
22     }
23 }
View Code

  • 从配置中心,将值改为3后,发布配置,此时不重启服务,从浏览器再在刷新获取后,我们得到的是最新值

 

标签:集成,springboot,spring,nacos,springframework,import,org,cloud
From: https://www.cnblogs.com/personblog/p/17054196.html

相关文章

  • windows下Nacos安装
        官网地址:http://nacos.io/zh-cn/index.htmlNacos(DynamicNamingandConfigurationService),Nacos是一个以服务为一等公民的注册、配置和管理中......
  • 230115_50_SpringBoot入门
    如果类中属性比较多,通过@value赋值比较麻烦。可以通过yaml配置文件给实例赋值。新建Person类,通过@ConfigurationProperties注解可以实现配置文件注入,其中prefix可以指......
  • springboot url中获取所有RequestMapping的URL路径列表集
    springboot项目在做URL权限控制的时候需要获取全部的URL,一个一个去controller中找费时费力,有的权限点的命名和URL有一定的对应关系。如果能用程序获得全部URL,将会省去很......
  • Spring Boot---(11)SpringBoot使用Junit单元测试
    摘要:本文详细的记录了SpringBoot如何结合Junit写测试用例,如何执行,打包执行,忽略执行等操作,SpringBoot内置了Junit测试组件,使用很方便,不用再单独引入其他测试组件。 演示环境......
  • SpringBoot完成SSM整合之SpringBoot整合junit
    SpringBoot​​......
  • nacos读配置的那些坑
    废话不多说直接描述问题nacos打war包无法读取配问题当你打成war时无法读取配置,但是你在启动nacos的时候又会把网页上面的配置打印出来但是你的程序就是没法读取经过几......
  • springboot2.3.x版本发生异常时,响应的message和exception为空问题
    原因:因为boot2.3.x版本可能考虑信息安全问题,把以下两个值默认为server:error:include-message:neverinclude-exception:false发生异常是返回{"ti......
  • AIR32F103(八) 集成Helix MP3解码库播放MP3
    目录AIR32F103(一)合宙AIR32F103CBT6开发板上手报告AIR32F103(二)Linux环境和LibOpenCM3项目模板AIR32F103(三)Linux环境基于标准外设库的项目模板AIR32F103(四)2......
  • Shiro+SpringBoot+Mybatis+Redis实现权限系统
     这个项目涉及到的技术:SpringBoot,Thymeleaf,MyBaits,Redis,Shiro,JavaMail,EasyUI,Excel导入、导出 下面展示一下界面: 主页:  用户列表:  角色权限授予: 资源列表:  用户角......
  • 初识Nacos 配置中心
    初识Nacos配置中心文章目录​​文档​​​​依赖​​​​Properties类型​​​​创建配置​​​​配置文件​​​​程序​​​​User​​​​UserService​​​​UserContr......