springboot 使用consul 讀取配置文件(遇到的坑太多,沒記錄)


最終成功版

  1. pom引入mavn依賴:

   
   
  
  
          
  1. <!--consul-->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-consul-discovery</artifactId>
  5. <version> 2.1.2.RELEASE</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.springframework.cloud</groupId>
  9. <artifactId>spring-cloud-starter-consul-config</artifactId>
  10. <version> 2.1.2.RELEASE</version>
  11. </dependency>

  2.yml文件:        


   
   
  
  
          
  1. spring:
  2. application:
  3. name: demo
  4. cloud:
  5. consul:
  6. config:
  7. data-key: data
  8. prefix: config
  9. format: yaml
  10. host: consul的ip地址
  11. port: 端口
  12. discovery:
  13. prefer-ip-address: true
  14. server:
  15. port: 8080

 3.啟動類添加

@EnableDiscoveryClient 注解,sb2.0以后不需要添加,@EnableAutoConfiguration
  
  
 
 
         

4.配置consul,key/value

key:config/demo/data  (demo對應項目名,data對應data-key)

value: 原始sb的yml配置,我的是有縮進的可以讀取的,沒有縮進沒試過。

原文地址:https://blog.csdn.net/weixin_39556804/article/details/93893404

總結一下,看了這么多文章,都是模模糊糊,千篇一律,還是 stackoverflow好使,當然還有源碼。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM