随笔 - 154  文章 - 0  评论 - 4  阅读 - 49544

nacos配置服务1

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
<!--<version>2.4.1</version>-->
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
<!--<spring-cloud.version>2020.0.0</spring-cloud.version>-->
</properties>
配置中心如何使用
1.依赖spring-cloud-starter-alibaba-nacos-config
2.在应用的 /src/main/resources/bootstrap.properties 配置文件中配置 Nacos Config 元数据
spring.application.name=nacos-config-example
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
3. 在nacoshttp://127.0.0.1:8848/nacos.....配置中添加数据据 gulimall-coupon.properties,编辑发布
4. 动态刷新 @RefreshScope @Value("${user.name}")

 

 成功

 

 参考连接:

https://github.com/alibaba/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme-zh.md

 

下图配置是版本兼容问题吗,为什么获取不到nacos配置中心里的值

 

老方法获取配置,是放在配置文件,用@Value获取,注意user.name此处无效,获取的是系统名字

 

 

 

 

 

 

 

 

 

 test

 

 

上述方法如果需要看到实时修改结果,需要重新改配置,打包发布,启动,再发请求。。。。。现有20台机器.....

新操作:把配置交给配置中心(从配置中心中动态获取配置)

spring.application.name=gulimall-coupon
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
通过启动日志可以发现

 

2021-05-01 17:26:47.607 WARN 79524 --- [ main] c.a.c.n.c.NacosPropertySourceBuilder : Ignore the empty nacos configuration and get it based on dataId[gulimall-coupon] & group[DEFAULT_GROUP]
2021-05-01 17:26:47.614 WARN 79524 --- [ main] c.a.c.n.c.NacosPropertySourceBuilder : Ignore the empty nacos configuration and get it based on dataId[gulimall-coupon.properties] & group[DEFAULT_GROUP]
2021-05-01 17:26:47.616 INFO 79524 --- [ main] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-gulimall-coupon.properties,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-gulimall-coupon,DEFAULT_GROUP'}]
2021-05-01 17:26:47.624 INFO 79524 --- [ main] c.d.g.coupon.GulimallCouponApplication : No active profile set, falling back to default profiles: default

...

2021-05-01 17:27:06.818 INFO 79524 --- [ main] c.a.n.client.config.impl.ClientWorker : [fixed-127.0.0.1_8848] [subscribe] gulimall-coupon.properties+DEFAULT_GROUP
2021-05-01 17:27:06.819 INFO 79524 --- [ main] c.a.nacos.client.config.impl.CacheData : [fixed-127.0.0.1_8848] [add-listener] ok, tenant=, dataId=gulimall-coupon.properties, group=DEFAULT_GROUP, cnt=1

在nacos中新增该配置

 

发布

 

实时刷新

@RefreshScope

 

 

编辑

 

 再请求测试

 

 优先使用配置中心的配置

posted on   daofree  阅读(330)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示