分布式配置中心
用来连接服务器,并为客户端提供配置信息
- 集中管理配置文件
- 不同环境不同配置,动态化的配置更新,分环境,比如 dev/test/prod/beta/release
- 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心同意拉取配置自己的信息
- 当配置发生改变时,服务不需要重启即可感知到配置的变化并应用新的配置
- 将配置信息以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
配置读取规则
- /{application}-{profile}.yml
- /{application}/{profile}[/{label}]
- /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;
}
}
- 启动client端
- 请求http://localhost:7512/get/config
- 修改gitee上配置文件foo的value为
rererererebus-refresh dev version 5
(修改version 后面的值,每次 + 1) - 请求配置中心http://localhost:7511/master/config-client.properties,发现获取的配置文件的中的值已经更新了
- 请求http://localhost:7512/get/config,还是原来的值
- 需要POST调用接口刷新
curl -X POST "http://localhost:7512/actuator/refresh"
- 再次请求http://localhost:7512/get/config,值已经刷新了
问题
服务Eureka注册异常
启动时会出现这些info日志
在Eureka的后台会发现,服务 UNKNOWN
这是因为Eureka的配置写在bootstrap.yml
配置文件里面了
eureka.client.healthcheck.enabled=true
应该只在中设置application.yml
。将值设置为bootstrap.yml
会产生不良的副作用,例如在Eureka中注册UNKNOWN
状态。
解决方法
- 注释
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/