1.配置中心是如何工做的?java
配置項怎麼配置? 經過配置文件進行配置並存儲在git中 誰去讀配置文件? configserver配置中心 應用系統中配置信息從哪來? 從configserver遠程獲取git
2.RefreshScope註解很是好用,可是若是是數據庫配置怎麼辦?默認上面沒有加註解吧?github
首先,通常狀況下,咱們不會對數據庫的地址發動更新,可是若是一旦須要修改而數據庫的鏈接若是須要熱部署,web
咱們須要重寫數據庫的鏈接配置bean,而後在bean上加入註解@RefreshScope算法
@Configuration public class DataSourceConfigure { @Bean @RefreshScope// 刷新配置文件 @ConfigurationProperties(prefix="spring.datasource") // 數據源的自動配置的前綴 public DataSource dataSource(){ return DataSourceBuilder.create().build(); } }
3.配置中心的一切都會直接暴露,那麼安全性如何保證?spring
一般來講配置中心不該該直接暴露出去,由於這頗有可能會出現安全性問題。因此咱們須要對配置中心進行帳號密碼設置。數據庫
設置方式很是簡單--在config-server的pom.xml下引入bootstrap
<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.