Spring Cloud(七)高可用的分布式配置中心(Spring Cloud Config)

上篇文章講述了一個服務(wù)如何從配置中心讀取文件,配置中心如何從遠(yuǎn)程git讀取配置文件,當(dāng)服務(wù)實例很多時粮揉,都從配置中心讀取文件巡李,這時可以考慮將配置中心做成一個微服務(wù),將其集群化扶认,從而達(dá)到高可用侨拦,架構(gòu)圖如下:


一,準(zhǔn)備工作

繼續(xù)使用上一篇文章的工程辐宾,創(chuàng)建一個eureka-server工程狱从,用作服務(wù)注冊中心。

在其pom.xml文件引入Eureka的起步依賴spring-cloud-starter-netflix- eureka-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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>demo2-springcloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>eureka-server</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

application.yml代碼

server:
  port: 8889

eureka:
  instance:
    hostname: localhost
  client:
    fetch-registry: false
    register-with-eureka: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

在啟動類添加@EnableEurekaServer注解

package com.example.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

二季研,改造config-server

將其注冊微到服務(wù)注冊中心,作為Eureka客戶端,pom添加eureka依賴

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

修改application.yml配置文件

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/forezp/SpringcloudConfig/
          search-paths: respo
          password:
          username:
      label: master
server:
  port: 8888
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8889/eureka/

在啟動類添加注解

package com.example.configserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

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

三誉察,改造config-client

將其注冊微到服務(wù)注冊中心与涡,作為Eureka客戶端,pom添加依賴

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

在啟動類添加@EnableEurekaClient注解

package com.example.configclient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@EnableEurekaClient
@RestController
@SpringBootApplication
public class ConfigClientApplication {

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

    @Value("${democonfigclient.message}")
    String message;
    @RequestMapping("/hi")
    public String sayHi(){
        return message;
    }
}

配置文件bootstrap.properties,注意是bootstrap!修改bootstrap.yml

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      profile: dev
      uri: http://localhost:8888/eureka/
      name: config-client
      discovery:
        enabled: true
        service-id: config-server
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8889/eureka/
server:
  port: 8881
  • spring.cloud.config.discovery.enabled 是從配置中心讀取文件持偏。
  • spring.cloud.config.discovery.serviceId 配置中心的servieId驼卖,即服務(wù)名。

這時發(fā)現(xiàn)鸿秆,在讀取配置文件不再寫ip地址酌畜,而是服務(wù)名,這時如果配置服務(wù)部署多份卿叽,通過負(fù)載均衡桥胞,從而高可用。
依次啟動eureka-servr,config-server,config-client
訪問網(wǎng)址:http://localhost:8889/


訪問http://localhost:8881/hi考婴,瀏覽器顯示:

四贩虾,易錯總結(jié)

1.config-client的配置文件修改是在bootstrap.yml上
2.注冊服務(wù)中心的路徑不能不一樣,不小心打錯了蕉扮,排查了幾個小時整胃。。喳钟。屁使。
3.啟動類添加的是@EnableEurekaClient不是@EnableEurekaServer

五,參考資料

大佬的文章https://blog.csdn.net/forezp/article/details/70037513

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末奔则,一起剝皮案震驚了整個濱河市蛮寂,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌易茬,老刑警劉巖酬蹋,帶你破解...
    沈念sama閱讀 219,110評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件及老,死亡現(xiàn)場離奇詭異,居然都是意外死亡范抓,警方通過查閱死者的電腦和手機骄恶,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來匕垫,“玉大人僧鲁,你說我怎么就攤上這事∠蟊茫” “怎么了寞秃?”我有些...
    開封第一講書人閱讀 165,474評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長偶惠。 經(jīng)常有香客問我春寿,道長,這世上最難降的妖魔是什么忽孽? 我笑而不...
    開封第一講書人閱讀 58,881評論 1 295
  • 正文 為了忘掉前任绑改,我火速辦了婚禮,結(jié)果婚禮上扒腕,老公的妹妹穿的比我還像新娘绢淀。我一直安慰自己,他們只是感情好瘾腰,可當(dāng)我...
    茶點故事閱讀 67,902評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著覆履,像睡著了一般蹋盆。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上硝全,一...
    開封第一講書人閱讀 51,698評論 1 305
  • 那天栖雾,我揣著相機與錄音,去河邊找鬼伟众。 笑死析藕,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的凳厢。 我是一名探鬼主播账胧,決...
    沈念sama閱讀 40,418評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼先紫!你這毒婦竟也來了治泥?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,332評論 0 276
  • 序言:老撾萬榮一對情侶失蹤遮精,失蹤者是張志新(化名)和其女友劉穎居夹,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,796評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡准脂,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,968評論 3 337
  • 正文 我和宋清朗相戀三年劫扒,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片狸膏。...
    茶點故事閱讀 40,110評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡粟关,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出环戈,到底是詐尸還是另有隱情闷板,我是刑警寧澤,帶...
    沈念sama閱讀 35,792評論 5 346
  • 正文 年R本政府宣布院塞,位于F島的核電站遮晚,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏拦止。R本人自食惡果不足惜县遣,卻給世界環(huán)境...
    茶點故事閱讀 41,455評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望汹族。 院中可真熱鬧萧求,春花似錦、人聲如沸顶瞒。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽榴徐。三九已至守问,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間坑资,已是汗流浹背耗帕。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留袱贮,地道東北人仿便。 一個月前我還...
    沈念sama閱讀 48,348評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像攒巍,于是被迫代替她去往敵國和親嗽仪。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,047評論 2 355

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