[ Spring ] Nacos Config Auto Refresh 2025

文章目录

          • [Add Dependencies](#Add Dependencies)
          • [Configure Properties](#Configure Properties)
          • [Nacos Application](#Nacos Application)
          • [Nacos Config](#Nacos Config)
          • [Nacos Controller](#Nacos Controller)
          • [Add Config File to Nacos Server](#Add Config File to Nacos Server)
          • [Start and Visit Service](#Start and Visit Service)
Add Dependencies
kotlin 复制代码
plugins {
    id("org.jetbrains.kotlin.jvm") version "2.0.21" apply false
    id("org.jetbrains.kotlin.kapt") version "2.0.21" apply false
    id("org.jetbrains.kotlin.plugin.spring") version "2.0.21" apply false
    id("org.springframework.boot") version "3.4.1" apply false
}
kotlin 复制代码
plugins {
    id("org.jetbrains.kotlin.jvm")
    id("org.jetbrains.kotlin.kapt")
    id("org.jetbrains.kotlin.plugin.spring")
    id("org.springframework.boot")
}

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

dependencies {
    val springBootVersion = "3.4.1"
    val springCloudVersion = "4.2.0"
    // commons
    api("io.github.hellogoogle2000:kotlin-commons:1.0.19")
    // kotlin
    api("org.jetbrains.kotlin:kotlin-reflect:2.0.21")
    // spring
    api("org.springframework.boot:spring-boot-starter:$springBootVersion")
    api("org.springframework.boot:spring-boot-starter-web:$springBootVersion")
    api("org.springframework.boot:spring-boot-devtools:$springBootVersion")
    api("org.springframework.cloud:spring-cloud-starter-bootstrap:$springCloudVersion")
    // nacos config
    api("com.alibaba.nacos:nacos-spring-context:2.1.1")
    api("com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-config:2023.0.3.2")
    api("com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery:2023.0.3.2")
}
Configure Properties

config file must named to bootstrap.properties

properties 复制代码
# service
server.port=10003
spring.application.name=nacos-app
Nacos Application
kotlin 复制代码
package x.spring.hello

import com.alibaba.nacos.api.annotation.NacosProperties
import com.alibaba.nacos.api.config.ConfigType
import com.alibaba.nacos.spring.context.annotation.EnableNacos
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@EnableNacos(
    globalProperties = NacosProperties(
        serverAddr = "localhost:8848",
        username = "root",
        password = "123456",
        namespace = "public"
    )
)
@NacosPropertySource(
    type = ConfigType.JSON,
    dataId = "nacos-app-dev.json",
    groupId = "DEFAULT_GROUP",
    autoRefreshed = true
)
@SpringBootApplication
class NacosClientApplication

fun main(args: Array<String>) {
    runApplication<NacosClientApplication>(*args)
}
Nacos Config
kotlin 复制代码
package x.spring.hello.component

import com.alibaba.nacos.api.config.ConfigType
import com.alibaba.nacos.api.config.annotation.NacosConfigurationProperties
import org.springframework.stereotype.Component

@Component
@NacosConfigurationProperties(
    type = ConfigType.JSON,
    dataId = "nacos-app-dev.json",
    groupId = "DEFAULT_GROUP",
    autoRefreshed = true
)
data class NacosDevConfig(
    var username: String = "",
    var password: String = ""
)
Nacos Controller
kotlin 复制代码
package x.spring.hello.controller

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
import x.kotlin.commons.serialize.JSON.toJson
import x.spring.hello.component.NacosDevConfig

@RestController
class NacosConfigController {

    @Autowired
    private lateinit var config: NacosDevConfig

    @GetMapping("/config")
    fun all(): String {
        return config.copy().toJson()
    }
}
Add Config File to Nacos Server

server config file must match client annotations

properties 复制代码
http://localhost:8848/nacos
properties 复制代码
namespace : public
data-id : nacos-app-dev.json
group : DEFAULT_GROUP
format : JSON
json 复制代码
{
    "username": "root",
    "password": "123456"
}
Start and Visit Service
properties 复制代码
http://localhost:10003/config

modify server config file, then client config values will automatically reload

相关推荐
booooooty33 分钟前
基于Spring AI Alibaba的多智能体RAG应用
java·人工智能·spring·多智能体·rag·spring ai·ai alibaba
极光雨雨38 分钟前
Spring Bean 控制销毁顺序的方法总结
java·spring
Spirit_NKlaus1 小时前
解决HttpServletRequest无法获取@RequestBody修饰的参数
java·spring boot·spring
lwb_01182 小时前
SpringCloud——Gateway新一代网关
spring·spring cloud·gateway
fundroid2 小时前
Kotlin 协程:Channel 与 Flow 深度对比及 Channel 使用指南
android·kotlin·协程
程序猿小D4 小时前
[附源码+数据库+毕业论文]基于Spring+MyBatis+MySQL+Maven+jsp实现的电影小说网站管理系统,推荐!
java·数据库·mysql·spring·毕业设计·ssm框架·电影小说网站
CodeWithMe5 小时前
【Note】《深入理解Linux内核》 Chapter 15 :深入理解 Linux 页缓存
linux·spring·缓存
llwszx5 小时前
Spring中DelayQueue深度解析:从原理到实战(附结构图解析)
java·后端·spring·delayqueue·延迟任务
移动开发者1号5 小时前
深入理解原子类与CAS无锁编程:原理、实战与优化
android·kotlin
移动开发者1号5 小时前
深入理解 ThreadLocal:原理、实战与优化指南
android·kotlin