本文共 2480 字,大约阅读时间需要 8 分钟。
nacos上有说明,根据不同的工程进行配置,如下图
集成nacos客户端包
com.alibaba.boot nacos-config-spring-boot-starter 0.1.6
加载配置文件
/***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
启动测试:
说明能加载到配置文件,成功
目前100000+人已关注加入我们
转载地址:http://bxwmf.baihongyu.com/