spring boot 集成nacos的案例

1.现在nacos进行配置添加

2.springboot项目集成nacos

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

集成nacos客户端包

 
  1. <dependency>

  2. <groupId>com.alibaba.boot</groupId>

  3. <artifactId>nacos-config-spring-boot-starter</artifactId>

  4. <version>0.1.6</version>

  5. </dependency>

加载配置文件

 
  1. /**

  2. *MITLicense

  3. *Copyright(c)2018haihua.liu

  4. *Permissionisherebygranted,freeofcharge,toanypersonobtainingacopy

  5. *ofthissoftwareandassociateddocumentationfiles(the"Software"),todeal

  6. *intheSoftwarewithoutrestriction,includingwithoutlimitationtherights

  7. *touse,copy,modify,merge,publish,distribute,sublicense,and/orsell

  8. *copiesoftheSoftware,andtopermitpersonstowhomtheSoftwareis

  9. *furnishedtodoso,subjecttothefollowingconditions:

  10. *Theabovecopyrightnoticeandthispermissionnoticeshallbeincludedinall

  11. *copiesorsubstantialportionsoftheSoftware.

  12. *THESOFTWAREISPROVIDED"ASIS",WITHOUTWARRANTYOFANYKIND,EXPRESSOR

  13. *IMPLIED,INCLUDINGBUTNOTLIMITEDTOTHEWARRANTIESOFMERCHANTABILITY,

  14. *FITNESSFORAPARTICULARPURPOSEANDNONINFRINGEMENT.INNOEVENTSHALLTHE

  15. *AUTHORSORCOPYRIGHTHOLDERSBELIABLEFORANYCLAIM,DAMAGESOROTHER

  16. *LIABILITY,WHETHERINANACTIONOFCONTRACT,TORTOROTHERWISE,ARISINGFROM,

  17. *OUTOFORINCONNECTIONWITHTHESOFTWAREORTHEUSEOROTHERDEALINGSINTHE

  18. *SOFTWARE.

  19. */

  20. packagecn.liuhaihua.web;

  21.  

  22. importcn.liuhaihua.web.mapper.BaseMapper;

  23. importcom.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;

  24. importorg.mybatis.spring.annotation.MapperScan;

  25. importorg.springframework.boot.SpringApplication;

  26. importorg.springframework.boot.autoconfigure.SpringBootApplication;

  27. importorg.springframework.boot.web.servlet.ServletComponentScan;

  28. importorg.springframework.transaction.annotation.EnableTransactionManagement;

  29.  

  30. /**

  31. *@ClassName:JWordpressWebApplication

  32. *@Description:Springboot应用程序启动类

  33. *@authorLiuhaihua

  34. *@date2018年6月26日

  35. *

  36. */

  37. @SpringBootApplication

  38. @ServletComponentScan

  39. @EnableTransactionManagement

  40. @MapperScan(basePackages="cn.liuhaihua.web.*",markerInterface=BaseMapper.class)

  41. @NacosPropertySource(dataId="web",autoRefreshed=true)

  42. publicclassJWordpressWebApplication{

  43.  

  44. /**

  45. *@Title:main

  46. *@Description:main启动方法

  47. *@param@paramargs

  48. *@returnvoid

  49. *@throws

  50. */

  51. publicstaticvoidmain(String[]args){

  52. SpringApplication.run(JWordpressWebApplication.class,args);

  53. System.out.println("JWordpressWebApplication启动成功");

  54.  

  55. }

  56.  

  57. }

主要代码是:

 
  1. @NacosPropertySource(dataId="web",autoRefreshed=true)

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

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

application.properties

  •  
spring.profiles.active=develop

application-develop.properties

 
  1. nacos.config.server-addr=10.42.226.203:8848

  2. nacos.config.namespace=39a58908-d8af-4816-b87d-c964aa8cbaf2

启动测试:

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

 

java杂记,经过阅读后。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值