分布式文件配置中心
1、配置环境
- dev 开发
- pre 预生产
- prod 生产
- uat/test 测试
2、动态更新配置方式
- 方式1:缓存
存放在缓存中,数据库备份
后台进行可视化配置
读取时先从缓存读取、缓存没有,再从数据库读取
缓存与数据库不一致时清理缓存
- 方式2:git(sprngcloud采用方式)
将配置文件存放在版本控制
springcloud搭建配置中心原理
springcloud搭建配置实现
1、搭建注册中心
- 搭建maven环境
<groupId>com.link</groupId>
<artifactId>cloudconfig</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cloudconfig</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<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>${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>
- eurake依赖
<groupId>com.link</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-server</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.link</groupId>
<artifactId>cloudconfig</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--注册中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
- 配置
server:
port: 9999
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
- 启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication{}
2、搭建配置中心
- 项目依赖
<groupId>com.link</groupId>
<artifactId>config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config-server</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.link</groupId>
<artifactId>cloudconfig</artifactId>
<version>0.0.1-SNAPSHOT</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.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
- 启动类
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
@EnableDiscoveryClient
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
- 配置
# 配置中心:确定配置文件所在文件夹
spring.application.name=config-server
server.port=9991
# 配置git仓库
# 公开仓库,可以不填写用户名和密码
# 仓库地址: SpringcloudConfig/respo/config-client-dev.properties
# 访问地址: http://localhost:9999/config-client/dev
# 作用:根据客户端配置名称环境映射到该git仓库配置文件路径下文件
spring.cloud.config.server.git.uri=https://github.com/forezp/SpringcloudConfig
# 子路径
spring.cloud.config.server.git.searchPaths= respo
# master分支
spring.cloud.config.label=master
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
# 改造
eureka.client.serviceUrl.defaultZone=http://localhost:9999/eureka/
3、客户端调用配置
- 依赖
<groupId>com.link</groupId>
<artifactId>config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config-client</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.link</groupId>
<artifactId>cloudconfig</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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-config</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
- 配置文件
注意文件名: bootstrap.properties
server.port= 9992
spring.application.name=config-client
# label:远程仓库的分支
spring.cloud.config.label=master
# profile:dev\test\pro
# config-client-dev.properties
# 确定配置中心远程某个分支文件夹下的具体文件
spring.cloud.config.profile=dev
# 配置服务中心地址
spring.cloud.config.uri= http://localhost:9991/
# 从配置中心读取文件
spring.cloud.config.discovery.enabled=true
# 配置中心的servieId,即服务名
spring.cloud.config.discovery.serviceId=config-client
eureka.client.serviceUrl.defaultZone=http://localhost:9999/eureka/
- 启动类配置,, 验证属性读取
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@RefreshScope
public class ConfigClientApplication {}
@RestController
public class Controller {
/**
* 从配置中心读取时,配置文件命名必须改为bootstrap.properties
* 而不是application.properties
*/
@Value("${foo}")
String foo;
@RequestMapping(value = "/getConfig")
public String getConfigMsg(){
return foo;
}
}