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 2020-12-29 19:26  daofree  阅读(326)  评论(0编辑  收藏  举报