Spring Cloud系列之配置中心Config

Spring Cloud系列之Eureka
Spring Cloud系列之配置中心Config
Spring Cloud系列之gateway
Spring Cloud系列之Feign
Spring Cloud系列之Hystrix
Spring Cloud系列之鏈路追蹤

配置中心可以簡單的理解為一個服務(wù)模塊佑惠,開發(fā)人員或運維人員可以通過界面對配種中心進行配置,下面相關(guān)的微服務(wù)連接到配置中心上面就可以實時連接獲取到配置中心上面修改的參數(shù)齐疙。更新的方式一般有兩種

  • pull模式膜楷,服務(wù)定時去拉取配置中心的數(shù)據(jù)

  • push模式,服務(wù)一直連接到配置中心上剂碴,一旦配置有變成把将,配種中心將把變更的參數(shù)推送到對應(yīng)的微服務(wù)上

這兩種做法其實各有利弊

  • pull可以保證一定可以拉取得到數(shù)據(jù),pull一般采用定時拉取的方式忆矛,即使某一次出現(xiàn)網(wǎng)絡(luò)沒有拉取得到數(shù)據(jù)察蹲,那在下一次定時器也將可以拉取得到數(shù)據(jù),最終保證能更新得到配置

  • push也有好處催训,避免pull定時器獲取存在時延洽议,基本可以做到準(zhǔn)實時的更新,但push也存在問題漫拭,如果有網(wǎng)絡(luò)抖動亚兄,某一次push沒有推送成功,將丟失這次配置的更新

目前比較流行的配種中心開源組件有Spring Cloud Config采驻,百度的disconf审胚,阿里的Nacos,還有攜程的apollo礼旅。

實現(xiàn)原理大同小異

配置中心.png

實踐

config server搭建
  • 引入依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>dy-springcloud</artifactId>
        <groupId>com.dy</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.dy</groupId>
    <artifactId>config-server</artifactId>

    <dependencies>

        <!--config server端依賴-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

        <!--config server端也注冊到注冊中心-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

    </dependencies>

</project>
  • 配置文件
#eureka 參數(shù)配置
eureka:
  # 此實例注冊到eureka服務(wù)端的唯一的實例ID,其組成為${spring.application.name}:${spring.application.instance_id:${random.value}}
  instance:
    #  與此實例相關(guān)聯(lián)的主機名膳叨,是其他實例可以用來進行請求的準(zhǔn)確名稱
    hostname: localhost
    # 獲取實例的ip地址
    prefer-ip-address: true
    #  eureka客戶需要多長時間發(fā)送心跳給eureka服務(wù)器,表明它仍然活著,默認(rèn)為30 秒
    lease-renewal-interval-in-seconds: 10
    #  Eureka服務(wù)器在接收到實例的最后一次發(fā)出的心跳后痘系,需要等待多久才可以將此實例刪除菲嘴,默認(rèn)為90秒
    lease-expiration-duration-in-seconds: 30
  client:
    # 注冊自己
    register-with-eureka: true
    # 是否拉取服務(wù)
    fetch-registry: true
    # 配置注冊中心服務(wù)地址
    service-url:
      # eureka 服務(wù)地址
      defaultZone: http://172.16.10.16:10001/eureka
server:
  port: 10002
spring:
  application:
    name: config-server
  cloud:
    # cloud配置中心相關(guān)配置
    config:
      server:
        # git相關(guān)配置
        git:
          # git 地址
          uri: https://gitee.com/dai-yong-1/config-server-dy.git
          # git用戶名
          username: xxxx
          # git密碼
          password: xxxxx
          # 配置文件所在文件夾
          search-paths: config-yml

配置中心git倉庫如下:

配置中心git倉庫.png
  • 啟動類
/**
 * @Description:
 * @author: dy
 */
@EnableConfigServer  //聲明當(dāng)前應(yīng)用是config server服務(wù)
@EnableDiscoveryClient //開啟Eureka客戶端發(fā)現(xiàn)功能
@SpringBootApplication
public class ConfigServerApplication {

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

}
config client 搭建
  • 引入依賴
<dependencies>

    <!--配置中心相關(guān)依賴-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>

    <!--erueka客戶端相關(guān)依賴-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>

</dependencies>
  • 配置類
server:
  port: 11001
spring:
  application:
    name: user-service
  cloud:
    config:
      enabled: true
      # 與遠程倉庫中的配置文件的application保持一致
      name: user-service
      # 遠程倉庫中的配置文件的profile保持一致
      profile: dev
      # 遠程倉庫中的 分支 版本保持一致
      label: master
      # config server地址
      uri: http://localhost:10002/
      # 配置的用戶名密碼
      username: root
      password: root

在git倉庫里面我們新建了一個配置文件user-service-dev.yml,配置內(nèi)容如下:

dy:
  config:
    app-id: 110
    app-name: 'user-service-name'

使用的時候在config client端我們新建一個配置文件:

package com.dy.user.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * @Description:
 * @author: dy
 */
@Data
@Component
@Configuration
@ConfigurationProperties(prefix = "dy.config")
public class AppInfo {

    private String appId;

    private String appName;

}

測試類如下:

package com.dy.user.resource;

import com.dy.user.client.UserClient;
import com.dy.user.config.AppInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @author: dy
 */
@Slf4j
@RestController
@RequestMapping(UserClient.MAPPING)
public class UserResource implements UserClient {

    @Autowired
    private AppInfo appinfo;

    @Override
    public String getUserId() {
        return "======111111222>>"+appinfo.getAppId();
    }
}

運行結(jié)果如下:

配置中心運行結(jié)果.png

如此我們就大致實現(xiàn)了配置中心的基礎(chǔ)功能

config刷新功能

但是如果我們線上實時修改了git倉庫里面的配置文件內(nèi)容汰翠,那我們的客戶端怎么及時讀取git倉庫里面實時的文件內(nèi)容呢龄坪?

spring cloud config給我們提供了兩種方式:

  • config配置手動刷新

  • 自動刷新

config配置手動刷新
  • 引入依賴
<!-- 健康監(jiān)測依賴 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
  • 添加配置
management:
  #啟用監(jiān)控 默認(rèn)打開health和info,* 表示開放所有端口
  endpoints:
    web:
      exposure:
        include: *
  #打印健康信息詳情
  endpoint:
    health:
      show-details: always
  • 引用類里面添加注解@RefreshScope

    該注解處代理的bean會在調(diào)用/refresh接口時被清空

@Slf4j
@RestController
@RequestMapping(UserClient.MAPPING)
@RefreshScope
public class UserResource implements UserClient {

    @Autowired
    private AppInfo appinfo;

    @Override
    public String getUserId() {
        log.info("==============請求來了===============");
        return "======111111222>>"+appinfo.getAppId();
    }
}
  • 先修改git倉庫配置文件內(nèi)容

  • 再調(diào)用http://localhost:11001/actuator/refresh 進行手動刷新,注意是:post方式訪問

  • 請求接口我們可以發(fā)現(xiàn)內(nèi)容改變了

雖然手動刷新可以避免我們重啟服務(wù)复唤,但是每次去刷腳本跑接口刷新每個服務(wù)感覺十分不便健田,那有什么更好的方式呢?

config自動刷新

在微服務(wù)架構(gòu)體系中佛纫,我們可以接口消息總線bus實現(xiàn)配置的自動更新抄课,也就是Spring Cloud Config+Spring Cloud Bus

Spring Cloud Bus是基于MQ的唱星,支持Rabbitmq/Kafka,是Spring Cloud的消息總線方案跟磨。

具體實現(xiàn)流程如下:

配置中心自動刷新流程.png

具體實現(xiàn)如下(mq我們使用的是Rabbitmq):

config server端

  • 引入依賴
<!-- spring cloud bus消息總線依賴 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
  • 添加配置
server:
  port: 10002
spring:
  application:
    name: config-server
  # 這里我們用的rabbitmq
  rabbitmq:
    addresses: amqp://192.168.2.56:5672
    username: sxw_demo
    password: sxw_demo
  cloud:
    # cloud配置中心相關(guān)配置
    config:
      server:
        # git相關(guān)配置
        git:
          # git 地址
          uri: https://gitee.com/dai-yong-1/config-server-dy.git
          # git用戶名
          username: 15828101225
          # git密碼
          password: hd961740841
          # 配置文件所在文件夾
          search-paths: config-yml
          
#eureka 參數(shù)配置  
eureka:
  # 此實例注冊到eureka服務(wù)端的唯一的實例ID,其組成為${spring.application.name}:${spring.application.instance_id:${random.value}}
  instance:
    #  與此實例相關(guān)聯(lián)的主機名间聊,是其他實例可以用來進行請求的準(zhǔn)確名稱
    hostname: localhost
    # 獲取實例的ip地址
    prefer-ip-address: true
    #  eureka客戶需要多長時間發(fā)送心跳給eureka服務(wù)器,表明它仍然活著,默認(rèn)為30 秒
    lease-renewal-interval-in-seconds: 10
    #  Eureka服務(wù)器在接收到實例的最后一次發(fā)出的心跳后抵拘,需要等待多久才可以將此實例刪除哎榴,默認(rèn)為90秒
    lease-expiration-duration-in-seconds: 30
  client:
    # 注冊自己
    register-with-eureka: true
    # 不拉取服務(wù)
    fetch-registry: true
    # 配置服務(wù)地址
    service-url:
      # eureka 服務(wù)地址,如果是集群的話僵蛛;需要指定其它集群eureka地址  ,如果是多臺eureka server 地址以逗號隔開
      defaultZone: http://172.16.10.16:10001/eureka

management:
  #啟用監(jiān)控 默認(rèn)打開health和info,* 表示開放所有端口
  endpoints:
    web:
      exposure:
        include: "*"
  #打印健康信息詳情尚蝌,
  endpoint:
    health:
      show-details: always

config client端

  • 引入依賴
<!-- spring cloud bus消息總線依賴 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
  • 添加配置
server:
  port: 11001
spring:
  application:
    name: user-service
  rabbitmq:
    addresses: amqp://192.168.2.56:5672
    username: sxw_demo
    password: sxw_demo
  zipkin:
    # zipkin server的請求地址
    base-url: http://localhost:10005/
    sender:
      #web 客戶端將蹤跡日志數(shù)據(jù)通過網(wǎng)絡(luò)請求http的方式發(fā)送到服務(wù)端
      #rabbit 客戶端將蹤跡日志數(shù)據(jù)通過mp  rabbit的方式發(fā)送到服務(wù)端
      #kafka 客戶端將蹤跡日志數(shù)據(jù)通過mp  kafka的方式發(fā)送到服務(wù)端
      type: web
  sleuth:
    sampler:
      probability: 1 # 采樣率
  cloud:
    config:
      enabled: true
      # 與遠程倉庫中的配置文件的application保持一致
      name: user-service
      # 遠程倉庫中的配置文件的profile保持一致
      profile: dev
      # 遠程倉庫中的 分支 版本保持一致
      label: master
      # config server地址
      uri: http://localhost:10002/
      # 配置的用戶名密碼
      username: root
      password: root

運行項目操作步驟如下:

如果要實現(xiàn)修改完遠程git文件自動刷新配置的話,可以在git上面添加一個WebHooks, URL需要把本地的 http://localhost:10002/actuator/bus-refresh映射成公網(wǎng)IP.可以使用第三方內(nèi)網(wǎng)穿透實現(xiàn).具體如下:

配置中心wehooks配置.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市充尉,隨后出現(xiàn)的幾起案子飘言,更是在濱河造成了極大的恐慌,老刑警劉巖驼侠,帶你破解...
    沈念sama閱讀 216,651評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件姿鸿,死亡現(xiàn)場離奇詭異,居然都是意外死亡倒源,警方通過查閱死者的電腦和手機苛预,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來笋熬,“玉大人热某,你說我怎么就攤上這事「烀” “怎么了昔馋?”我有些...
    開封第一講書人閱讀 162,931評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長糖耸。 經(jīng)常有香客問我绒极,道長,這世上最難降的妖魔是什么蔬捷? 我笑而不...
    開封第一講書人閱讀 58,218評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮榔袋,結(jié)果婚禮上周拐,老公的妹妹穿的比我還像新娘。我一直安慰自己凰兑,他們只是感情好妥粟,可當(dāng)我...
    茶點故事閱讀 67,234評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著吏够,像睡著了一般勾给。 火紅的嫁衣襯著肌膚如雪滩报。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,198評論 1 299
  • 那天播急,我揣著相機與錄音脓钾,去河邊找鬼。 笑死桩警,一個胖子當(dāng)著我的面吹牛可训,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播捶枢,決...
    沈念sama閱讀 40,084評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼握截,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了烂叔?” 一聲冷哼從身側(cè)響起谨胞,我...
    開封第一講書人閱讀 38,926評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎蒜鸡,沒想到半個月后胯努,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,341評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡术瓮,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,563評論 2 333
  • 正文 我和宋清朗相戀三年康聂,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片胞四。...
    茶點故事閱讀 39,731評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡恬汁,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出辜伟,到底是詐尸還是另有隱情氓侧,我是刑警寧澤,帶...
    沈念sama閱讀 35,430評論 5 343
  • 正文 年R本政府宣布导狡,位于F島的核電站约巷,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏旱捧。R本人自食惡果不足惜独郎,卻給世界環(huán)境...
    茶點故事閱讀 41,036評論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望枚赡。 院中可真熱鬧氓癌,春花似錦、人聲如沸贫橙。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽卢肃。三九已至疲迂,卻和暖如春才顿,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背尤蒿。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評論 1 269
  • 我被黑心中介騙來泰國打工郑气, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人优质。 一個月前我還...
    沈念sama閱讀 47,743評論 2 368
  • 正文 我出身青樓竣贪,卻偏偏與公主長得像,于是被迫代替她去往敵國和親巩螃。 傳聞我的和親對象是個殘疾皇子演怎,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,629評論 2 354

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