Spring Cloud Config

  在分布式系统中,由于服务数量很多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,使用Spring Cloud Config来实现分布式配置中心,它支持配置服务放在配置服务的内存中(本地),也支持放在远程git仓库中,在spring cloud config组件中,分为两个角色,一个是config server,一个是config client。

准备配置仓库

  https://gitee.com/baidawei/SpringCloudConfig.git

构建配置中心(config server)

新建一个spring boot项目,名为com.david.configserver , 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>com.david</groupId>
<artifactId>configserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>configserver</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <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>Edgware.SR2</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> </project>

在启动类中加上@EnableConfigServer注解开启配置服务器的功能:

@EnableConfigServer
@SpringBootApplication
public class ConfigserverApplication { public static void main(String[] args) {
SpringApplication.run(ConfigserverApplication.class, args);
}
}

在application.yml中添加配置服务的基本信息以及git仓库的相关信息:

spring:
application:
name: david-config-server
cloud:
config:
server:
git:
uri: https://gitee.com/baidawei/SpringCloudConfig.git
      search-paths: repo
username: #如果需要权限username
password: #password
server:
port: 8888

如果项目公开可以不使用账号密码,searchPaths 是文件的路径.

假设我们读取配置中心的应用名为david-config-client,那么我们可以在git仓库中创建如下两个配置文件:

david-config-client.yml

info:
profile: default
form: git-repo

david-config-client-dev.yml dev环境

info:
profile: dev
form: git-repo

到现在,使用git管理的分布式配置中心就完成了,可以通过浏览器、postman等工具来访问配置的内容了。访问配置信息的url与配置文件的映射关系如下:

  /{application}/{profile}[/{label}]

  /{application}-{profile}.yml

  /{label}/{application}-{profile}.yml

  /{application}-{profile}.properties

  /{label}/{application}-{profile}.properties

其中,{label}对应git上不同的分支,默认为master。

访问david-config-client.yml 路径为:http://localhost:8888/david-config-client/master

{"name":"david-config-client","profiles":["master"],"label":null,"version":"b5fdf45844bf09c30e403e7b069f1c243fb1d4a7","state":null,"propertySources":[{"name":"https://gitee.com/baidawei/SpringCloudConfig.git/repo/david-config-client.yml","source":{"info.profile":"default","info.form":"git-repo"}}]}

dev: http://localhost:8888/david-config-client/dev

{"name":"david-config-client","profiles":["dev"],"label":null,"version":"b5fdf45844bf09c30e403e7b069f1c243fb1d4a7","state":null,"propertySources":[{"name":"https://gitee.com/baidawei/SpringCloudConfig.git/repo/david-config-client-dev.yml","source":{"info.profile":"dev","info.from":"git-repo"}},{"name":"https://gitee.com/baidawei/SpringCloudConfig.git/repo/david-config-client.yml","source":{"info.profile":"default","info.form":"git-repo"}}]}

构建客户端

  完成了分布式配置中心后,构建一个david-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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.david</groupId>
<artifactId>config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>config-client</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <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>Edgware.SR2</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> </project>

配置文件bootstrap.yml,来指定获取配置中心的配置文件

spring:
application:
name: david-config-client #对应{application}位置
cloud:
config:
uri: http://localhost:8888/ #配置中心网址
profile: default #对应{profile}位置 dev等
label: master #对应{label}位置 分支
server:
port: 8881

名字必须是bootstrap.yml或properties才能被正确加载,

新建TestController

@RestController
public class TestController {
@Value("${info.profile}")
String info;
@GetMapping("/test")
public String test(){
return info;
}
}

把server和client都启动起来

http://localhost:8881/test 输出default 切换配置文件中的profile为dev  输出为dev

这说明,config-client是从config-server中获取了info.profile属性,而config-server是从git仓库读取的

最新文章

  1. 一步一步学ROP之linux_x86篇
  2. ASP.NET实现微信功能(1)(创建菜单,验证,给菜单添加事件)
  3. 3、Python字符串和循环
  4. 一个简单的Linq to TreeNode
  5. 深入理解计算机系统(4.1)---X86的孪生兄弟,Y86指令体系结构
  6. HDOJ 3593 The most powerful force
  7. java poi ppt操作示例
  8. 2016.04.28,英语,《Vocabulary Builder》Unit 20
  9. E - Power Strings,求最小周期串
  10. 用状态矩阵解决有序操作的case爆炸问题(转载)
  11. 序列化form表单内容为json对象
  12. PI-利用SoapUI 测试web service的方法介绍
  13. 我们说的oc是动态运行时语言是什么意思?
  14. 在Hibernate中分别使用JDBC和JTA事务的方法
  15. 【动态规划】HDU 5791 Two
  16. 数据库的四种语言(DDL、DML、DCL、TCL)
  17. 区分window8中 ie10 window phone8
  18. Python--socketserve源码分析(二)
  19. css实现超出文本省略号的两个方法
  20. jquery中的 deferred之 then (二)

热门文章

  1. mysql根据用户的邀请码查询该用户所有的上级
  2. RequestMapping注解_修饰类
  3. HDU 4451 容斥原理
  4. 将Jquery EasyUI中DataGird的数据导入Excel中
  5. Linux下汇编语言学习笔记77 ---
  6. 洛谷—— P1419 寻找段落
  7. Quartz.Net 使用心得(二)
  8. 简单解决 WIN10更新后 远程桌面提示 CredSSP加密Oracle修正的问题
  9. ubuntu Change Language
  10. [Vue @Component] Place Content in Components with Vue Slots