一、概述

什么是配置中心呢,在基于微服务的分布式系统中,每个业务模块都可以拆分成独立自主的服务,由多个请求来协助完成某个需求,那么在某一具体的业务场景中,某一个请求需要调用多个服务来完成,那么就存在一个问题,多个服务所对应的配置项也是非常多的,每个服务都有自己的配置文件,如果某一个微服务配置进行了修改,那么其他的服务消费者也需要做出对应的调整,如果在每个服务消费者对应的配置文件中去修改的话是非常麻烦的,并且改完后每个服务还需要重新部署。这时候就需要把各个服务的配置进行统一管理,便于部署与维护,所以Spring Cloud提出了解决方法,就是Spring Cloud Config.

Spring Cloud Config可以通过服务端为多个客户端提供配置服务。Spring Cloud Config可以将配置文件存储在本地,也可以将配置文件存储在远程仓库中。可以在不重启微服务的前提下来修改它的配置。具体操作就是创建Config Server,通过它管理所有的配置文件。

二、实战!本地配置中心

1.创建Maven工程,pom.xml配置如下:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>

2.创建application.yml,配置详情如下:

server:
port: 8762
spring:
application:
name: nativeconfigserver
profiles:
active: native
cloud:
config:
server:
native:
search-locations: classpath:/shared

注解说明

​ * profiles.active:配置文件获取方式,native表示从本地获取

​ * cloud.config.server.native.search-locations:本地配置文件存放的路径,classpath表示当前文件所在目录

3.resources路径下创建shared文件夹,并且在此目录下创建configclient-dev.yml,配置文件内容如下:

server:
port: 8070
foo: foo version 1

4.创建启动类,代码如下:

package com.zing;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication
@EnableConfigServer
public class NativeConfigServerApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(NativeConfigServerApplication.class, args);
} }

注解说明

​ * @EnableConfigServer:声明一个配置中心

5.创建一个客户端来读取本地配置中心的配置文件,先创建Maven项目,pom.xml配置如下:

    <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>

6.创建一个bootstrap.yml,文件名必须为这个,不能是application.yml这个默认配置文件的名称,因为我们的配置要从配置中心拿,我们只需要写一些其他的配置项信息,如下:

spring:
application:
name: configclient
profiles:
active: dev
cloud:
config:
uri: http://localhost:8762
fail-fast: true

注解说明

​ * cloud.config.uri:本地Config Server 的访问路径

​ * cloud.config.fail-fase:设置客户端优先判断Config Server获取是否正常

​ * 通过spring.application.name结合spring.profiles.active拼接获得目标配置文件名称configclient-dev.yml,去Config Server中查找该文件。

7.创建客户端启动类,代码如下:

package com.zing;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class NativeConfigClientApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(NativeConfigClientApplication.class, args);
} }

8.创建controller代码如下:

package com.zing.handler;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("/native")
public class NativeConfigHandler { @Value("${server.port}")
private String port; @Value("${foo}")
private String foo; @GetMapping("/index")
public String index() {
return this.port+"-"+this.foo;
} }

9.依次启动配置中心服务nativeconfigserver和配置中心客户端nativeconfigclient,不同启动注册中心,因为也没有配置注册中心地址。两个服务都启动好之后,我们访问 http://localhost:8070/native/index 可以看到如下页面:

结果和我们预想的一样,客户端nativeconfigclient读取到了配置中心nativeconfigserver的配置文件信息,成功验证了Spring Cloud Config的本地配置中心功能。

三、总结

Spring Cloud Config可以在不重启服务端的前提下通过修改配置中心的配置文件来修改它的配置,便于我们集中化管理集群配置 。 目前除了支持本地存储以外,还可以支持Git以及Subversion。

Spring Cloud09: Config 配置中心的更多相关文章

  1. 跟我学SpringCloud | 第七篇:Spring Cloud Config 配置中心高可用和refresh

    SpringCloud系列教程 | 第七篇:Spring Cloud Config 配置中心高可用和refresh Springboot: 2.1.6.RELEASE SpringCloud: Gre ...

  2. 微服务SpringCloud之Spring Cloud Config配置中心Git

    微服务以单个接口为颗粒度,一个接口可能就是一个项目,如果每个项目都包含一个配置文件,一个系统可能有几十或上百个小项目组成,那配置文件也会有好多,对后续修改维护也是比较麻烦,就和前面的服务注册一样,服务 ...

  3. 微服务SpringCloud之Spring Cloud Config配置中心服务化

    在前面两篇Spring Cloud Config配置中心的博客中都是需要指定配置服务的地址url:spring.cloud.config.uri,客户端都是直接调用配置中心的server端来获取配置文 ...

  4. spring cloud --- config 配置中心 [本地、git获取配置文件]

    spring boot      1.5.9.RELEASE spring cloud    Dalston.SR1 1.前言 spring cloud config 配置中心是什么? 为了统一管理配 ...

  5. SpringCloud学习笔记(7):使用Spring Cloud Config配置中心

    简介 Spring Cloud Config为分布式系统中的外部化配置提供了服务器端和客户端支持,服务器端统一管理所有配置文件,客户端在启动时从服务端获取配置信息.服务器端有多种配置方式,如将配置文件 ...

  6. Spring Cloud Config 配置中心高可用

    详细参见 <Spring Cloud 与 Docker微服务架构实战> p163-9.10 Spring Cloud Config 与 Eureka 配合使用 p163-9.12 Conf ...

  7. Spring Cloud Config 配置中心

    请将远程配置文件的格式写对: 比如使用 *.yml 或者 *.properties yml: testconfig: testvalue properties: testconfig=testvalu ...

  8. Spring Cloud Config配置中心的使用

    一.概述 1. 为什么使用? 1> 配置文件太多,不方便维护 2> 配置文件一般都保存这各种明文显示的密码,无法保证配置内容的安全性,也无法做到按权限分配给个人 3> 更新配置项目需 ...

  9. 微服务SpringCloud之Spring Cloud Config配置中心SVN

    在回来的路上看到一个个的都抱着花,吃了一路的狗粮,原本想着去旁边的工业园里跑跑步呢,想想还是算了,人家过七夕,俺们过巴西.上一博客学习了Spring Cloud Config使用git作为配置中心,本 ...

随机推荐

  1. 【点分治】2019 首尔 icpc Gene Tree

    题目 链接:https://ac.nowcoder.com/acm/contest/15644/B来源:牛客网 A gene tree is a tree showing the evolution ...

  2. Linux安装与使用FTP服务-vsftpd

    简介 vsftpd 是"very secure FTP daemon"的缩写,安全性是它的一个最大的特点.vsftpd 是一个 UNIX 类操作系统上运行的服务器的名字,它可以运行 ...

  3. 自动化测试面试官:登录或注册时有验证码怎么处理?OCR图像识别技术大揭秘!

    本节大纲 读取cookie实现免登陆 pytesseract+tesseract-ocr实现图像识别 Pillow库对验证码截图 API接口实现图像识别 今天的这个技术点,为什么要给大家分享一下呢? ...

  4. 【js】Leetcode每日一题-子数组异或查询

    [js]Leetcode每日一题-子数组异或查询 [题目描述] 有一个正整数数组 arr,现给你一个对应的查询数组 queries,其中 queries[i] = [Li, Ri]. 对于每个查询 i ...

  5. Codeforces Round #660 (Div. 2)

    A. Captain Flint and Crew Recruitment 题意:定义了一种数(接近质数),这种数可以写成p*q并且p和q都是素数,问n是否可以写成四个不同的数的和,并且保证至少三个数 ...

  6. 33.2.NIO

    4.1概述[理解] BIO Blocking IO,阻塞型IO NIO No Blocking IO,非阻塞型IO 阻塞IO的弊端 在等待的过程中,什么事也做不了 非阻塞IO的好处 不需要一直等待,当 ...

  7. Windows系统下consul的安装、启动、配置

    阅读时长:3分钟 操作系统:Windows10 一.consul的安装 首先在consul.exe文件目录下的地址栏中输入cmd. 接着输入consul指令,敲击回车安装consul. 安装成功后会有 ...

  8. x小结:certutil -hashfile D:\1.exe MD5

    在Win7上,MD5不要使用小写,在Win10上没有这个问题 x小结:certutil -hashfile D:\1.exe MD5certutil -hashfile D:\1.exe SHA1ce ...

  9. 【转载】]基于RedHatEnterpriseLinux V7(RHEL7)下SPEC CPU 2006环境搭建以及测试流程 介绍、安装准备、安装、config文件以及运行脚本介绍

    https://www.codetd.com/article/1137423 <版权声明:本文为博主原创文章,未经博主允许不得转载> 本次利用SPECCPU2006测试工具来进行Intel ...

  10. 037.Python的UDP语法

    UDP语法 1 创建一个socket的UDP对象 import socket #创建对象 socket.SOCK_DGRAM 代表UDP协议 sk = socket.socket(type=socke ...