好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java springboot 整合 Nacos的实例代码

Nacos注册中心使用

1)工程添加依赖包

<!-- nacos注册中心依赖包 --> <dependency> <groupId> com.alibaba.cloud </groupId> <artifactId> spring-cloud-starter-alibaba-nacos-discovery </artifactId> </dependency> <!-- 监控检查--> <dependency> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-starter-actuator </artifactId> </dependency>

2)nacos-web工程添加配置文件bootstrap.yml

spring : application : name : nacos - web cloud : nacos : discovery : server - addr : 127.0 . 0.1 : 8848 server : port : 9100 #健康检查 management : endpoints : web : exposure : include : "*"

3)nacos-web工程添加启动类

@SpringBootApplication @EnableDiscoveryClient public class NacosWebApplication { ​ public static void main ( String [] args ) { SpringApplication . run ( NacosWebApplication . class , args ); } }

4)nacos-web工程新建InfoController

@RestController public class InfoController { ​ @Value ( "${server.port}" ) private String port ; ​ @Value ( "${key:''}" ) private String key ; ​ @GetMapping ( value = "/echo/{message}" ) public String echo ( @PathVariable ( value = "message" ) String message ) { return "Hello Nacos Discovery " + message + ", i am from port " + port ; } ​ @GetMapping ( value = "/config" ) public String config () { return "Hello Nacos Config get " + key ; } }

5)启动服务并查看效果

6)编辑配置内容

7)启动项目访问工程

☆ 动态刷新☆

Nacos Config Starter 默认为所有获取数据成功的 Nacos
的配置项添加了监听功能,在监听到服务端配置发生变化时会实时触发
org.springframework.cloud.context.refresh.ContextRefresher 的 refresh
方法 。

如果需要对 Bean 进行动态刷新,参照 Spring 和 Spring Cloud 规范。推荐给类添加 @RefreshScope
进行自动刷新

发布新配置

接受到了新配置


8)多环境支持

为不同的环境编写专门的配置文件,可以通过切换profiles的参数来实现

测试环境 test

开发环境 dev

生产环境 prod

-- spring . profiles . active =环境名称


9) Nacos实现多配置

nacos可以同时支持多环境配置。只需要在nacos配置中心中根据dataId进行区分即可。dataId 完整的拼接格式如下

在 Nacos Config Starter 中,dataId 完整的拼接格式如下

${prefix}-${spring.profiles.active}.${file-extension} prefix 默认为
spring.application.name 的值,也可以通过配置项
spring.cloud.nacos.config.prefix 来配置。 spring.profiles.active 即为当前环境对应的
profile file-extension 为配置内容的数据格式,可以通过配置项
spring.cloud.nacos.config.file-extension 来配置。 目前只支持 properties 类型。

10)修改bootstrap.yml

spring : profiles : active : dev #开发环境 application : name : nacos - web cloud : nacos : discovery : server - addr : 127.0 . 0.1 : 8848 config : server - addr : 127.0 . 0.1 : 8848 file - extension : yml #后缀 prefix : nacos - web #前缀 server : port : 9100

11)nacos中新增配置信息

当前开发环境为dev时

12)不同应用间配置信息共享
Nacos中新建common.yml

修改nacos-web工程配置文件,添加

spring : application : name : nacos - web cloud : nacos : discovery : server - addr : 192.168 . 200.128 : 8848 config : server - addr : 192.168 . 200.128 : 8848 file - extension : yml #指定文件扩展名,默认为properties prefix : nacos - web #添加共享配置的dataId,如多个使用逗号分隔,并且越靠后,优先级越高 #文件后缀名不能少,只支持yaml,yml,properies shared - dataids : common . yml #哪些共享配置支持动态刷新,如多个使用逗号分隔 refreshable - dataids : common . yml profiles : active : dev #开发环境 server : port : 9100 management : endpoints : web : exposure : include : "*" #日志 logging : level : org . springframework . web : debug

到此这篇关于Java springboot 整合 Nacos的文章就介绍到这了,更多相关Java springboot 整合 Nacos内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/qq_31920553/article/details/115470277

查看更多关于Java springboot 整合 Nacos的实例代码的详细内容...

  阅读:17次