1. 程式人生 > >基於spring-cloud-config的分散式git配置更新

基於spring-cloud-config的分散式git配置更新

首先我們新建一個模組server-config

需要安裝rabbitmq,用於推送配置
https://www.cnblogs.com/ericli-ericli/p/5902270.html

引入依賴:

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>spring-cloud-config</groupId> <artifactId>spring-cloud-config</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding
>
UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <file.encoding>UTF8</file.encoding> <spring-cloud.version>Finchley.SR2</spring-cloud.version> </properties> <parent> <groupId>org.springframework.boot</
groupId
>
<artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-monitor</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-actuator</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

配置檔案設定:
需要注意git如果設定為私有倉庫,最好用ssh+key,RSA金鑰的生成方法就不在這裡贅述了,如果為公有的話,哪種方法都可以,這裡給出私有倉庫的方法

application.properties

#服務埠
server.port=8887
#服務名稱
spring.application.name=spring-cloud-config-server
#服務註冊中心
eureka.client.service-url.defaultZone=http://localhost:8881/eureka/
#服務的git倉庫地址
#支援兩種方法:一、url+賬號密碼 二、ssh+key
#此處使用ssh+key
#spring.cloud.config.server.git.uri=https://gitee.com/leftbehindmatches/spring-cloud-examples.git

#git的ssh地址
spring.cloud.config.server.git.uri=[email protected].com:leftbehindmatches/spring-cloud-examples.git
#可以使用本地的ssh配置,但這裡忽略本地的ssh設定,這裡使用遠端設定
spring.cloud.config.server.git.ignoreLocalSshSettings= true
#RSA私鑰 這裡需要拼接起來,全顯示綠色才正確, 首行需新增\n\ ,其他行新增\, 倒數第二行新增\n\,最後一行不用加
spring.cloud.config.server.git.privateKey=-----BEGIN RSA PRIVATE KEY-----\n\
MIIEpAIBAAKCAQEA1RtREwf+Gb8EM1brcax9BVMaSwzeUxZQ8ico2AOCK/mbKY4B\
n+tUqJHVUWtTqGx0DbEOjGbqPkwAfel7IZbA8GBn3D0ybDjHSB7dq445p6F7WTUA\
GzL8//loKlLqnS+p5dkB9M36T7pQIO4ic9EnqCtSeD67DYbnYhdG7WGzuvByA5YV\
Sky3CMJF1P0UtX1z7AKRt/MXsM7EaaCrtJOBplR8qOA4E53jU4e1ExZjMKvS8/om\
og7fby4udAKHJaIUsUwyIxlYpDpsL0rF8X2mKwRrlABh3dq4WD1jbu0IAzvk2xZs\
PkI30jS3rIE7TeFTXzopK4HU6Xn/sdrXyOOVeQIDAQABAoIBAQCKKi3EQvhRxPB6\
QK5hgs2DuD9hzTQPdnaAqTbn5k5tKGWa6YDQKAk5SWOeaBFTLw2HGvYtvUO9pBTl\
jYzRWCY7MqXjuqGIXL+HWMurQIjMz/ti+OPavpIde5w3ZI7L3m0uoiWMjEbvjgIJ\
****************************************************************
1po5gu5v/33dw97Fukm9cw+qs2RyX473FHXA4roRAoGAFcdyd15gxL7YqZHmqG1S\
fWn46fFl/QCjPLWqEsFAbK3OoNlId2o+BujiYOgZJX9RN0jvGfOvFoSjdUKfMrRI\
A1MCZidZnkCHPGeN1ELR9BLJriJYbWTYA4+dXfLS7KKqQJPS32vVvT1BYORYHO/E\
P8tybvfNseCTkGLvG6GsybkCgYEAiWuzHlzNL/TCQojOcK+H4a+JTxIc4gfPqDxl\
PS53gdnQKTea7aZQnE7ck+oT4DxDP0tdDKhRpBeycOktKXc7zjBsbKMVpeV/Pe58\
9bKFo2OLa06aMKzMO4gQIpacHq7fWS4MCW76K5LGubfCZoMu5P6ihN0evEgANbcq\
89zQIGECgYAM8ky1izFo6pz2tD6f9Uh+FkBi5ybS9EQPmXlLT01zTaMTNESWj6Vl\
RiJgYFSD79EFAvCn8hXMhHgd/wM1HW8X7wg8XnjhlaSzwfrzOtjt//G0t0MyDFqB\
hZ0WDHwwLhfWLBEaYR457m3v9xRr8uZvbso5JfBL4UWEvNxW41ubaw==\n\
-----END RSA PRIVATE KEY-----


#配置檔案所在的目錄  這裡需要注意的是根路徑spring-cloud-examples, 但是路徑開頭不需要橫杆\
spring.cloud.config.server.git.search-paths=spring-cloud-config/config-repo
#配置檔案所在的分支
spring.cloud.config.label=master

#這裡需要使用rabbitmq來進行重新整理
spring.rabbitmq.host=127.0.0.1
#rabbitmq的埠為5672,並不是瀏覽器檢視埠
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

#匯流排重新整理
management.endpoints.web.exposure.include=bus-refresh

在github或者gitee上建立配置檔案存放地址:
這裡是使用gitee,存放在config-repo:

https://gitee.com/leftbehindmatches/spring-cloud-examples/tree/master/spring-cloud-config/config-repo

新建三個檔案,檔名為:a-config-dev.properties

name=match-1
version=dev

a-config-prod.properties

name=match-2
version=prod

a-config-test.properties

name=match-3
version=test

倉庫中的配置檔案會被轉換成web介面,訪問可以參照以下的規則:

/{application}/{profile}[/{label}]

/{application}-{profile}.yml

/{label}/{application}-{profile}.yml

/{application}-{profile}.properties

/{label}/{application}-{profile}.properties

以a-config-dev.properties為例子,它的application是a-config,profile是dev。client會根據填寫的引數來選擇讀取對應的配置。

依次啟動eureka-server,spring-cloud-config-server再呼叫

由於已經設定了配置檔案的根路徑了,這裡直接訪問即可

http://localhost:8887/a-config/test

可以看到瀏覽器返回

{"name":"a-config","profiles":["test"],"label":null,"version":"6d70c58fc75ac8b3f7a2218347c8be67352b710c","state":null,"propertySources":[{"name":"[email protected]:leftbehindmatches/spring-cloud-examples.git/spring-cloud-config/config-repo/a-config-test.properties","source":{"name":"match","age":"18","version":"test"}}]}

再次呼叫 http://localhost:8887/a-config/dev

可以看到讀取到dev的資訊

{"name":"a-config","profiles":["dev"],"label":null,"version":"6d70c58fc75ac8b3f7a2218347c8be67352b710c","state":null,"propertySources":[{"name":"[email protected]:leftbehindmatches/spring-cloud-examples.git/spring-cloud-config/config-repo/a-config-dev.properties","source":{"name":"match","age":"18","version":"dev"}}]}

改造eureka-client 為eureka-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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <artifactId>eureka-config-client</artifactId>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <file.encoding>UTF8</file.encoding>
        <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</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>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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


</project>

這裡需要

application.properties與bootstrap.properties
不然會報錯,原因暫時沒找到

application.properties為空
東西都寫在bootstrap.properties

server.port=8888
spring.application.name=eureka-config

eureka.client.service-url.defaultZone=http://localhost:8881/eureka/

#這裡為配置檔案的名稱
spring.cloud.config.name=a-config
#這裡為配置檔案的字尾
spring.cloud.config.profile=dev
#spring.cloud.config.uri=http://localhost:8887/
spring.cloud.config.label=master
#開啟Config服務發現支援
spring.cloud.config.discovery.enabled=true
#指定server端的name,也就是server端spring.application.name的值
spring.cloud.config.discovery.serviceId=spring-cloud-config-server

## 重新整理時,關閉安全驗證
#Spring boot 2.0的改動較大,/bus/refresh全部整合到actuator裡面了,變成了/actuator/bus-refresh,所以之前1.x的management.security.enabled全部失效,不適用於2.0
#2.0的properties配置是這樣的:
management.endpoints.web.exposure.include=bus-refresh
## 開啟訊息跟蹤
spring.cloud.bus.trace.enabled=true

spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

application.java:

package top.littlematch.eureka.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@RefreshScope
@RestController
public class EurekaConfigApplication {
    public static void main(String[] args){
        SpringApplication.run(EurekaConfigApplication.class, args);
    }

    
    @Value("${version}")
    private String version;

    @RequestMapping("/hello")
    public String hello(@RequestParam(name = "name") String name) {
        return "你好," + name + "。版本號:" + version;
    }
}

啟動eureka-config, 瀏覽器訪問:

http://localhost:8888/hello?name=match

你好,match。版本號:dev

修改a-config-dev.properties檔案

name=match_1
version=test

修改版本號後, push到git

使用連線重新整理配置
(2.0的重新整理連線變更為如下)
http://localhost:8888/actuator/bus-refresh

再次訪問, 已實現重新整理:

http://localhost:8888/hello?name=match

你好,match。版本號:test

gitee程式碼地址: https://gitee.com/leftbehindmatches/spring-cloud-examples/tree/master/spring-cloud-config