Springboot 实践(17)spring boot整合Nacos配置中心

前文我们讲解了Nacos服务端的下载安装,本文我们降价spring boot整合nacos,实现Nacos服务器配置参数的访问。

一、启动Nacos服务,创建三个配置文件,如下所示

  • Springboot-Nacos-Client-dev.yaml文件配置参数
  • Springboot-Nacos-Client.yaml文件配置参数
  • sjl.yaml文件配置参数

二、建立Nacos客户端

1、利用MyEclipse2019创建web project工程,命名为"Springboot-Nacos-Client",工程全貌如下图所示:

2、修改pom.xml文件

pom文件中加入对应jar包,

<!--注册中心的依赖-->

<dependency>

<groupId>com.alibaba.cloud</groupId>

<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>

<version>2.1.2.RELEASE</version>

</dependency>

<!-- 配置中心的依赖 -->

<dependency>

<groupId>com.alibaba.cloud</groupId>

<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>

<version>2.1.2.RELEASE</version>

</dependency>

<dependency>

<groupId>com.alibaba.nacos</groupId>

<artifactId>nacos-client</artifactId>

<version>2.1.2</version>

</dependency>

<dependency>

<groupId>com.alibaba.cloud</groupId>

<artifactId>spring-cloud-alibaba-dependencies</artifactId>

<version>2.1.2.RELEASE</version>

<type>pom</type>

<scope>import</scope>

</dependency>

备注:注意上述jar包的版本,需要和Nacos官网发布的匹配版本一致,否则会有各种奇怪问题出现。

3、修改application.yml文件

文件内容如下:

spring:

application:

name: Springboot-Nacos-Client

profiles:

active: dev # 激活配置

cloud:

nacos:

discovery:

server-addr: 127.0.0.1:8848

username: nacos

password: nacos

config:

enabled: true # 是否开启配置中心 默认true

server-addr: 127.0.0.1:8848 #nacos地址

username: nacos

password: nacos

namespace: 7fe8fb2f-9e3a-438b-bf9a-1a0ca8d4c898

group: test

prefix: ${spring.application.name}

file-extension: yaml #文件后缀,可省略不配置

extension-configs:

  • data-id: sjl.yaml

group: test

refresh: true

  • data-id: Springboot-Nacos-Client.yaml

group: test

refresh: true

备注:

spring-cloud-starter-alibaba-nacos-config 默认加载文件

加载 dataid 为 {spring.application.name}.{file-extension:properties} 为前缀的基础配置,

加载dataid为 {spring.application.name}-{profile}.${file-extension:properties} 的基础配置

**☆**extension-configs: #扩展配置,用于引入多个配置;多配置文件时,避免出现系统参数配置%

**☆**配置文件优先权

通过内部相关规则(应用名、扩展名、profiles)自动生成相关的 Data Id 配置优先级最高nacos中的配置优先于本地配置,本地的bootstrap.yml>bootstrap.properties>application.yml>application.yaml>application.properties

扩展配置(extension-configs) > 共享配置(shared-configs)

同为扩展配置,存在如下优先级关系:extension-configs[3] > extension-configs[2] > extension-configs[1] > extension-configs[0]

同为共享配置,存在如下优先级关系:shared-configs[3] > shared-configs[2] > shared-configs[1] > shared-configs[0]

4、添加启动函数

@SpringBootApplication(scanBasePackages= {"com.SJL"},exclude = {

DataSourceAutoConfiguration.class,

DataSourceTransactionManagerAutoConfiguration.class,

HibernateJpaAutoConfiguration.class})

@ComponentScan(basePackages= {"com.SJL"})

@ServletComponentScan(basePackages= {"com.SJL"})

@EnableDiscoveryClient

public class ConfingClientApplication {

public static void main(String[] args) {

SpringApplication.run(ConfingClientApplication.class, args);

}

}

5、添加Controller,访问Nacos配置文件参数

创建三个Controller文件,分别如下:

☆NacosConfigController文件

@RequestMapping("/config")

@RestController

@RefreshScope

@NacosConfigurationProperties(dataId = "Springboot-Nacos-Client-dev.yaml", groupId = "test", autoRefreshed = true)

public class NacosConfigController {

@Value(value = "${config.name}")

private String configName;

@NacosInjected

private ConfigService configService;

@GetMapping("getConfigName")

public String getConfigName(){

return configName;

}

}

☆NacosConfigController2文件

@RequestMapping("/config2")

@RestController

@RefreshScope

@NacosConfigurationProperties(dataId = "Springboot-Nacos-Client.yaml", groupId = "test", autoRefreshed = true)

public class NacosConfigController2 {

@Value(value = "${auther}")

private String auther;

@NacosInjected

private ConfigService configService;

@GetMapping("getAuther")

public String getAuther(){

return auther;

}

}

☆NacosConfigController3文件

RequestMapping("/config3")

@RestController

@RefreshScope

@NacosConfigurationProperties(dataId = "sjl.yaml", groupId = "test", autoRefreshed = true)

public class NacosConfigController3 {

@Value(value = "${name}")

private String name;

@GetMapping("getName")

public String getName(){

return name;

}

}

三、测试

在浏览中,输入"localhost:2881/swagger-ui.html",测试三个controller的返回结果,如下所示,能够获得争取结果。

相关推荐
c***93771 小时前
SpringBoot实现异步调用的方法
java·spring boot·spring
青衫码上行2 小时前
【Java Web学习 | 第15篇】jQuery(万字长文警告)
java·开发语言·前端·学习·jquery
凯子坚持 c2 小时前
Docker 容器实战:从镜像管理到私有仓库构建深度解析
java·docker·eureka
q***01653 小时前
Windows操作系统部署Tomcat详细讲解
java·windows·tomcat
x***13394 小时前
【MyBatisPlus】MyBatisPlus介绍与使用
android·前端·后端
f***68605 小时前
【SpringBoot篇】详解Bean的管理(获取bean,bean的作用域,第三方bean)
java·spring boot·后端
z***75156 小时前
【Springboot3+vue3】从零到一搭建Springboot3+vue3前后端分离项目之后端环境搭建
android·前端·后端
likuolei6 小时前
Eclipse 快捷键
java·ide·eclipse
w***95498 小时前
SQL美化器:sql-beautify安装与配置完全指南
android·前端·后端