当前位置: 首页 > news >正文

【Java】Spring Boot 2 集成 nacos

官方文档:https://nacos.io/zh-cn/docs/quick-start-spring-boot.html

pom

本次Springboot版本 2.2.6.RELEASE,nacos-config 版本 0.2.7,nacos-discovery版本 0.2.7
parent

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.6.RELEASE</version><relativePath/> <!-- lookup parent from repository -->
</parent>

dependencies

<!-- nacos-config-->
<dependency><groupId>com.alibaba.boot</groupId><artifactId>nacos-config-spring-boot-starter</artifactId><version>0.2.7</version>
</dependency>
<!-- nacos-discovery-->
<dependency><groupId>com.alibaba.boot</groupId><artifactId>nacos-discovery-spring-boot-starter</artifactId><version>0.2.7</version>
</dependency>

Nacos

Nacos版本 nacos-server-2.1.0,启动方式 ./startup.cmd -m standalone 单机方式.

实现配置的动态变更

注解方式

示例使用默认的空间

在这里插入图片描述

在这里插入图片描述

my.http_url=aaaaa
server.port=8092
.................
package cn.com.codingce.demo;import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@NacosPropertySource(dataId = "test", autoRefreshed = true)
@SpringBootApplication
public class CodingceDemoApplication {public static void main(String[] args) {SpringApplication.run(CodingceDemoApplication.class, args);}}

启动项目

2023-02-26 10:39:28.055  INFO 9544 --- [           main] c.a.b.n.c.u.NacosConfigPropertiesUtils   : nacosConfigProperties : NacosConfigProperties{serverAddr='127.0.0.1:8848', contextPath='null', encode='null', endpoint='null', namespace='null', accessKey='null', secretKey='null', ramRoleName='null', autoRefresh=false, dataId='null', dataIds='null', group='DEFAULT_GROUP', type=null, maxRetry='null', configLongPollTimeout='null', configRetryTime='null', enableRemoteSyncConfig=false, extConfig=[], bootstrap=Bootstrap{enable=false, logEnable=false}}

测试

package cn.com.codingce.demo.conrtoller;import cn.com.codingce.demo.utils.ResT;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("test")
public class Test {/*** 通过NacosValue读取配置,* autoRefreshed 表示是否自动更新*/@NacosValue(value = "${my.http_url}", autoRefreshed = true)private String httpUrl;/*** 信息*/@RequestMapping(value = "/a", method = RequestMethod.GET)public ResT info() {return ResT.ok().put("nacos", httpUrl);}}

http://localhost:8092/test/a

在这里插入图片描述
在这里插入图片描述
测试可以实现动态的配置变更.

配置文件方式

采用自定义的命名空间进行测试,新建命名空间
在这里插入图片描述
在这里插入图片描述

新建test配置
在这里插入图片描述
这里我们已经创建完了,就截一张编辑图片.
在这里插入图片描述

配置文件 application.properties

# nacos
# nacos.config.bootstrap.enable 这个配置必须设置, 否则不会读取 properties 配置文件
nacos.config.bootstrap.enable=true
# bootstrap.log-enable
nacos.config.bootstrap.log-enable=true
# server-addr 本机启动, 远端写远端的ip:端口
nacos.config.server-addr=127.0.0.1:8848
# nacos.config.type=properties 这个配置必须设置, 未设置会报空指针, 支持的类型 properties xml json text html yaml
nacos.config.type=properties
# Data Id
nacos.config.dataId=test
# 命名空间ID
nacos.config.namespace=7746f477-222d-4c88-8244-3fae3ae5bdfa
# Group
nacos.config.group=dev
# 自动刷新
nacos.config.auto-refresh=true

nacos.config.bootstrap.enable=true nacos.config.type=properties 这两个配置有点坑了,也怪自己不仔细,大家用的时候注意是否缺少这两个配置~

2023-02-26 10:57:13.579  INFO 7856 --- [           main] c.a.b.n.c.u.NacosConfigPropertiesUtils   : nacosConfigProperties : NacosConfigProperties{serverAddr='127.0.0.1:8848', contextPath='null', encode='null', endpoint='null', namespace='7746f477-222d-4c88-8244-3fae3ae5bdfa', accessKey='null', secretKey='null', ramRoleName='null', autoRefresh=true, dataId='test', dataIds='null', group='dev', type=PROPERTIES, maxRetry='null', configLongPollTimeout='null', configRetryTime='null', enableRemoteSyncConfig=false, extConfig=[], bootstrap=Bootstrap{enable=true, logEnable=true}}

在这里插入图片描述
在这里插入图片描述

测试可以实现动态的配置变更.

实现服务的注册与发现

// todo

http://www.lryc.cn/news/20635.html

相关文章:

  • JavaSE学习笔记day14
  • LLVM高级架构介绍
  • 全网最经典函数题型【详解】——C语言
  • emqx桥接配置+常见问题解决+jmeter压测emqx
  • improve-1
  • 华为OD机试用Python实现 -【云短信平台优惠活动】(2023-Q1 新题)
  • Facebook广告投放运营中的关键成功因素是什么?
  • 2023年1月综合预订类APP用户洞察——旅游市场复苏明显,三年需求春节集中释放
  • 基于stm32计算器设计
  • 基于SpringCloud的可靠消息最终一致性02:项目骨架代码(上)
  • RockerMQ集群部署
  • unicloud的aggregate聚合查询时间戳转日期
  • Vue2.0开发之——使用ref引用组件实例(41)
  • 极狐GitLab仓库瘦身
  • 2288hv5超融合服务器 数码管报888
  • 【Zabbix实战之部署篇】Zabbix监控windows系统配置方法
  • 在Windows上编译Nginx
  • 语音识别与Python编程实践
  • MATLAB绘制泰勒图(Taylor diagram)
  • ClickHouse高可用集群分片-副本实操(四)
  • 2022年中国工业机器人行业市场回顾及2023年发展前景预测分析
  • Gehpi的网络布局
  • 华为OD机试用Python实现 -【天然蓄水库 or 天然蓄水池】(2023-Q1 新题)
  • 西北工业大学大学物理(I)下期末考试2021-2022选填解析
  • 【数据结构】手撕红黑树
  • Linux基础命令-which查找命令文件位置
  • 在Python中,导入拓展库的规范如下:
  • SEATA是什么?它的四种分布式事务模式
  • 【华为OD机试模拟题】用 C++ 实现 - 去重求和(2023.Q1)
  • 如何用 chatGPT,给大家来一个自我介绍