SpringBoot Nacos 安装、注解开发配置自动加载更新并监听

SpringBoot Nacos 安装及配置入门Demo

基于注解

  • 安装
  1. 下载tar包

github https://github.com/alibaba/nacos/releases/tag/1.4.0 不过下载的有时候很慢

csdn https://download.csdn.net/download/HumorChen99/13344131 我发的,免费下

  1. 解压即可,在bin目录有startup.sh启动
#进入bin目录并以独立模式启动
./startup.sh -m standalone
  1. 在浏览器访问 http://ip:8848/nacos 账号密码默认都为nacos
  2. 添加一个测试配置 systemSwtich值设置为true
  • 建立springboot项目

  • 依赖

    <?xml version="1.0" encoding="UTF-8"?>
    <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.2.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>springboot_nacos_demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>springboot_nacos_demo</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>com.alibaba.boot</groupId>
                <artifactId>nacos-config-spring-boot-starter</artifactId>
                <version>0.2.7</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    
  • application.yml

    nacos:
      config:
        server-addr: 192.168.108.129:8848
    server:
      port: 80
    
  • controller代码

autoRefreshed参数为是否自动刷新,开启后一旦配置更新即可同步更新
NacosValue(value = “${systemSwitch:false}”,autoRefreshed = true)
systemSwitch:false 配置:默认值,也就是查不到的时候返回这个默认的

  package com.example.springboot_nacos_demo.controller;
  
  import com.alibaba.nacos.api.config.ConfigType;
  import com.alibaba.nacos.api.config.annotation.NacosValue;
  import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
  import org.springframework.web.bind.annotation.GetMapping;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RestController;
  
  @RestController
  @RequestMapping("/nacos")
  @NacosPropertySource(dataId = "example",autoRefreshed = true)
  public class NacosController {
      @NacosValue(value = "${systemSwitch:false}",autoRefreshed = true)
      private boolean systemSwitch;
  
      @GetMapping("/systemSwitch")
      public boolean getSystemSwitch(){
          return this.systemSwitch;
      }
  }
  • 浏览器打开查看内容
    在这里插入图片描述

  • 监听配置更新事件 @NacosConfigListener(dataId = “example”)

type为配置类型,可以为properties yaml json等等
一旦配置变更,该方法将收到消息,做自定义处理

  @RestController
  @RequestMapping("/nacos")
  @NacosPropertySource(dataId = "example",autoRefreshed = true,type = ConfigType.JSON)
  public class NacosController {
      @NacosValue(value = "${systemSwitch:false}",autoRefreshed = true)
      private boolean systemSwitch;
  
      @NacosConfigListener(dataId = "example")
      public void onMessage(String msg){
          System.out.println(msg);
      }
      
      @GetMapping("/systemSwitch")
      public boolean getSystemSwitch(){
          return this.systemSwitch;
      }
  }

一旦配置被更改,监听的接口将立即收到消息。在示范里我们打印了传来的消息,是更新后的配置
在这里插入图片描述

  • 访问 http://localhost/nacos/systemSwitch显示true
posted @ 2020-12-10 10:59  HumorChen99  阅读(1)  评论(0编辑  收藏  举报  来源