两种方式实现Spring Boot项目集成Nacos做配置中心教程

两种方式实现Spring Boot项目集成Nacos做配置中心教程

经验文章nimo972025-03-10 14:15:239A+A-

1 方式一:SpringBoot实现

1.1 pom依赖


  4.0.0

  
    org.springframework.boot
    spring-boot-starter-parent
    2.7.18
    
  

  org.example
  spring_security_oauth2_client
  1.0-SNAPSHOT
  jar

  spring_security_oauth2_client
  http://maven.apache.org

  
    UTF-8
  

  
    
      org.springframework.boot
      spring-boot-starter-web
    
      
        org.springframework.boot
        spring-boot-starter
      
      
        com.alibaba.boot
        nacos-config-spring-boot-starter
        0.2.12
      
  

1.2 配置文件application.properties

nacos:
  config:
    server-addr: 192.168.0.109:8848
    username: nacos
    password: nacos

1.3 配置类

package com.zydgbbs.nacos.config;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import org.springframework.context.annotation.Configuration;

@Configuration
@NacosPropertySource(dataId = "nacos-test", autoRefreshed = true)
public class NacosConfig {

    @NacosValue(value = "${name}", autoRefreshed = true)
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

1.4 访问类

package com.zydgbbs.nacos.controller;

import com.zydgbbs.nacos.config.NacosConfig;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class NacosProviderController {

    @Resource
    private NacosConfig nacosConfig;

    @GetMapping
    public String getName(){
        return nacosConfig.getName();
    }

}

1.5 启动类

package com.zydgbbs.nacos;

import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class NacosClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(NacosClientApplication.class, args);
    }
}

1.6 nacos配置

data-id配nacos-test

name=面试题解析

1.7 访问测试

http://localhost:8080/
面试题解析
# 说明:当nacos里的配置更新后,刷新这个URL,页面返回的值会变成最新的nacos里的配置。

2 方式二:SpringCloud实现

2.1 pom依赖


  4.0.0

  
    org.springframework.boot
    spring-boot-starter-parent
    2.3.12.RELEASE
    
  

  org.example
  spring_security_oauth2_client
  1.0-SNAPSHOT
  jar

  spring_security_oauth2_client
  http://maven.apache.org

  
    UTF-8
  

  
    
      org.springframework.boot
      spring-boot-starter-web
    
      
        com.alibaba.cloud
        spring-cloud-starter-alibaba-nacos-config
        2.2.8.RELEASE
      
  

2.2 配置文件bootstrap.properties

spring.application.name=nacos-test
spring.cloud.nacos.server-addr=192.168.0.109:8848
spring.cloud.nacos.config.password=nacos
spring.cloud.nacos.config.username=nacos
# 额外的配置项
spring.cloud.nacos.config.shared-configs[0].data-id=nacos-test2
spring.cloud.nacos.config.extension-configs[0].data-id=nacos-test2

2.3 配置类

package com.zydgbbs.nacos.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;

@Configuration
@RefreshScope
public class NacosConfig {

    @Value(value = "${name}")
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

2.4 访问类

package com.zydgbbs.nacos.controller;

import com.zydgbbs.nacos.config.NacosConfig;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class NacosProviderController {

    @Resource
    private NacosConfig nacosConfig;

    @GetMapping
    public String getName(){
        return nacosConfig.getName()+":"+nacosConfig.getAge();
    }

}

2.5 启动类

package com.zydgbbs.nacos;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class NacosClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(NacosClientApplication.class, args);
    }
}

2.6 nacos配置

data-id配nacos-test

name=面试题解析

data-id配nacos-test2

age=12

2.7 访问测试

http://localhost:8080/
面试题解析:12
# 说明:当nacos里的配置更新后,刷新这个URL,页面返回的值会变成最新的nacos里的配置。

3 小结

大家可以按照这两种方式跑下,应该没有什么问题,如有问题,请留言讨论。

【温馨提示】

点赞+收藏文章,关注我并私信回复【面试题解析】,即可100%免费领取楼主的所有面试题资料!

点击这里复制本文地址 以上内容由nimo97整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

尼墨宝库 © All Rights Reserved.  蜀ICP备2024111239号-7