SpringCloud ------ config配置中心

分佈式配置中心

用來連接服務器,併爲客戶端提供配置信息

  1. 集中管理配置文件
  2. 不同環境不同配置,動態化的配置更新,分環境,比如 dev/test/prod/beta/release
  3. 運行期間動態調整配置,不再需要在每個服務部署的機器上編寫配置文件,服務會向配置中心同意拉取配置自己的信息
  4. 當配置發生改變時,服務不需要重啓即可感知到配置的變化並應用新的配置
  5. 將配置信息以REST接口的形式暴露(post / crul訪問刷新即可)

版本

SpringBoot版本

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

SpringCloud版本

<dependencyManagement>
	<dependencies>
		<!-- spring boot 2.2.5 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-dependencies</artifactId>
			<version>2.2.5.RELEASE</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
		<!-- spring cloud Hoxton.SR3 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>Hoxton.SR3</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

spring-cloud-config

spring-cloud-config-server

gitee私有倉庫中的文件

https://gitee.com/…/SpringCloudConfig/blob/master/dev/config-client.properties
master是分支
dev是文件夾
config-client.properties是配置文件名

foo=rererererebus-refresh dev version 3
test.maps.a=a
test.maps.b=b
test.maps.c=c

config-center
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>
        <artifactId>springcloud</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>config-center</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config</name>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <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-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

application.yml

server:
  port: 7511

spring:
  application:
    name: config-center
  cloud:
    config:
      server:
        git:
          # 倉庫地址
          uri: https://gitee.com/.../...
          # 搜索目錄
          search-paths: dev
          # 倉庫爲私有時需要配置用戶名密碼
          username: 用戶名
          password: 密碼
      # 讀取分支
      label: master

eureka:
  instance:
    hostname: localhost
    instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
    prefer-ip-address: true
    status-page-url: http://${spring.cloud.client.ip-address}:${server.port}
    lease-renewal-interval-in-seconds: 5
    lease-expiration-duration-in-seconds: 5
  client:
    healthcheck:
      enabled: true
    serviceUrl:
      defaultZone: http://localhost:7500/eureka/

ConfigCenterApplication

package org.example.springcloud.config;

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

@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigCenterApplication {

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

@EnableConfigServer: 開啓ConfigServer功能

請求http://localhost:7511/master/config-client.properties
可以獲取到配置的內容

foo: rererererebus-refresh dev version 3
test.maps.a: a
test.maps.b: b
test.maps.c: c

配置讀取規則

  1. /{application}-{profile}.yml
  2. /{application}/{profile}[/{label}]
  3. /label/{application}-{profile}.yml

根據日誌也可以在本地找到從git上down下來的配置文件
Adding property source: file:/C:/Users/lx/AppData/Local/Temp/config-repo-3070889713751237114/dev/config-client.properties

客戶端

config-client

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>
        <artifactId>springcloud</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>org.example.springcloud</groupId>
    <artifactId>config-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <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-actuator</artifactId>
        </dependency>
        <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-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>

bootstrap.yml

server:
  port: 7512

spring:
  application:
    name: config-client
  cloud:
    #Config客戶端配置
    config:
      # 分支名稱
      label: master
      # 配置文件名稱
      name: config-client
       #讀取後綴名稱 3個綜合:master分支上config-client.application的配置文件被讀取
      # profile: dev
      # 配置中心地址
      # uri: http://localhost:7511
      # 使用了Eureka 所以直接用 配置中心的服務名就能找到
      discovery:
        service-id: config-center

eureka:
  instance:
    hostname: localhost
    instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
    prefer-ip-address: true
    status-page-url: http://${spring.cloud.client.ip-address}:${server.port}
    lease-renewal-interval-in-seconds: 5
    lease-expiration-duration-in-seconds: 5
  client:
    healthcheck:
      enabled: true
    serviceUrl:
      defaultZone: http://localhost:7500/eureka/
package org.example.springcloud.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient
@SpringBootApplication
public class ConfigClientApplication {

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

}

測試

controller

package org.example.springcloud.config.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigController {

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

    @GetMapping("/get/config")
    public String getConfig() {
        return foo;
    }
}

請求 http://localhost:7512/get/config
獲取配置中foo的值

動態刷新

客戶端需要引入actuator

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

暴露監控端口
bootstrap.yml

management:
  endpoints:
    web:
      exposure:
        include: "*"

在controller類上添加
刷新功能的註解
因爲需要修改的值在controller裏面

@RefreshScope
@RestController
public class ConfigController {

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

    @GetMapping("/get/config")
    public String getConfig() {
        return foo;
    }
}
  1. 啓動client端
  2. 請求http://localhost:7512/get/config
  3. 修改gitee上配置文件foo的value爲rererererebus-refresh dev version 5(修改version 後面的值,每次 + 1)
  4. 請求配置中心http://localhost:7511/master/config-client.properties,發現獲取的配置文件的中的值已經更新了
  5. 請求http://localhost:7512/get/config,還是原來的值
  6. 需要POST調用接口刷新curl -X POST "http://localhost:7512/actuator/refresh"
  7. 再次請求http://localhost:7512/get/config,值已經刷新了

問題

服務Eureka註冊異常

啓動時會出現這些info日誌
exler
在Eureka的後臺會發現,服務 UNKNOWN
Exler
這是因爲Eureka的配置寫在bootstrap.yml配置文件裏面了

eureka.client.healthcheck.enabled=true應該只在中設置application.yml。將值設置爲bootstrap.yml會產生不良的副作用,例如在Eureka中註冊UNKNOWN狀態。

解決方法

  1. 註釋healthcheck.enabled
eureka:
  instance:
    hostname: localhost
    instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
    prefer-ip-address: true
    status-page-url: http://${spring.cloud.client.ip-address}:${server.port}
    lease-renewal-interval-in-seconds: 5
    lease-expiration-duration-in-seconds: 5
  client:
    # healthcheck:
      # enabled: true
    serviceUrl:
      defaultZone: http://localhost:7500/eureka/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章