一、简介

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。

在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。

二、构建Config Server

创建一个spring-boot项目,取名为config-server,其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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>configserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config-server</name>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR2</spring-cloud.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>${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>
</project>

在程序的入口Application类加上 @EnableConfigServer注解 开启配置服务器的功能,代码如下:

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

application.properties配置以下:

spring.application.name=config-server
server.port=8888 #服务注册中心
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
#服务的git仓库地址,末尾加或不加".git"都可
spring.cloud.config.server.git.uri=https://gitee.com/xiaoliu66007/springcloud /*替换成自己的git地址*/
#配置文件所在的目录
spring.cloud.config.server.git.searchPaths=/**/config
/*替换成自己的git地址下的文件夹*/
#配置文件所在的分支 
spring.cloud.config.label=master
spring.cloud.config.server.git.username=******* /*替换成git账号*/
spring.cloud.config.server.git.password=******* /*替换成git密码*/

注意事项:

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

为了方便测试,我是在码云上自己上传了 config-server-dev.properties文件到config文件夹下,如图:

 

----------------------------------------------------------------------------注意事项完毕------------------------------------------------------------------------------------------------

config-server-dev.properties内容如下
foo = foo version 3

启动程序:访问http://localhost:8888/config-server/dev,其中的config-server和dev 参照下面的“http请求地址和资源文件映射:”

效果如下:

证明配置服务中心可以从远程程序获取配置信息。

接上面的注意事项,http请求地址和资源文件映射如下:

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

三、构建一个config client

重新创建一个springboot项目,取名为config-client,其pom文件:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config-client</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR2</spring-cloud.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>${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>
</project>

其配置文件bootstrap.properties:

server.port=8881
spring.application.name=config-server
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri= http://localhost:8888/
  • spring.cloud.config.label 指明远程仓库的分支
  • spring.cloud.config.profile

    • dev开发环境配置文件
    • test测试环境
    • pro正式环境
  • spring.cloud.config.uri= http://localhost:8888/ 指明配置服务中心的网址。

程序的入口类,如下:
/**
http://localhost:8881/hi可以访问到配置中心config-server,通过配置中心访问到远程git地址下的"config-server-dev.properties"(详见bootstrap.properties配置)
bootstrap.properties文件指定读取的文件为:
“【spring.application.name】- 【spring.cloud.config.profile】.properties”
即config-server-dev.properties
*/
package com.example.demo;
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; @SpringBootApplication
@RestController
public class ConfigClientApplication { public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
@Value("${foo}")
String foo;
@RequestMapping(value = "/hi")
public String hi(){
return foo;
}
}
访问 http://localhost:8881/hi结果如下
foo version 3

这就说明,config-client从config-server获取了name和age的属性,而config-server是从git仓库读取的,如图

本文源码下载: 
https://github.com/forezp/SpringCloudLearning/tree/master/chapter6

最新文章

  1. BFC深入理解
  2. OC load与initialize
  3. node 常用命令
  4. 集群间Session共享问题解决方案
  5. Kindeditor 代码审计
  6. linux下的crontab服务
  7. 配置NTP服务ntpd/ntp.conf(搭建Hadoop集群可参考)
  8. (转)使用 /proc 文件系统来访问 Linux 内核的内容
  9. 转:我们是否应该把后端构建为API
  10. Linux+Nginx+Asp.net Core部署
  11. python统计词频
  12. 安装percona-toolkit工具时遇到的问题
  13. 深入浅出Automation Anywhere
  14. CSS变形transform(2d)
  15. jenkins部署war包到远程服务器的tomcat
  16. Springmvc Exception
  17. Python学习---ModelForm拾遗180325
  18. java面试第十五天
  19. 利用ItextSharp 生成PDF文档改进版
  20. timestamp类型在jsp页面输出格式化方法

热门文章

  1. Python 面向对象【2】
  2. 绕过PALOALTO TRAPS EDR解决方案
  3. SQL逻辑查询语句执行顺序
  4. linux 用户空间获得纳秒级时间ns【转】
  5. struts2框架学习之第一天
  6. 029_shell编写工作常用工具类总结
  7. PHP一维数组转二维数组正则表达式
  8. UVA 1395 MST
  9. kerberos简单介绍
  10. Js -----后台json数据,前端生成下载text文件