转载

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
正文到此结束
Loading...