config-server用來搭建配置中心,而配置信息一般使用gitlab倉庫來存儲,這樣在你的配置發生改變時,不需要從新打包,而如果使用native
的試,則需要從新打一個config-server的jar包。
配置的熱更新
當你的服務的配置信息發生改變時,一般來說需要從新重啟你的服務,配置信息才能生效,這對於我們來說是不友好的,所以springcloud有一種消息總線的方式來實現配置信息的熱更新,更你的服務不需要從新啟動。
項目搭建
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: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
enable-self-preservation: false #自我保護機制
eviction-interval-timer-in-ms: 30000 #及時踢出已關停的節點
config-server
它是配置中心,其它服務如果通過config-server在eureka里的服務名去連接它,這種是以eureka為核心;也可以單獨指定,並把eureka的信息寫到config-server倉庫里,這是以config-server為核心,這兩種方式都可以,咱們這個例子是以eureka為核心的,所以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: http://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: http://localhost:8761/eureka
logging:
level:
org:
springframework:
security: DEBUG
management:
endpoints:
web:
exposure:
include: bus-refresh
配置更新
當你的config_repo
倉庫有文件更新時,你可以調用任意一個服務去觸發它,測試的代碼如
curl -v -X POST "http://localhost:8081/actuator/bus-refresh"
如果成功后,一般會返回httpstatuscode:204
的狀態碼,當然這種也是手動更新,如果希望動態更新,可以在gitlab或者github上對config_repo項目添加webhook的事件,當有分支合並到dev或者master時,去自動觸發bus-refresh。