Spring-cloud 微服務(wù)架構(gòu)搭建 02 - config-server 集成git動態(tài)刷新配置及安全管理

1. sping-cloud config簡介

微服務(wù)的體系中,配置文件的統(tǒng)一管理是非常有必要的,我們需要替代人為手動維護配置文件的責(zé)任,因為在大型的微服務(wù)體系中我們需要維護的配置成百上千份,很容易發(fā)生配置漂移。此時,Spring-Cloud Config可以給我們提供配置的統(tǒng)一管理和分發(fā),以及自動化刷新配置的需求。

2. sping-cloud config 服務(wù)特點

2.1. 分離性

Config配置中心支持將配置文件存入本地或者無縫銜接git管理的特性能夠集中進行版本的控制和配置文件的管理;

2.2. 抽象性

我們在獲取配置文件時,不需要自己進行配置文件讀取操作,可以通過http請求配配置中心提供的服務(wù)進行配置文件讀取,并且集成bus總線可以動態(tài)IDE刷新配置,并且刷新所有的服務(wù)實例無需重啟機器;

2.3. 集中性

所有的配置文件信息集中存儲在配置中心,有效的進行數(shù)據(jù)的版本統(tǒng)一管理。

2.4. 穩(wěn)定性

作為spring旗下項目,與eureka、consul等緊密結(jié)合,可實現(xiàn)高可用部署,降低服務(wù)奔潰的風(fēng)險,實現(xiàn)拉取一份配置文件本地緩存等特性來降低風(fēng)險,保證了高可用和冗余。

3. Config-Server 服務(wù)端搭建

注:本文項目采用idea工具進行搭建

  • 使用idea自身的spring initializr進行項目的初始化,集成git使用的rabbitmq請自行搜索安裝,如果是mac,請直接使用brew install rabbitmq即可
  • 初始化完成項目之后進行pom文件導(dǎo)入
<!-- config-server 啟動引入 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- security 安全控制 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- eureka 服務(wù)注冊 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 消息總線 配置動態(tài)配置中心 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
  • 修改application.yml文件,添加如下配置:
spring:
  application:
    name: config-server
#  profiles:
#    active: native    #開啟本地配置 默認(rèn)為git
#  cloud:
#    config:
#      server:
#        native:
#          search-locations: classpath:config-repo
## git管理配置
  cloud:
    config:
      retry:
        initial-interval: 3000
        multiplier: 1.5
        max-interval: 20000
        max-attempts: 6
      server:
        git:
          uri: #個人git地址
          search-paths: '{application}'
          username: # 你個人的git賬戶
          password: # git密碼
          default-label: master
  rabbitmq:
    host: localhost
    port: 5672
    username: guest #默認(rèn)密碼
    password: guest #默認(rèn)密碼
  # 安全認(rèn)證 
  security:
    user:
      name: luhanlin
      password: ***
      
## 暴露所有監(jiān)控端口 主要暴露動態(tài)刷新接口
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS
  • 修改bootstrap.yml文件,鏈接eureka-config,添加如下配置:
#服務(wù)注冊中心配置
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/  #,http://xxxxx:8761/eureka/
      # 指定多少秒從注冊中心獲取一次實例服務(wù)列表 默認(rèn) 30秒
      # 減少值可以解決服務(wù)注冊慢問題,但一般不要設(shè)置太小
      registry-fetch-interval-seconds: 20
  instance:
    # 獲取實例ip地址
    prefer-ip-address: true
    # 心跳包發(fā)送時間 默認(rèn) 30秒
    lease-renewal-interval-in-seconds: 60
    # 最后一次心跳時間間隔以下值之后清楚實例列表中的值,不應(yīng)該小于心跳檢測時間 默認(rèn)90秒
    lease-expiration-duration-in-seconds: 100
#    instance-id: config-server
  • 最后在Config服務(wù)啟動實例上面添加一下注解:
@EnableDiscoveryClient  // 開啟服務(wù)注冊
@EnableConfigServer     // 開啟配置中心服務(wù)端
@SpringBootApplication
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

最后啟動Eureka—server,在啟動Config-Server就可以了,注意:rabbitmq服務(wù)需要啟動,不然會報錯。

4. Config-Client 端搭建

  • 此處我以Demo-Service項目為例。

  • 首先我們構(gòu)建一個maven工程,初始化完成后進行pom引入(沒有加入boot監(jiān)控依賴):

<!-- Eureka客戶端啟動類 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- Config客戶端啟動類 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-client</artifactId>
</dependency>
<!-- 消息總線 配置動態(tài)配置中心 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
  • 修改我們的配置文件Bootstrap.yml, 配置如下:
spring:
  application:
      name: demo
  # 配置中心服務(wù)的地址
  cloud:
    config:
      discovery:
        # 默認(rèn)false,設(shè)為true表示使用注冊中心中的config-server配置而不自己配置config-server的uri
        enabled: true
        service_id: config-server
      fail-fast: true
      name: demo
      profile:  dev # 要讀取的配置文件profile屬性
      # 使用git管理配置文件時指定
      label: master
      username: luhanlin
      password: ***
# 指定服務(wù)注冊中心的位置。
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    hostname: localhost
    preferIpAddress: true
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS
  • 配置完后需要在Client啟動類上加上一下注解:
@RefreshScope   # GIT動態(tài)刷新注解
@EnableDiscoveryClient # 服務(wù)發(fā)現(xiàn)

5. 動態(tài)刷新配置測試

// 此類進行配置文件的信息讀取,**使用對象進行測試會更加準(zhǔn)確**
@Data
public class CustomBean {
    private String id;
    private String name;
    private String version;
}

@Configuration  // 配置類,如不熟悉可以先行進行spring-b
public class BusinessConfig {

    @Bean
    @ConfigurationProperties(prefix="custom.bean")
    public CustomBean initCustomBean(){
        log.info(">>>>>>>>>>> CustomBean >>>>>>>>>>>>>>> 配置成功!!!");
        return new CustomBean();
    }
}

@RestController  // 對外api提供,最后返回配置中信息
@RequestMapping(value = "/test")
public class TestController {

    @Autowired
    private CustomBean customBean;

    @GetMapping(value = "/hello")
    public ResultInfo hello(){
        return  ResultUtil.success(customBean.getVersion());
    }
}

// 配置文件(demo-dev.yml,位于個人倉庫中)添加如下配置:
custom:
  bean:
    id: 100
    name: luhanlin
    version: 1.0.1
{
    "code": "I0000",
    "msg": "請求成功",
    "data": "1.0.1"
}
  • 修改配置倉庫中的配置文件,并提交到push到遠(yuǎn)程git倉庫
custom:
  bean:
    id: 100
    name: luhanlin
    version: 1.0.9
  • 這是再次訪問http://localhost:8080/test/hello,發(fā)現(xiàn)返回信息沒。使用POST請求訪問http://localhost:7001/actuator/bus-refresh(老版使用fresh/refresh)進行屬性的刷新,發(fā)現(xiàn)403無法訪問請求,因為我在配置中添加了security配置需要進行安全認(rèn)證,而新版的boot和cloud中需要Config-server中進行以下配置才行:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    /**
     * 高版本的丟棄了 
     * 
     * security: 
     *   basic: 
     *    enabled: true 
     * 
     * 配置,應(yīng)該使用以下方式開啟
     *
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // Configure HttpSecurity as needed (e.g. enable http basic).
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
        http.csrf().disable();
        //注意:為了可以使用 http://${user}:${password}@${host}:${port}/eureka/ 這種方式登錄,所以必須是httpBasic,
        // 如果是form方式,不能使用url格式登錄
        http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
    }
}
{
    "code": "I0000",
    "msg": "請求成功",
    "data": "1.0.9"
}
  • 配置成功

6. config-server配置RSA加密

  • 由于我們配置文件時很多需要加密的信息不宜暴露給外界,需要進行各種加密操作,其中spring-cloud提供了對稱加密和RSA非對稱加密的形式,此處我們使用RSA加密的方式,通過Config暴露的entrypt接口進行加密,我們只需要在配置文件中使用{cipher},如:“{cipher}shdshdswwhxxxxxxxx66x65xsdsdsd”,其中在yml配置文件中一定要使用雙引號包含起來,不然會出錯,詳細(xì)的RSA配置大家可以自行搜索博客進行配置。

本文github代碼地址
spring-cloud 基礎(chǔ)模塊搭建 -- 歡迎指正

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,622評論 6 544
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,716評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,746評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,991評論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 72,706評論 6 413
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 56,036評論 1 329
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,029評論 3 450
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 43,203評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,725評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 41,451評論 3 361
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,677評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,161評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,857評論 3 351
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,266評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,606評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,407評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 48,643評論 2 380

推薦閱讀更多精彩內(nèi)容