配置中心高級應用及基本原理
1.配置中心是如何工作的?
配置項怎麼配置? 通過配置文件進行配置並存儲在git中 誰去讀配置文件? configserver配置中心 應用系統中配置信息從哪來? 從configserver遠程獲取
2.RefreshScope註解非常好用,但是如果是數據庫配置怎麼辦?默認上面沒有加註解吧?
首先,一般情況下,我們不會對數據庫的地址發動更新,但是如果一旦需要修改而數據庫的連接如果需要熱部署,
我們需要重寫數據庫的連接配置bean,然後在bean上加入註解@RefreshScope
@Configuration
public class DataSourceConfigure {
@Bean
@RefreshScope// 刷新配置文件
@ConfigurationProperties(prefix="spring.datasource") // 數據源的自動配置的前綴
public DataSource dataSource(){
return DataSourceBuilder.create().build();
}
}
3.配置中心的一切都會直接暴露,那麼安全性如何保證?
通常來說配置中心不應該直接暴露出去,因爲這很有可能會出現安全性問題。所以我們需要對配置中心進行賬號密碼設置。
設置方式非常簡單--在config-server的pom.xml下引入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
修改配置中心的配置文件:application.yml
server:
port: 5000
spring:
application:
name: config-server
cloud:
config:
server:
git:
# uri: https://github.com/yaojin156/tmp.git
# force-pull: true
##定義去拉取配置的地址
uri: D:\\lesson-1\\config-server\\src\\main\\resources\\configs
###引入賬號密碼機制
security:
user:
password: 123456
name: nick
當我們加上這個配置之後,訪問配置中心就需要密碼了。
相應的,我們需要在客戶端也給上配置--修改hello-demo的bootstrap.yml
server:
port: 8001
spring:
application:
name: helloclient
cloud:
config:
name: helloclient
uri : http://localhost:5000/
username: nick
password: 123456
profiles:
active: dev
這裏是第一個保護措施.
但是,我們的配置中某些重要東西不應當以明文展示在觀衆的視野下,如協議頭:
所以,我們還可以給重要的東西進行加密計算--
如果我們定義了賬號密碼加密方式,那麼我們需要在security去自定義一些配置:
創建WebSecurityConfig
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//Spring Security不會創建HttpSession,但如果它已經存在,將可以使用HttpSession
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
// 在 @EnableWebSecurity配置中,禁用CSRF。
http.csrf().disable();
//注意:爲了可以使用 http://${user}:${password}@${host}:${port}/eureka/ 這種方式登錄,所以必須是httpBasic,
// 如果是form方式,不能使用url格式登錄
http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
}
}
修改配置文件application.yml 加入配置--啓用加密算法:
server:
port: 5000
spring:
application:
name: config-server
cloud:
config:
server:
git:
# uri: https://github.com/yaojin156/tmp.git
# force-pull: true
uri: D:\\lesson-1\\config-server\\src\\main\\resources\\configs
###啓用加密解密算法
encrypt:
enabled: true
security:
user:
password: 123456
name: nick
添加bootstrap.yml添加祕鑰
encrypt:
key: "A123456c"
這很明顯是對稱性加密算法,這裏就實現了加密算法
訪問http://localhost:5000/encrypt/ 將賬號密碼寫入,在body中寫入字符。會生成密文:
header: '{cipher}6f252b94ecd2a3ecebe3b93e0c3d6e11b506e8e5cc4b7a02eac40ac05a1ea0d9
按照這種規則寫入,在客戶端獲取的時候會通過解密,然後得到我們的結果.
可以直接通過這個地址調用http://localhost:5000/decrypt獲得解密後的結果.
4.配置中心的加密解密算法在哪裏?怎麼實現的?
加密是EncryptionController#encrypt()
解密是EncryptionController#decrypt()
5.客戶端怎麼到服務器端去讀取配置的?
PropertySourceBootstrapConfiguration#initialize
這下面去尋找 locator.locate(environment)
通過實現類ConfigServicePropertySourceLocator
找到restTemlate
6.服務端是如何知道客戶端要來讀取配置的?如何接收?
EnvironmentController#labelled()
7.bus機制和自動刷新
操作:我們可以使用spring-cloud-bus來實現全局刷新--以rabbitMQ用來做消息通知與推送
操作如下--在config-server下引入bus相關jar包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
同時,因爲我們是用rabbitMQ來做消息通知與推送,所以我們需要加入配置:
server:
port: 5000
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/yaojin156/tmp.git
force-pull: true
##定義去拉取配置的地址
# uri: D:\\lesson-1\\config-server\\src\\main\\resources\\configs
###啓用加密解密算法
encrypt:
enabled: true
###引入賬號密碼機制
security:
user:
password: 123456
name: nick
rabbitmq:
host: localhost
username: guest
password: guest
port: 5672
在客戶端我們也需要集成消息總線--在hello-demo中加入配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
然後,我們去修改配置,提交到git上.
訪問配置中心地址:http://localhost:5000/actuator/bus-refresh 我們會看到,配置了消息總線的客戶端都會接到刷新通知.
注意:自動刷新,需要我們通過鉤子的配置方式,可以在gitlab上面定義一個webhook,來通過它向我們發送事件,那麼這裏就可以來接收到事件了。一旦接收到鉤子發佈的事件,我們這裏就會通知客戶端來獲取新的配置。
方式:引入monitor的jar包,然後將http://localhost:5000/monitor 添加到webhook上,然後就完成了
8.我們演示了配置中心,那麼我們是如何把它和註冊中心結合起來的?
將註冊中心作爲中心應用,注意,註冊中心作爲中心點來用的時候,其他的demo需要把註冊中心寫好
1.修改eurekaServer配置文件,不再從註冊中心獲取配置
2.修改client配置文件,不再從註冊中心獲取配置中心的地址
3.修改配置中心,引入jar,將自己定義爲一個客戶端
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
啓動類加上註解@EnableEurekaClient
4.修改hello-demo中的配置,將url地址改爲service-id.