爲什麼要使用SpringCloudConfig和SpringCloudBus
在分佈式系統中,配置文件很多,不太方便管理;於是就出現了配置中心組件SpringCloudConfig,來幫助我們管理配置文件,實時更新。它支持將配置文件存放在git中;而SpringCloudBus可以讓我們的服務不用重啓,配置就能修改。
服務器
1. pom依賴
<!--config-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- Spring Cloud Bus配置 -->
<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>
2. 配置文件
server:
port: 12000
spring:
application:
name: tensquare-config
cloud:
config:
server:
git:
uri: https://gitee.com/hanmtou/tensquare-config.git
rabbitmq:
host: 106.13.191.207
management: # 暴露觸發消息總線的地址
endpoint:
web:
exposure:
include: bus-refresh
3. 啓動類
@SpringBootApplication
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class,args);
}
}
測試
在gitee創建一個倉庫
將配置文件user-dev.yml上傳到git上;配置中心對配置文件的名是有格式要求的
文件命名規則:
{ApplicationName}-{profile}.yml或{ApplicationName}-{profile}.properties application爲應用名稱 profile指的開發環境(用於區分開發環境,測試環境、生產環境 等)
啓動服務器;
http://localhost:12000/user-dev.yml
這就說明服務器搭建成功了。
客戶端
1. pom依賴
<!--config-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- spring cloud bus -->
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2. bootstarp.yml
spring:
cloud:
config:
name: eureka
label: master #git的哪個分支
profile: dev #profile
uri: http://127.0.0.1:12000 #配置中心服務器地址和端口
**bootstrap.yml(bootstrap.properties)**用來在程序引導時執行,應用於更加早期配置信息讀取,如可以使用來配置application.yml中使用到參數等
application.yml(application.properties) 應用程序特有配置信息,可以用來配置後續各個模塊中需使用的公共參數等。
bootstrap.yml 先於 application.yml 加載
3. git上的配置文件user-dev
server:
port: 9009
spring:
application:
name: tensquare-user # 模塊名
datasource: # 配置數據源
druid:
url: jdbc:mysql://127.0.0.1:3306/tensquare_user?characterEncoding=UTF-8&useUnicode=true&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
username: 用戶名
password: 密碼
driverClassName: com.mysql.cj.jdbc.Driver
initialSize: 5 #初始建立連接數量
minIdle: 5 #最小連接數量
maxActive: 20 #最大連接數量
maxWait: 10000 #獲取連接最大等待時間,毫秒
testOnBorrow: true #申請連接時檢測連接是否有效
testOnReturn: false #歸還連接時檢測連接是否有效
timeBetweenEvictionRunsMillis: 60000 #配置間隔檢測連接是否有效的時間(單位是毫秒)
minEvictableIdleTimeMillis: 300000 #連接在連接池的最小生存時間(毫秒)
jpa: # 配置jpa
database: mysql
show-sql: true
redis:
host: 106.13.191.207
port: 6380
rabbitmq:
host: 106.13.191.207
eureka:
client:
serviceUrl:
defaultZone: http://127.0.0.1:6868/eureka/
instance:
prefer-ip-address: true # 跨域訪問
testbus: 6666
在修改完配置文件後:以post 方式 http://127.0.0.1:12000/actuator/bus-refresh 進行更新
@RefreshScope:是spring cloud提供的一種特殊的scope實現,用來實現配置、實例熱加載。該註解可更新自定義註解如修改testbus值:
@RestController
@RefreshScope
@RequestMapping("/user")
public class UserController {
@Value("${testbus}")
private String testbus;
}