Spring Cloud(Finchley.RCI) (九) Spring Cloud分佈式配置中心

在分佈式系統中, 由於服務數量巨多, 爲了方便服務配置文件統一管理, 實時更新, 所以需要分佈式配置中心組件。在Spring Cloud中, 有分佈式配置中心組件Spring Cloud Config, 它支持配置服務放在配置服務的內存中(即本地), 也支持放在遠程Git倉庫中。在Spring Cloud Config組件中, 分兩個角色, 一個是Config Server, 二是Config Client.

分佈式配置中心服務端

創建一個工程名爲hello-spring-cloud-config的項目, pom.xml配置文件如下:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.funtl</groupId>
        <artifactId>hello-spring-cloud-dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../hello-spring-cloud-dependencies/pom.xml</relativePath>
    </parent>

    <artifactId>hello-spring-cloud-config</artifactId>
    <packaging>jar</packaging>

    <name>hello-spring-cloud-config</name>
    <url>http://www.funtl.com</url>
    <inceptionYear>2018-Now</inceptionYear>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</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>

        <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>
                <configuration>
                    <mainClass>com.funtl.hello.spring.cloud.config.ConfigApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Application

通過@EnableConfigServer註解, 開啓配置服務功能

package com.funtl.hello.spring.cloud.config;

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 ConfigApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class, args);
    }
}

application.yml

增加Config相關配置, 並設置端口號爲: 8888

spring:
  application:
    name: hello-spring-cloud-config
  cloud:
    config:
      label: master
      server:
        git:
          uri: https://github.com/topsale/spring-cloud-config
          search-paths: respo
          username:
          password:
server:
  port: 8888
eureka:
  client:
    serviceUrl:
      default: http://localhost:8761/eureka

相關配置說明, 如下:

spring.cloud.config.label: 配置倉庫的分支

spring.cloud.config.server.git.uri: 配置Git倉庫的地址

spring.cloud.config.server.git.search-paths: 配置倉庫路徑

spring.cloud.config.server.git.username: 訪問Git倉庫的賬號

spring.cloud.config.server.git.password: 訪問Git倉庫的密碼

注意事項:

如果使用GitLab作爲倉庫的話, git.uri需要在結尾加上.git, GitHub則不用

測試

瀏覽器訪問: http://localhost:8888/config--client/dev/master

分佈式配置中心客戶端

創建一個工程名爲hello-spring-cloud-config-client的項目, pom.xml文件配置如下:

Application.yml

增加Config Client相關配置, 並設置端口號爲: 8889

cloud:
  config:
    uri: http://localhost:8888
    name: web-admin-feign
    label: master
    profile: dev

 

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