Spring Cloud
简介
Spring Cloud 包含了许多子项目,提供了一些工具来快速构建分布式系统中一些常用模式,分布式配置管理、服务注册和发现、断路器、智能路由、全局锁等;
主要项目
- Spring Cloud Config
- Spring Cloud Netflix
- Spring Cloud Bus
- Spring Cloud Zookeeper
…
Spring Cloud Config
简介
Spring Cloud Config在分布式中提供了客户端和服务端来支持应用外部化配置,在分布式环境中做配置管理。
特性
- 使用HTTP协议,配置基于资源,键值对或者YAML 文件配置
- 支持加密解密,支持对称或者非对称
- 很容易嵌入到SpringBoot应用中
- 从远程资源初始化Spring变量绑定到配置服务
快速开始
配置中心
Spring Cloud Config使用git来管理配置文件,请先建立git资源库,可以使用本地git资源库,这里使用github
github地址
https://github.com/pretent/cloud-config-server.git
并在cloud-config-server资源库中建立配置文件configClient-dev.properties
configClient-dev.properties
info.foo = bar
配置文件名称匹配以下顺序和规则
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
{application}为使用此配置中心的应用spring.application.name配置
{profile}为使用此配置中心的应用spring.profiles.active配置
{label}指定从git哪个分支拉取配置文件
建立SpringBoot应用程序
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>org.pretent.spring.cloud</groupId>
<artifactId>ConfigServer</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config</artifactId>
<version>1.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
</project>
Application.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
/**
* date: 17/1/2 23:43
* author: PRETENT
**/
@SpringBootApplication
@EnableConfigServer
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
application.properties
server.port = 8888
spring.cloud.config.server.git.uri = https://github.com/pretent/cloud-config-server.git
至此配置中心就搭建好了,只需要启动此SpringBoot应用即可。
客户端
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>org.pretent.spring.cloud</groupId>
<artifactId>configClient</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<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>
</dependencies>
</project>
Application.java
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* date: 17/1/3 20:51
* author: PRETENT
**/
@SpringBootApplication
@RestController
public class Application {
@Value("${info.foo}")
private String name;
@RequestMapping("/")
public String home() {
return "Hello World: " + this.name;
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
bootstrap.properties
spring.cloud.config.uri = http://127.0.0.1:8888
spring.application.name = configClient
spring.profiles.active = dev
spring.cloud.config.uri 为配置中心的地址
spring.application.name 配置文件名称${application}
spring.profiles.active 配置文件profile${profile}
至此客户端启动就可以从配置中心拉取配置了,将https://github.com/pretent/cloud-config-server.git
的配置文件configClient-dev.properties修改提交后再启动可以发现取到的值已经是修改后的值了,配置中心到此也搭建完成。