一,基本介紹

yml 組態檔保存到git服務器,例如 github.com 或 gitee.com
微服務啟動時,從服務器獲取組態檔
二,準備作業
1)在父工程springcloud1下新建檔案夾 config

2)把2,3,4三個專案的組態檔,復制到config目錄,并修改名稱

注:一般運行配置用dev,測驗用test,生產用prod,官方檔案這么寫的,當然,取名沒有要求,這樣會顯得更專業些,
3)分別在三個組態檔中添加配置
默認情況下,git倉庫中的命令會覆寫本地命令引數,埠這樣的引數,不可以被隨便修改,所以設定不覆寫,
cloud:
config:
override-none: true

注意是在spring下
4)創建倉庫
雙擊 shift

可以選用父工程作為倉庫

5)提交檔案

沒創建遠程倉庫的話,創建遠程倉庫,
push

三,搭建配置中心服務器
1.新建模塊

添加eureka client,config server依賴

2.修改pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud1</artifactId>
<groupId>com.drhj</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.drhj</groupId>
<artifactId>sp09-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sp09-config</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.配置application.yml
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://gitee.com/ronghuading/springcloud1
search-paths: /config #/子目錄/子目錄/子目錄 不用寫/tree/master
server:
port: 6001
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
如果起不來,把注解洗掉了
注意,這里的uri是倉庫地址,search-paths是config所在倉庫的子目錄
4.啟動類添加@EnableConfigServer
package com.drhj.sp09;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
@SpringBootApplication
public class Sp09ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(Sp09ConfigApplication.class, args);
}
}
5.測驗
1)啟動eureka服務器,檢查是否又config-server 的注冊資訊
http://eureka1:2001/

2)訪問配置中心的組態檔
http://localhost:6001/item-service/dev

http://localhost:6001/user-service/dev

http://localhost:6001/order-service/dev

四,配置中心客戶端
1.注銷服務原有配置
將item-service,user-service,order-service的application.yml中的所有配置都注銷
2.添加config依賴
在三個服務的pom.xml中添加如下
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
3.配置bootstrap.yml
創建bootstrap.yml檔案,這里以user-service為例,因為user服務中添加用戶資訊的原因,所以沒有獲取配置資訊啟動不了
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
spring:
cloud:
config:
discovery:
enabled: true #從注冊中心發現配置中心的地址
service-id: config-server
name: user-service
profile: dev
item-serice,order-service同樣的配置,改變 對應的name
4,重啟服務
重啟user-service,獲取到倉庫配置,啟動成功


五,config bus + rabbitmq 訊息總線配置重繪

post 請求訊息總線重繪端點,服務器會向 rabbitmq 發布重繪訊息,接收到訊息的微服務會向配置服務器請求重繪配置資訊
Bus 是spring cloud config 提供的一個輔助完成訊息收發操作的組件以及配置的重繪
RabbitMQ詳見:RabbitMQ基礎運用
1.準備作業
在配置專案 config中添加 actuaor 依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
在需要重繪配置的專案即這里的 item-service,user-service,order-service和配置專案config中添加 Bus, RabbitMQ, binder-rabbit 依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
在配置專案config的yml檔案中添加配置
1)暴露 bus-refrush: m.e.w.e.i=bus-refrush
2)rabbitmq連接
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://gitee.com/ronghuading/springcloud1
search-paths: /config
rabbitmq:
host: 192.168.64.140
port: 5672
username: admin
password: admin
virtual-host: /drhj #如果創建了虛擬空間,則添加,注意加斜杠,使用默認的無需添加
server:
port: 6001
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
management:
endpoints:
web:
exposure:
include: bus-refresh
2.配置重繪
修改config目錄的三個檔案并提交
1)添加rabbitmq的連接

2)提交推送到遠程倉庫


啟動專案測驗
注意啟動順序是
eureka -> config -> item,user,order -> zuul
正常啟動后,清空控制臺資訊
使用postman 訪問 http://localhost:6001/actuator/bus-refresh 注意要把所有引數,請求頭等等都清空,最好創建新的測驗

查看控制臺

出現如上表示重繪成功
想只重繪某一個服務,添加子路徑即可,如:http://localhost:6001/actuator/bus-refresh/item-service


3.總結
所以要實作配置的更改,只需要在存放yml檔案的config中對對應的yml檔案進行修改,然后上傳git遠程倉庫,通過使用rabbitmq,手動bus重繪,即可完成配置的修改,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/335477.html
標籤:其他
上一篇:Kafka基礎----架構
