springboot集成nacos

1.现在nacos进行配置添加

springboot集成nacos

2.springboot项目集成nacos

nacos上有说明,根据不同的工程进行配置,如下图

springboot集成nacos

集成nacos客户端包

<dependency><groupId>com.alibaba.boot</groupId><artifactId>nacos-config-spring-boot-starter</artifactId><version>0.1.6</version></dependency>复制代码

加载配置文件

/***MITLicense*Copyright(c)2018haihua.liu*Permissionisherebygranted,freeofcharge,toanypersonobtainingacopy*ofthissoftwareandassociateddocumentationfiles(the"Software"),todeal*intheSoftwarewithoutrestriction,includingwithoutlimitationtherights*touse,copy,modify,merge,publish,distribute,sublicense,and/orsell*copiesoftheSoftware,andtopermitpersonstowhomtheSoftwareis*furnishedtodoso,subjecttothefollowingconditions:*Theabovecopyrightnoticeandthispermissionnoticeshallbeincludedinall*copiesorsubstantialportionsoftheSoftware.*THESOFTWAREISPROVIDED"ASIS",WITHOUTWARRANTYOFANYKIND,EXPRESSOR*IMPLIED,INCLUDINGBUTNOTLIMITEDTOTHEWARRANTIESOFMERCHANTABILITY,*FITNESSFORAPARTICULARPURPOSEANDNONINFRINGEMENT.INNOEVENTSHALLTHE*AUTHORSORCOPYRIGHTHOLDERSBELIABLEFORANYCLAIM,DAMAGESOROTHER*LIABILITY,WHETHERINANACTIONOFCONTRACT,TORTOROTHERWISE,ARISINGFROM,*OUTOFORINCONNECTIONWITHTHESOFTWAREORTHEUSEOROTHERDEALINGSINTHE*SOFTWARE.*/packagecn.liuhaihua.web;importcn.liuhaihua.web.mapper.BaseMapper;importcom.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;importorg.mybatis.spring.annotation.MapperScan;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.boot.web.servlet.ServletComponentScan;importorg.springframework.transaction.annotation.EnableTransactionManagement;/***@ClassName:JWordpressWebApplication*@Description:Springboot应用程序启动类*@authorLiuhaihua*@date2018年6月26日**/@SpringBootApplication@ServletComponentScan@EnableTransactionManagement@MapperScan(basePackages="cn.liuhaihua.web.*",markerInterface=BaseMapper.class)@NacosPropertySource(dataId="web",autoRefreshed=true)publicclassJWordpressWebApplication{/***@Title:main*@Description:main启动方法*@param@paramargs*@returnvoid*@throws*/publicstaticvoidmain(String[]args){SpringApplication.run(JWordpressWebApplication.class,args);System.out.println("JWordpressWebApplication启动成功");}}复制代码

主要代码是:

@NacosPropertySource(dataId="web",autoRefreshed=true)复制代码

dataId="web" 对应的是你在nacos服务端的配置

如果要区分不同环境的话(开发/测试/生产)需要配上namespace,

application.properties

spring.profiles.active=develop复制代码

application-develop.properties

nacos.config.server-addr=10.42.226.203:8848nacos.config.namespace=39a58908-d8af-4816-b87d-c964aa8cbaf2复制代码

启动测试:

springboot集成nacos

说明能加载到配置文件,成功

原文 

https://juejin.im/post/5e814c1b6fb9a03c7a330800

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。

PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » springboot集成nacos

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址