SpringBoot 集成 Nacos

目录

  • 一、前言
  • 二、Nacos集成
    • 1、引入Nacos依赖
    • 2、设置Nacos配置
    • 3、加载Nacos配置中心配置项
    • 4、Nacos集成验证
    • 5、Nacos配置中心配置项动态生效


Nacos安装详见:Spring Cloud 系列之 Nacos 配置中心


一、前言

上一篇已经讲解了怎样安装安装、启动、配置 Nacos,这篇我们讲解如何在项目中使用 Nacos

还不了解 Nacos 的详见:Spring Cloud 系列之 Nacos 配置中心

在集成 Nacos 之前,首先我们要先创建一个 Spring Boot 项目:IDEA 创建 SpringBoot 项目


二、Nacos集成

1、引入Nacos依赖

<dependencies><!-- nacos --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId><version>2.2.1.RELEASE</version></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId><version>2.2.1.RELEASE</version></dependency>
<dependencies>

注:Spring Boot版本要低于2.4,否则启动应用会报错。
在这里插入图片描述


2、设置Nacos配置

项目中默认配置文件是 application.propertiesNacos 配置加在此配置文件中的话,应用启动会报连接 Nacos 失败,我们需要创建 bootstrap.propertiesbootstrap.yml 配置文件(添加任意一个即可),下面我们以 bootstrap.properties 为例:

spring.application.name=apm-mobile-android
spring.cloud.nacos.username=nacos
spring.cloud.nacos.password=nacos
spring.cloud.nacos.server-addr=10.0.7.115:18117spring.cloud.nacos.discovery.namespace=PROD
spring.cloud.nacos.config.namespace=PROD
spring.cloud.nacos.config.timeout=3000
spring.cloud.nacos.config.refresh-enabled=truespring.cloud.nacos.config.group=apm
spring.cloud.nacos.config.prefix=${spring.application.name}
spring.cloud.nacos.config.file-extension=propertiesspring.cloud.nacos.config.shared-configs[0].group=apm
spring.cloud.nacos.config.shared-configs[0].data-id=apm-mobile-android.properties
spring.cloud.nacos.config.shared-configs[0].refresh=truespring.liquibase.enabled=false

在这里插入图片描述


3、加载Nacos配置中心配置项

在初始化类中添加 @EnableDiscoveryClient 注解即可:

package com.example.springbootdemo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@EnableDiscoveryClient
@SpringBootApplication
public class SpringbootdemoApplication {public static void main(String[] args) {SpringApplication.run(SpringbootdemoApplication.class, args);new BootstrapManager();}}

在这里插入图片描述


4、Nacos集成验证

Nacos配置如下:

在这里插入图片描述

启动应用,然后访问:http://localhost:8085/hello

出现如下界面说明加载Nacos配置成功。

在这里插入图片描述


5、Nacos配置中心配置项动态生效

需要在配置对象中添加 @RefreshScope 注解,然后重启应用。

package com.example.springbootdemo.config;import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;@Data
@Component
@Configuration
@RefreshScope
public class GlobalConfig {@Value("${data.domain:http://10.0.0.1:18080}")private String dataDomain;@Value("${log.level:DEBUG}")private String logLevel;
}

在这里插入图片描述

重启后,访问:http://localhost:8085/hello

在这里插入图片描述

Nacos 配置中的 log.level 修改为 DEBUG ,然后重新访问:http://localhost:8085/hello,出现如下界面说明 Nacos 配置动态生成成功。

在这里插入图片描述


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/446728.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C++primer第八章 IO库 8.3string流

8.3string流 sstream头文件定义了三个类型来支持内存IO,这些类型可以向string写入数据,从string读取数据&#xff0c;就像string是一个IO流一样。istringstream从string读取数据&#xff0c;ostringstream向string写入数据&#xff0c;而头文件stringstream既可从string读数据…

英语口语海报演讲--东软

海报 海报上的内容 Nuclear waste water 1.Damage the devastating impact of nuclear radiation on the world 2.Marine life genetically mutated or dead 3.water resources polluted water resources 4.the future of humanity genetic damage/food and environment destr…

Java中 List、Set、Map遍历方式以及性能比较

目录一、简介二、遍历方式1、ArrayList遍历方式&#xff08;1&#xff09;for循环遍历&#xff08;2&#xff09;foreach循环遍历&#xff08;3&#xff09;Iterator迭代器遍历2、LinkedList遍历方式&#xff08;1&#xff09;for循环遍历&#xff08;2&#xff09;foreach循环…

codeforces 263A-C语言解题报告

263A题目网址 题目解析 1.输入5*5的矩阵(下标从到5),包含24个0和一个1,问如何移动最小的次数(i相邻行或列)可以让1位于3行3列 举例: 输入: 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 输出: 3 注意点 1.因为数组是从0开始的,所以减2就行 row-2col-2 2.使用整型二维…

一个DEMO让你彻底理解线程池

目录一、简介二、线程池任务场景场景一&#xff1a;提交5个任务&#xff0c;执行总耗时500ms场景二&#xff1a;提交10个任务&#xff0c;执行总耗时500ms场景三&#xff1a;提交11个任务&#xff0c;执行总耗时1000ms场景四&#xff1a;提交20个任务&#xff0c;执行总耗时100…

C++primer第九章 顺序容器 9.1 顺序容器概述 9.2容器库概览

一个容器就是一些特定类型对象的集合。顺序容器(sequentialcontainer)为程序员提供了控制元素存储和访问顺序的能力。这种顺序不依赖于元素的值&#xff0c;而是与元素加入容器时的位置相对应。与之相对的&#xff0c;我们将在第11章介绍的有序和无序关联容器&#xff0c;则根据…

SpringBoot 启动报错:Failed to configure a DataSource: ‘url‘ attribute is not specified and no emb

目录一、报错日志二、原因分析三、问题排查四、解决方案方案一&#xff1a;如果项目不需要数据库相关信息就排除此类的autoconfig方案二&#xff1a;配置文件添加数据库链接信息方案三&#xff1a;配置pom.xml中yml或者properties扫描一、报错日志 **************************…

codeforces 339A-C语言解题报告

339A题目网址 题目解析 1.输入如321的式子,升序排序(从小到大)成123 举例: 输入: 11313 输出: 11133 2.对字符串进行排序采取拍冒泡排序算法 char c0; for(i0;i<strlen(s)-1;i) {for(j0;j<strlen(s)-1;j){if(s[j]>s[j1]){cs[j];s[j]s[j1];s[j1]c;}} }代码 #includ…

C++primer第九章 顺序容器 9.3 顺序容器操作

9.3顺序容器操作 顺序容器和关联容器的不同之处在于两者组织元素的方式。这些不同之处直接关系到了元素如何存储、访问、添加以及删除。上一节介绍了所有容器都支持的操作&#xff08;罗列于表9.2&#xff08;第295页&#xff09;&#xff09;。本章剩余部分将介绍顺序容器所特…

SpringBoot 集成Nacos报错(一)

目录配置信息报错信息解决方案配置信息 <project><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.2</version><relativePath/></parent>…

C++primer第九章 顺序容器 9.4 vector对象是如何增长的

为了支持快速随机访问&#xff0c;vector将元素连续存储&#xff0c;每个元素紧挨着前一个元素存储。通常情况下&#xff0c;我们不必关心一个标准库类型是如何实现的&#xff0c;而只需关心它如何使用。然而&#xff0c;对于vector和string,其部分实现渗透到了接口中。假定容器…

codeforces 281A-C语言解题报告

281A题目网址 题目解析 1.字符串首字母大写 代码 #include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> int main() {char s[1000]{\0};scanf("%s",s);if(s[0]>A&&s[0]<Z){printf("%s",s…

SpringBoot 配置文件bootstrap和application的区别

目录一、SpringBoot配置文件二、bootstrap和application区别三、bootstrap和application的应用场景一、SpringBoot配置文件 bootstrap&#xff08;.yml 或者 .properties&#xff09; application&#xff08;.yml 或者 .properties&#xff09; 二、bootstrap和application区…

C++primer第九章 顺序容器 9.5 额外的string操作

除了顺序容器共同的操作之外&#xff0c;string类型还提供了一些额外的操作。这些操作中 的大部分要么是提供string类和C 风格字符数组之间的相互转换,要么是增加了允许我们用下标代替迭代器的版本。标准库string类型定义了大量函数。幸运的是&#xff0c;这些函数使用了重复的…

Zookeeper Mac下安装操作

目录一、下载Zookeeper二、修改配置1、设置启动配置文件2、修改配置三、启动Zookeeper服务命令1、bin目录下执行&#xff08;1&#xff09;启动Zookeeper命令&#xff08;2&#xff09;查看Zookeeper状态命令&#xff08;3&#xff09;停止Zookeeper命令2、配置环境变量执行&am…

codeforces 266A-C语言解题报告

266A题目网址 题目解析 1.输入n(1–50)个石头个数,输入RGB的石头颜色,求问拿走最小的石头个数,让它们相邻的石头颜色不同 代码 #include<stdio.h> #include<stdlib.h> #include<string.h> int main() {int n,i,count0;char s[50]{\0};scanf("%d&quo…

2014年考研英语二作文PartB图表题

作文详细解析 题目 Write an essay based on the following chart, in which you should interpret the chart, and give your comments You should write about 150 words on the ANSWER SHEET.(15 points) 注意点 1.图表题在第一段描述图表信息时,一定要写清楚y轴变化…

Zookeeper 终端命令

目录一、服务端命令1、启动Zookeeper服务命令2、查看Zookeeper状态命令3、停止Zookeeper服务命令4、启动Zookeeper客户端命令二、客户端命令1、查看帮助2、查看当前znode所包含的内容3、创建znode4、创建短暂znode5、创建带序号znode6、创建短暂带序号znode7、获取znode数据8、…

C++primer第九章 顺序容器 9.6 容器适配器

9.6容器适配器 除了顺序容器外&#xff0c;标准库还定义了三个顺序容器适配器&#xff1a;stack、queue和priority_queue适配器(adaptor)是标准库中的一个通用概念。容器、迭代器和函数<369I都有适配器。本质上&#xff0c;一个适配器是一种机制&#xff0c;能使某种事物的…

codeforces 236A-C语言解题报告

236题目网址 题目解析 1.输入字符串,判断其中不同的字符个数,奇偶输出不同的语句 2.使用冒泡排序去排序,当遇到s[k]!s[k1]时进行计数 代码 #include<stdio.h> #include<stdlib.h> #include<string.h> int main() {char s [100]{\0};int i,j,k,count0;cha…