Spring Cloud Config Server配置中心

背景

spring boot項目再也不讀取本身的配置文件,而是統一去配置中心讀取屬於本身的配置文件java

配置中心服務端

IDEA上面建立一個空的maven工程,命名爲configuration-servicegit

建立目錄

mkdir -p src/main/java/hello

配置服務器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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>io.spring</groupId>
    <artifactId>configuration-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>


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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

這個Maven配置主要是引入Spring Cloud的配置中心服務器的配置。github

建立服務器端主類ConfigServiceApplication.java

package hello;

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

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

這裏主要是啓用該服務爲配置中心服務。web

建立服務器端主配置application.properties

server.port=8888

spring.cloud.config.server.git.uri=${HOME}/Downloads/config

這裏配置配置中心服務端口爲8888,以及讀取的配置文件位置目錄配置。spring

配置中心客戶端

在IDEA上面建立Maven空工程,命名爲configuration-clientapache

建立目錄

mkdir -p src/main/java/hello

添加客戶端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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>io.spring</groupId>
    <artifactId>configuration-client</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

添加客戶端主類ConfigClientApplication

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

這裏是spring boot的正常配置。bootstrap

添加客戶端資源主類MessageRestController

package hello;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RefreshScope
@RestController
public class MessageRestController {
    @Value("${message:Hello default}")
    private String message;

    @RequestMapping("/message")
    String getMessage() {
        return this.message;
    }
}

這裏暴露一個請求,這個請求是從配置文件中讀取本身的配置參數messagebash

客戶端application.properties配置

server.port=8082
spring.application.name=a-bootiful-client
management.endpoints.web.exposure.include=*

這裏主要就是設置當前服務端口和服務名,已經開放相關運維接口,這些運維接口主要就是localhost:8082/actuator/refresh的post請求,用於刷新配置中心的配置參數的。服務器

客戶端bootstrap.properties針對配置中心的配置

spring.application.name=a-bootiful-client
# N.B. this is the default:
spring.cloud.config.uri=http://localhost:8888

這裏主要是配置當前服務名稱,以及配置中心的服務地址。注意這裏服務名,必須與配置中心讀取的文件名保持一致,即配置中心的配置文件名必須爲a-bootiful-client.properties纔可以讀取到文件。app

配置中心的配置文件管理

配置中心主要使用git來管理配置文件,也就是說每次對配置文件的修改,必須使用git來提交後,配置中心纔會承認。

建立配置文件

cd ~/Downloads/config
git init
vi a-bootiful-client.properties
git add a-bootiful-client.properties
git commit -m "建立a-bootiful-client.properties文件"

a-bootiful-client.properties內容以下:

message=zyl

客戶端測試是否生效

配置中心GET測試

修改配置文件

vi a-bootiful-client.properties
git commit -am "修改配置a-bootiful-client.properties文件"

a-bootiful-client.properties內容以下:

message=zyl2

客戶端測試是否生效

這個適合就須要先調用刷新接口,再進行測試。 客戶端post刷新接口

再調用測試接口:

配置中心GET2測試

總結

上面就是對Spring Cloud的配置中心這個部門的基本使用,這裏仍是涉及到配置文件更新後,使用git版本控制工具提交後,各個節點怎麼批量更新的問題。

源代碼在這裏:

spring-cloud-config-demo

參考

Centralized Configuration

相關文章
相關標籤/搜索