準(zhǔn)備工作
環(huán)境以及版本:
tools | vision |
---|---|
IDEA | 2018.3 |
SpringBoot | 2.1.3 RELEASE |
Gradle | 5.2.1+ |
JDK | 1.8 |
Spring Cloud | Greenwich.RELEASE |
說(shuō)明
以下改造均在子項(xiàng)目進(jìn)行操作,父項(xiàng)目的github地址贞间,下述需下載父項(xiàng)目用來(lái)管理公共依賴:
https://github.com/cuifuan/springcloud-tools
1.改造你的 config-server
1.1 構(gòu)建配置文件 build.gradle
dependencies {
implementation "org.springframework.cloud:spring-cloud-config-server"
implementation "org.springframework.cloud:spring-cloud-bus"
implementation "org.springframework.cloud:spring-cloud-starter-bus-amqp"
}
1.2 配置文件 application.yml
server:
port: 7001
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: git地址 #例如https://github.com/cuifuan/springcloud-configuration.git
search-paths: 倉(cāng)庫(kù)文件下文件夾
default-label: master
username: git賬號(hào)
password: git密碼
bus:
trace:
enabled: true
rabbitmq:
host: rabbitmq地址
port: 5672
username: rabbit賬號(hào)【默認(rèn):guest】
password: rabbit密碼【默認(rèn):guest】
virtual-host: /
eureka:
# 修改在服務(wù)中心的地址status為 ip+端口 【例如:10.0.0.100:88】
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${server.port}
client:
serviceUrl:
defaultZone: http://springcloud-tools:8761/eureka/
info:
app:
description: This is Spring Cloud remote Registration center.
name: tools-config-server
version: 0.0
management:
endpoint:
bus-refresh:
enabled: true
endpoints:
web:
exposure:
include: refresh,bus-refresh
1.3 啟動(dòng)類(lèi) ToolsConfigServerAppliaction.java
package store.zabbix.config;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@EnableConfigServer
@SpringCloudApplication
@RestController
public class ToolsConfigServerAppliaction {
public static void main(String[] args) {
SpringApplication.run(ToolsConfigServerAppliaction.class, args);
}
@RequestMapping("/")
public String home() {
return "Hello World! My name is configserver.";
}
}
2. 改造你的 config-client (客戶端)
用來(lái)讀取配置文件的
2.1 構(gòu)建配置文件 build.gradle
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-config'
implementation "org.springframework.cloud:spring-cloud-bus"
implementation "org.springframework.cloud:spring-cloud-starter-bus-amqp"
}
2.2 配置文件 bootstrap.yml
spring:
cloud:
config:
name: tools-config-client #對(duì)應(yīng){application}部分
profile: dev #對(duì)應(yīng){profile}部分
#uri: http://localhost:8888/ #配置中心的具體地址
label: master #對(duì)應(yīng)git的分支贿条。如果配置中心使用的是本地存儲(chǔ),則該參數(shù)無(wú)用
discovery:
enabled: true #開(kāi)啟Config服務(wù)發(fā)現(xiàn)支持
service-id: config-server #指定配置中心的service-id增热,便于擴(kuò)展為高可用配置集群整以。
eureka:
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${server.port}
client:
service-url:
defaultZone: http://springcloud-tools:8761/eureka/
2.2 配置文件 application.yml
server:
port: 7003
spring:
application:
name: tools-config-client
cloud:
config:
#配置重試機(jī)制
retry:
initial-interval: 2000
max-attempts: 2000
max-interval: 2000
multiplier: 1.2
fail-fast: true
bus:
#動(dòng)態(tài)刷新配置
refresh:
enabled: true
#跟蹤總線事件
trace:
enabled: true
rabbitmq:
host: rabbitmq地址
port: 5672
username: rabbit賬號(hào)【默認(rèn):guest】
password: rabbit密碼【默認(rèn):guest】
#配置actuator
# 1.X版本的springboot 配置: management.security.enabled=false 已經(jīng)作廢
#關(guān)閉安全認(rèn)證
management:
endpoint:
bus-refresh:
enabled: true
#refresh接入點(diǎn)顯式暴露出來(lái)
endpoints:
web:
exposure:
include: refresh,bus-refresh
2.3 啟動(dòng)類(lèi) ConfigClientApplication.java
注意:一定不要忘了加
@RefreshScop
注解
package store.zabbix.configreader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
@RefreshScope
@EnableDiscoveryClient
public class ConfigClientApplication {
/** * http://localhost:8881/actuator/bus-refresh */
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
@Value("${message}")
String message;
@GetMapping("/hello")
public String getMessage(){
return message;
}
}
3.遠(yuǎn)程配置文件
image
4.操作流程
- 1 啟動(dòng)config-server
- 2 啟動(dòng)config-client
- 3 訪問(wèn)http://127.0.0.1:7003/hello
- 4 之后更改遠(yuǎn)程倉(cāng)庫(kù)配置文件,提交
- 5 訪問(wèn)http://127.0.0.1:7003/hello發(fā)現(xiàn)無(wú)變化
- 6 用postman做post請(qǐng)求http://10.0.0.82:7001/actuator/bus-refresh
或者用
curl -X POST http://10.0.0.82:7001/actuator/bus-refresh
- 7 再次訪問(wèn)http://127.0.0.1:7003/hello
這個(gè)時(shí)候的讀取的配置文件已發(fā)生變化
5.需要注意的坑
- 請(qǐng)確定你的rabbitmq是通的峻仇,guest賬戶無(wú)法通過(guò)遠(yuǎn)程ip訪問(wèn)
- 注意上面的ip地址公黑,我是這邊測(cè)試使用,請(qǐng)更改成個(gè)人正確或者本地
- springboot2.x之后bus刷新地址更改為
/actuator/bus-refresh
- springboot和springcloud版本要一致摄咆,其他版本沒(méi)試過(guò)