第七篇 : 高可用的分佈式配置中心(Spring Cloud Config)

第七篇 : 高可用的分佈式配置中心(Spring Cloud Config)

上一篇文章講述了一個服務如何從配置中心讀取文件,配置中心如何從遠程git讀取配置文件,當服務實例很多時,都從配置中心讀取文件,這時可以考慮將配置中心做成一個微服務,將其集羣化,從而達到高可用,架構圖如下:

一、創建項目

繼續使用上一篇文章的工程,備創代碼,複製成新的工程使用

1. 創建 eureka-server

創建eureka-server工程,用作服務註冊中心 

1. pom.xml

在其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>

    <groupId>com.gf</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>

    <parent>
        <groupId>com.gf</groupId>
        <artifactId>chapter07</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

    </dependencies>

</project>

2. applicaton.yml

在配置文件application.yml上,指定服務端口爲8889,加上作爲服務註冊中心的基本配置,代碼如下:

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

3. 主啓動類 EurekaServerApplication

package com.gf;

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

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

}

2. 改造 config-server

1. pom.xml

在其pom.xml文件加上EurekaClient的起步依賴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>

    <groupId>com.gf</groupId>
    <artifactId>config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>config-server</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.gf</groupId>
        <artifactId>chapter07</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <dependencies>
        <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>

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

</project>

2. application.yml

配置文件application.yml,指定服務註冊地址爲http://localhost:8889/eureka/,其他配置同上一篇文章,完整的配置如下: 

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

3. 主啓動類 ConfigServerApplication

最後需要在程序的啓動類Application加上@EnableEureka的註解。 

package com.gf;

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;

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

}

3. 改造 config-client

1. pom.xml

將其註冊微到服務註冊中心,作爲Eureka客戶端,需要pom文件加上起步依賴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>

    <groupId>com.gf</groupId>
    <artifactId>config-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>config-client</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.gf</groupId>
        <artifactId>chapter07</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

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

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

</project>

2. bootstrap.yml

配置文件bootstrap.yml,注意是bootstrap。加上服務註冊地址爲http://localhost:8889/eureka/

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      profile: dev
      #uri: http://localhost:8888/
      discovery:
        enabled: true
        service-id: config-server
server:
  port: 8881
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8889/eureka/
  • spring.cloud.config.discovery.enabled 是從配置中心讀取文件。

  • spring.cloud.config.discovery.serviceId 配置中心的servieId,即服務名。

這時發現,在讀取配置文件不再寫ip地址,而是服務名,這時如果配置服務部署多份,通過負載均衡,從而高可用。

依次啓動eureka-servr,config-server,config-client
訪問網址:http://localhost:8889/

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

dev

源碼下載:https://github.com/gf-huanchupk/SpringCloudLearning


本文分享自微信公衆號 - 程序員果果(huanchuguofupk_gz)。
如有侵權,請聯繫 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章