一、简介

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

在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

史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)的更多相关文章

  1. SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)(Finchley版本)

    在上一篇文章讲述zuul的时候,已经提到过,使用配置服务来保存各个服务的配置文件.它就是Spring Cloud Config. 一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管 ...

  2. SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)

    一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud config ...

  3. 【SpringCloud】第六篇: 分布式配置中心(Spring Cloud Config)

    前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...

  4. SpringCloud学习(六)分布式配置中心(Spring Cloud Config)(Finchley版本)

    在上一篇文章讲述zuul的时候,已经提到过,使用配置服务来保存各个服务的配置文件.它就是Spring Cloud Config. 简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理, ...

  5. springCloud学习-高可用的分布式配置中心(Spring Cloud Config)

    1.简介 高可用的分布式配置中心,即将配置中心做成一个微服务,将其集群化,从而达到高可用.config-server和config-client向eureka-server注册,且将config-se ...

  6. 史上最简单的SpringCloud教程 | 第三篇: 服务消费者(Feign)

    转载请标明出处: https://www.fangzhipeng.com/springcloud/2017/07/12/sc03-feign/ 本文出自方志朋的博客 最新Finchley版本请访问: ...

  7. 史上最简单的SpringCloud教程 | 第十篇: 高可用的服务注册中心(Finchley版本)

    转载请标明出处: 原文首发于 https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f10-eureka/ 本文出自方志朋的博客 文章 史上最简单 ...

  8. SpringCloud(6)分布式配置中心Spring Cloud Config

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

  9. 史上最简单的 SpringCloud 教程

    史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现(Eureka)史上最简单的SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)史上最简单的Spri ...

随机推荐

  1. BurpSuite使用笔记

    参考:Burp Suite 实战指南 Proxy模块 options--> add 设置监听地址,端口 binding 如果是作为本地浏览器代理默认(127.0.0.1:8080)就可以了. 如 ...

  2. 深入理解JVM - 1 - Java内存区域划分

    作者:梦工厂链接:https://www.jianshu.com/p/7ebbe102c1ae来源:简书简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处. Java与C++之间有一堵 ...

  3. mysql5.7 版本中 timestamp 不能为零日期 以及sql_mode合理设置

    ---恢复内容开始--- 摘要: mysql5.7版本相比较之前的版本有很多的特性的增加以及默认配置的改变,在使用中难免会遇到与之前的使用习惯或者项目需求不符的情况.就需要调整相应的变量的值,比如sq ...

  4. awk入门【转】

    awk其实不仅仅是工具软件,还是一种编程语言.不过,本文只介绍它的命令行用法,对于大多数场合,应该足够用了. 一.基本用法 awk的基本用法就是下面的形式. # 格式 $ awk 动作 文件名 # 示 ...

  5. python3+selenium框架设计03-封装日志类

    首先我们先来实现日志的功能,日志可以使用python3自带logging模块,不会的可以百度一下相关文章,也可以看我另外一篇文章Python3学习笔记24-logging模块 在封装日志类前,我们需要 ...

  6. HTML5在线预览PDF

    简介 PDF.js is a Portable Document Format (PDF) viewer that is built with HTML5. PDF.js is community-d ...

  7. oracle 多行变一行 wmsys.wm_concat

    背景        还是那个问题,部分程序员喜欢用sql解决问题.发现了这个函数,当初真是大喜过望,现在是哭笑不得.10g支持这个函数,11好像不支持了,而且只有oracle支持,其实自己写个通用方法 ...

  8. php OpenSSL 加解密

    2018-1-6 17:10:19 星期六 $data = '123456'; $openssl_method = 'AES-256-CBC'; $openssl_iv_length = openss ...

  9. mgo 的 session 与连接池

    简介 mgo是由Golang编写的开源mongodb驱动.由于mongodb官方并没有开发Golang驱动,因此这款驱动被广泛使用.mongodb官网也推荐了这款开源驱动,并且作者在github也表示 ...

  10. .net常用的代码生成工具

    之前很多从事C#开发的用过动软代码生成器,然后随着IT技术的快速发展,涌现出很多优秀的工具关于.Net的,首推微软的Entity Framework,其次是NHibernate.Entity Fram ...