Spring Cloud Config整合RabbitMQ 由於版本問題導致的坑


最近閑來無事就整合了一下Spring Cloud Config跟RabbitMQ,遇到了不少坑,記錄一波。

先從安裝MQ開始吧:

第一步:先安裝Erlang,下載地址:https://www.erlang.org/  安裝步驟就省略了。

第二步:安裝RabbitMQ,下載地址:https://www.rabbitmq.com/download.html 安裝步驟同樣省略,不會自行百度吧。

第三步:啟動RabbitMQ:

      1、進入到sbin目錄下執行:rabbitmq-plugins enable rabbitmq_management

      2、開啟服務執行:rabbitmq-server

      打開瀏覽器訪問:http://localhost:15672

      默認UserName:guest    Password:guest

      注意端口:15672

登錄成功后記得添加一個用戶,用作項目登錄,添加用戶時記得添加權限哦!!!!

到此為止算是完成一小步了。接下來就是項目配置了,分為server端和client端。

server端配置:

  1.pom.xml:

 1 <dependency>
 2             <groupId>org.springframework.cloud</groupId>
 3             <artifactId>spring-cloud-starter-bus-amqp</artifactId>
 4             <version>2.2.1.RELEASE</version>
 5         </dependency>
 6         <dependency>
 7             <groupId>org.springframework.cloud</groupId>
 8             <artifactId>spring-cloud-bus</artifactId>
 9             <version>2.2.1.RELEASE</version>
10         </dependency>
11 
12         <dependency>
13             <groupId>org.springframework.cloud</groupId>
14             <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
15             <version>3.0.3.RELEASE</version>
16         </dependency>
17 
18         <dependency>
19             <groupId>org.springframework.boot</groupId>
20             <artifactId>spring-boot-starter-actuator</artifactId>
21         </dependency>

注意:Spring boot 2.0的改動較大,/bus/refresh全部整合到actuador里面了,所以 spring-boot-starter-actuator 不能少

2.application.properties:

 1 spring.application.name=spring-cloud-config-server
 2 
 3 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
 4 
 5 server.port=8040
 6 
 7 # 配置git倉庫的地址
 8 spring.cloud.config.server.git.uri=https://github.com/isTeemo/spring-cloud-config.git
 9 # git倉庫地址下的相對地址,可以配置多個,用,分割。
10 spring.cloud.config.server.git.search-paths=neo-config
11 spring.cloud.config.label=master
12 # git倉庫的賬號
13 spring.cloud.config.server.git.username=14 # git倉庫的密碼
15 spring.cloud.config.server.git.password=
16 
17 ## 刷新時,關閉安全驗證
19 management.endpoints.web.exposure.include=bus-refresh
20 spring.cloud.bus.refresh.enabled=true
21 ## 開啟消息跟蹤
22 #spring.cloud.bus.trace.enabled=true
23 
24 spring.rabbitmq.host=localhost
25 spring.rabbitmq.port=5672
26 spring.rabbitmq.username=admin
27 spring.rabbitmq.password=admin

注意:

  a.RabbitMQ的端口為:15672 ,但是這里配置必須寫成:5672。

  b.刷新關閉安全驗證配置,在Spring boot 1.x用:management.security.enabled=false,但是2.0后得用:management.endpoints.web.exposure.include=bus-refresh

  c.請求刷新地址:1.x版本為:http://ip:端口/bus/refresh(如:http://localhost:8040/bus/refresh),2.0以后為:http://ip:端口/actuator/bus-refresh(如:http://localhost:8040/actuator/bus-refresh),特別注意:兩個都是POST請求

 

接下來是client端,:

 1.pom.xml(跟server端一樣):

 1 <dependency>
 2             <groupId>org.springframework.cloud</groupId>
 3             <artifactId>spring-cloud-starter-bus-amqp</artifactId>
 4             <version>2.2.1.RELEASE</version>
 5         </dependency>
 6         <dependency>
 7             <groupId>org.springframework.cloud</groupId>
 8             <artifactId>spring-cloud-bus</artifactId>
 9             <version>2.2.1.RELEASE</version>
10         </dependency>
11 
12         <dependency>
13             <groupId>org.springframework.cloud</groupId>
14             <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
15             <version>3.0.3.RELEASE</version>
16         </dependency>
17 
18         <dependency>
19             <groupId>org.springframework.boot</groupId>
20             <artifactId>spring-boot-starter-actuator</artifactId>
21         </dependency>

注意:Spring boot 2.0的改動較大,/bus/refresh全部整合到actuador里面了,所以 spring-boot-starter-actuator 不能少

2.bootstrap.properties:

 1 spring.application.name=spring-cloud-config-client
 2 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
 3 server.port=8041
 4 
 5 
 6 spring.cloud.config.name=neo-config
 7 spring.cloud.config.profile=dev
 8 spring.cloud.config.label=master
 9 
10 spring.cloud.config.discovery.enabled=true
11 spring.cloud.config.discovery.serviceId=spring-cloud-config-server
12 
13 ## 刷新時,關閉安全驗證
14 management.endpoints.web.exposure.include=bus-refresh
15 ## 開啟消息跟蹤
16 spring.cloud.bus.trace.enabled=true
17 
18 spring.rabbitmq.host=localhost
19 spring.rabbitmq.port=5672
20 spring.rabbitmq.username=admin
21 spring.rabbitmq.password=admin

注意點跟server端一樣。

特別注意:client端在獲取配置的方法上需要加上 @RefreshScope 注解。

其實這是坑都是自己不讀官方文檔導致的,作為一個程序猿,應該養成多逛逛官方網站的習慣,畢竟現在的東西迭代速度非常快,一不小心就落后了。

另外,我在看其他大神文章時看到一個問題,由於本人剛剛接觸這個東西,所以沒有發現,也不曉得是不是真的,但是還是記錄一下吧,希望對遇到的人有用。


免責聲明!

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



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