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. importcn.liuhaihua.web.mapper.BaseMapper;

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

  23. importorg.mybatis.spring.annotation.MapperScan;

  24. importorg.springframework.boot.SpringApplication;

  25. importorg.springframework.boot.autoconfigure.SpringBootApplication;

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

  27. importorg.springframework.transaction.annotation.EnableTransactionManagement;

  28. /**

  29. *@ClassName:JWordpressWebApplication

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

  31. *@authorLiuhaihua

  32. *@date2018年6月26日

  33. *

  34. */

  35. @SpringBootApplication

  36. @ServletComponentScan

  37. @EnableTransactionManagement

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

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

  40. publicclassJWordpressWebApplication{

  41. /**

  42. *@Title:main

  43. *@Description:main启动方法

  44. *@param@paramargs

  45. *@returnvoid

  46. *@throws

  47. */

  48. publicstaticvoidmain(String[]args){

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

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

  51. }

  52. }

主要代码是:

 
  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杂记,经过阅读后。

发布了306 篇原创文章 · 获赞 67 · 访问量 38万+

猜你喜欢

转载自blog.csdn.net/ailiandeziwei/article/details/105239299
今日推荐