Spring Cloud Config是Spring Cloud团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端与客户端两个部分,Spring Cloud Config是一个独立的server,工程过程中不需要eureka注册。
•其中服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置仓库并为客户端提供获取配置信息、加密/解密信息等访问接口;
•而客户端则是微服务架构中的各个微服务应用或基础设施,它们通过指定的配置中心来管理应用资源与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。
Spring Cloud Config实现了对服务端和客户端中环境变量和属性配置的抽象映射,所以它除了适用于Spring构建的应用程序之外,也可以在任何其他语言运行的应用程序中使用。
•由于Spring Cloud Config实现的配置中心默认采用Git来存储配置信息,所以使用Spring Cloud Config构建的配置服务器,天然就支持对微服务应用配置信息的版本管理,并且可以通过Git客户端工具来方便的管理和访问配置内容。
•当然它也提供了对其他存储方式的支持,比如:SVN仓库、本地化文件系统
Spring Cloud Config 使用:
•通过Spring Cloud构建一个Config Server,非常简单,只需要三步:
•1 pom.xml中引入spring-cloud-config-server依赖。
•2 在程序主类添加@EnableConfigServer注解,开启Config Server。
•3 最后对yml文件进行GIT配置即可。
•配置中心验证:
•首先编写4个配置文件~,然后里面比如有个from属性,进行设置不同的值。最后通过gitbash上传到GitHub上,根据验证规则,如下所示:http://localhost:4000/{application}/{profile}/{label}
•Yml GIT 管理配置访问规则,如下图所示
•首先我们引入:spring-cloud-starter-config依赖,再添加自动刷新模块依赖spring-boot-starter-actuator。然后编写配置文件bootstrap.yml
•最后可以很轻松的使用@Value属性注入到spring bean在任何位置,非常的方便!
动态刷新配置功能。很多时候我们去修改git上的properties配置文件,希望我们的我服务在不停机更新配置的情况下动态进行更新配置,这样就需要使用到我们的config动态刷新模块了,必须要记得引入依赖:spring-boot-starter-actuator
•然后我们可以在任何位置。比如config-client的controller设置@RefreshScope注解标识动态刷新。
•最后spring-boot-starter-actuator 从1.5开始很多端点都受权限保护了,也就是增加了安全/权限,要么我们可以使用security进行配置用户/密码,或者我们可以在yml主配置文件里禁用掉安全配置即可!
•最后我们通过访问://http://localhost:7001/refresh 就可以实现动态刷新配置中心数据啦。也就是我们可以理解为更为高大上的zookeeper配置中心产生了。
server/pom:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>spring-cloud-06-config-server</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.cc.springcloud.Application</mainClass>
</configuration>
</plugin>
</plugins>
</build>
--------------------------------------------------------------------------------------------------------------
server/application:
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: 输入具体git地址
server:
context-path: /
port: 4000
---------------------------------------------------------------------------------------------------------
server/Application:
package com.cc.springcloud;
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 //开启配置中心服务
@SpringBootApplication //springboot 核心配置
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
---------------------------------------------------------------------------------------------------------
client/pom:
<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>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>spring-cloud-06-config-client</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.cc.springcloud.Application</mainClass>
</configuration>
</plugin>
</plugins>
</build>
---------------------------------------------------------------------------------------------------------
client/bootstrap:【客户端的配置文件命名为bootstrap.yml,约定名称】
spring:
application:
name: evn ##此处名字与配置文件头相同
cloud:
config:
uri: http://localhost:4000 ##指定要从那个服务上读取配置,此处从config-server
profile: dev ##确定切换什么环境数据
label: master ##git分支,如不是master要指定具体分支
server:
context-path: /
port: 7001
##应用配置中心需要禁用安全检查
management:
security:
enabled: false
---------------------------------------------------------------------------------------------------------
client/ConfigController:
package com.cc.springcloud.api;
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;
/**
* 此处简化架构,未创建eureka服务器,
* 正式生产中config-client也是微服务组件,需要注册到eureka;
*/
/**@RefreshScope
* 不停机,动态热更新配置
* 强制刷新:使用post方法 http://localhost:7001/refresh
* 真实环境可以通过脚本监听,一旦配置文件发生更改,自动发送一个post请求来刷新
*
*/
@RefreshScope
@RestController
public class ConfigController {
@Value("${from}")
private String from;
@RequestMapping(value="/from")
public String from() {
System.out.println("-----from-----"+from);
return this.from;
}
}