【Spring Cloud】Spring Cloud Config 配置统一管理(一)
Spring Cloud Config 由服务端和客户端组成,借助git仓库存储配置信息,可以轻松的实现分布式项目配置文件统一管理
配置中心服务端 spring-cloud-config-server
依赖
<?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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-configer</artifactId>
<dependencies>
<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>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
配置
server:
port: 8888
spring:
application:
name: springcloud-configer
cloud:
config: # git管理配置
server:
git:
uri: https://github.com/yuyufeng1994/mydemo #配置git仓库位置
search-paths: framework/springcloud/springcloud-configer-conf/conf #具体目录
label: master
启动 @EnableConfigServer
package top.yuyufeng.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
/**
* @author yuyufeng
* @date 2018/12/25.
*/
@EnableConfigServer
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
启动调试
我在 framework/springcloud/springcloud-configer-conf/conf 目录下放了 配置文件 base-dev.yml
启动服务后,打开地址 http://127.0.0.1:8888/base/dev 可以看到加载的配置文件
配置规则详解
默认master分支
- / { 应用名 } / { 环境名 } [ / { 分支名 } ]
- / { 应用名 } - { 环境名 }.yml
- / { 应用名 } - { 环境名 }.properties
- / { 分支名 } / { 应用名 } - { 环境名 }.yml
- / { 分支名 } / { 应用名 } - { 环境名 }.properties
配置中心客户端
现在,我们来新建一个Spring Boot的项目使用配置中心的文件进行配置
依赖
<?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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-web</artifactId>
<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>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
配置文件
配置文件名称最好是 bootstrap.yml。 bootstrap.yml是项目最先加载的配置文件,里面需要有配置中心的地址和
配置存储的位置。
server:
port: 8080
spring:
application:
name: springcloud-web
cloud:
config: # git管理配置
uri: http://127.0.0.1:8888/
label: master
profile: dev
name: base,${spring.application.name} #表示使用的配置文件为 base.yml、springcloud-web.yml
测试Controller
写一个Controller,里面来读取配置文件的信息
package top.yuyufeng.demo.action;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author yuyufeng
* @date 2018/12/26.
*/
@RestController
public class IndexController {
@Value("${my-name}")
private String myName;
@RequestMapping(value = "/")
public String index() {
return "name:" + myName;
}
}
启动调试
package top.yuyufeng.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author yuyufeng
* @date 2018/12/26.
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
启动之后,可在日志中看到从远程获取配置信息。