config-server-bus動態更新配(pei)置
config-server用來搭建配置中心,而配置信息一般使用gitlab倉庫來存儲,這樣在你的配置發生改變時,不需要從新打包,而如果使用native的試(shi),則需要從新(xin)打一(yi)個config-server的jar包。
配置的熱更新
當你(ni)(ni)的(de)服務的(de)配置(zhi)(zhi)信息發生改變(bian)時,一般來(lai)說需(xu)要從(cong)新(xin)重啟你(ni)(ni)的(de)服務,配置(zhi)(zhi)信息才能(neng)生效,這(zhe)對于我們來(lai)說是不友(you)好的(de),所以springcloud有(you)一種消息總線的(de)方式來(lai)實(shi)現配置(zhi)(zhi)信息的(de)熱(re)更新(xin),更你(ni)(ni)的(de)服務不需(xu)要從(cong)新(xin)啟動(dong)。
項目搭建
eureka-server
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
程序添加注解
@SpringBootApplication
@EnableEurekaServer
public class EurekaserverApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaserverApplication.class, args);
}
}
yml文件
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: //${eureka.instance.hostname}:${server.port}/eureka/
server:
enable-self-preservation: false #自我保護機制
eviction-interval-timer-in-ms: 30000 #及時踢出已關停的節點
config-server
它(ta)是配置中心(xin),其它(ta)服(fu)務(wu)如果通(tong)過config-server在eureka里的(de)服(fu)務(wu)名去連接它(ta),這種(zhong)是以(yi)eureka為(wei)(wei)核心(xin);也可(ke)以(yi)單(dan)獨指定,并把eureka的(de)信息寫到config-server倉庫里,這是以(yi)config-server為(wei)(wei)核心(xin),這兩種(zhong)方式都可(ke)以(yi),咱們這個例子是以(yi)eureka為(wei)(wei)核心(xin)的(de),所(suo)以(yi)config-server也是一個eureka-client.
<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>
程序添加注解
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigserverApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigserverApplication.class, args);
}
}
添加yml配置
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: git@github.com:***/config_repo.git
username: ***
password: ***
searchPaths: '{profile}'
server:
port: 8888
eureka:
client:
service-url:
defaultZone: //localhost:8761/eureka/
config-client-service1
這是我們具體的業務服務,它是一個eureka-client也是一個config-client,它需要把自己注冊到eureka里,也需要從config-server拉自己的信息,它需要有配置信息的熱更新,所以這需要引用amqp包和actuator健康檢測包。
<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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
程序添加注解
@SpringBootApplication
@EnableDiscoveryClient
@RestController
@RefreshScope
public class Service1Application {
@Value("${auth.name:empty}")
String author;
public static void main(String[] args) {
SpringApplication.run(Service1Application.class, args);
}
@GetMapping("/hello")
public String hello() {
return author;
}
}
添加yml文件
spring:
cloud:
bus.trace.enabled: true #配置動態更新
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
config:
discovery:
enabled: true #這塊表示啟用service-id不用uri
service-id: config-server #這塊是服務的id
label: master
profile: svt
application:
name: service1
server:
port: 8081
eureka:
instance:
prefer-ip-address: true #基于IP地址的注冊而不是主機名
client:
service-url:
defaultZone: //localhost:8761/eureka
logging:
level:
org:
springframework:
security: DEBUG
management:
endpoints:
web:
exposure:
include: bus-refresh
配置更新
當你的config_repo倉(cang)庫有文件更新時,你(ni)可以調(diao)用任(ren)意一(yi)個服務去(qu)觸發它,測(ce)試(shi)的代(dai)碼如
curl -v -X POST "//localhost:8081/actuator/bus-refresh"
如果成功后,一般會返回httpstatuscode:204的狀態碼,當(dang)(dang)然這種(zhong)也是(shi)手(shou)動(dong)更新,如果希望(wang)動(dong)態更新,可以在gitlab或者(zhe)github上對config_repo項目添加webhook的事件,當(dang)(dang)有分支合(he)并(bing)到dev或者(zhe)master時,去自(zi)動(dong)觸發(fa)bus-refresh。