前言

本篇主要介绍的是SpringCloud中的分布式配置中心(SpringCloud Config)的相关使用教程。

SpringCloud Config

Config 介绍

Spring Cloud Config项目是一个解决分布式系统的配置管理方案。它包含了Client和Server两个部分,server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client通过接口获取数据、并依据此数据初始化自己的应用。

开发准备

开发环境

  • JDK:1.8
  • SpringBoot:2.1.1.RELEASE
  • SpringCloud:Finchley

注:不一定非要用上述的版本,可以根据情况进行相应的调整。需要注意的是SpringBoot2.x以后,jdk的版本必须是1.8以上!

确认了开发环境之后,我们再来添加相关的pom依赖。

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>

SpringCloud Config 示例

目前SpringCloud Config的使用主要是通过Git/SVN方式做一个配置中心,然后每个服务从其中获取自身配置所需的参数。SpringCloud Config也支持本地参数配置的获取。如果使用本地存储的方式,在 application.propertiesapplication.yml 文件添加 spring.profiles.active=native 配置即可,它会从项目的 resources路径下读取配置文件。如果是读取指定的配置文件,那么可以使用 spring.cloud.config.server.native.searchLocations = file:D:/properties/ 来读取。

服务端

首先是服务端这块,首先创建一个注册中心,为了进行区分,创建一个springcloud-config-eureka的项目。 代码和配置和之前的基本一样。

application.properties配置信息:

配置信息:

spring.application.name=springcloud-hystrix-eureka-server
server.port=8005
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:8005/eureka/

配置说明:

  • spring.application.name: 这个是指定服务名称。
  • server.port:服务指定的端口。
  • eureka.client.register-with-eureka:表示是否将自己注册到Eureka Server,默认是true。
  • eureka.client.fetch-registry:表示是否从Eureka Server获取注册信息,默认为true。
  • eureka.client.serviceUrl.defaultZone: 这个是设置与Eureka Server交互的地址,客户端的查询服务和注册服务都需要依赖这个地址。

服务端这边只需要在SpringBoot启动类添加@EnableEurekaServer注解就可以了,该注解表示此服务是一个服务注册中心服务。

代码示例:


@SpringBootApplication
@EnableEurekaServer
public class ConfigEurekaApplication { public static void main(String[] args) {
SpringApplication.run(ConfigEurekaApplication.class, args);
System.out.println("config 注册中心服务启动...");
}
}

创建好了注册中心之后,我们再来创建一个配置中心,用于管理配置。

创建一个springcloud-config-server的项目。然后在application.properties配置文件添加如下配置:

配置信息:

 spring.application.name=springcloud-config-server
server.port=9005
eureka.client.serviceUrl.defaultZone=http://localhost:8005/eureka/
spring.cloud.config.server.git.uri = https://github.com/xuwujing/springcloud-study/
spring.cloud.config.server.git.search-paths = /springcloud-config/config-repo
spring.cloud.config.server.git.username =
spring.cloud.config.server.git.password =

配置说明:

  • spring.application.name: 这个是指定服务名称。
  • server.port:服务指定的端口。
  • eureka.client.serviceUrl.defaultZone: 这个是设置与Eureka Server交互的地址,客户端的查询服务和注册服务都需要依赖这个地址。
  • spring.cloud.config.server.git.uri: 配置的Git长裤的地址。
  • spring.cloud.config.server.git.search-paths: git仓库地址下的相对地址 多个用逗号","分割。
  • spring.cloud.config.server.git.username:git仓库的账号。
  • spring.cloud.config.server.git.password:git仓库的密码。

注:如果想使用本地方式读取配置信息,那么只需将spring.cloud.config.server.git的配置改成spring.profiles.active=native,然后在resources路径下新增一个文件即可。

这里为了进行本地配置文件测试,新建一个configtest.properties配置文件,添加如下内容:


word=hello world

代码这块也很简单,在程序主类中,额外添加@EnableConfigServer注解,该注解表示启用config配置中心功能。代码如下:

、、、

@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication { public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
System.out.println("配置中心服务端启动成功!");
}
}

、、、

完成上述代码之后,我们的配置中心服务端已经构建完成了。

客户端

我们新建一个springcloud-config-client的项目,用于做读取配置中心的配置。pom依赖还是和配置中心一样,不过需要新增一个配置,用于指定配置的读取。

创建一个bootstrap.properties文件,并添加如下信息:

配置信息:

spring.cloud.config.name=configtest
spring.cloud.config.profile=pro
spring.cloud.config.label=master
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=springcloud-config-server
eureka.client.serviceUrl.defaultZone=http://localhost:8005/eureka/

配置说明:

  • spring.cloud.config.name: 获取配置文件的名称。
  • spring.cloud.config.profile: 获取配置的策略。
  • spring.cloud.config.label:获取配置文件的分支,默认是master。如果是是本地获取的话,则无用。
  • spring.cloud.config.discovery.enabled: 开启配置信息发现。
  • spring.cloud.config.discovery.serviceId: 指定配置中心的service-id,便于扩展为高可用配置集群。
  • eureka.client.serviceUrl.defaultZone: 这个是设置与Eureka Server交互的地址,客户端的查询服务和注册服务都需要依赖这个地址。

:上面这些与spring-cloud相关的属性必须配置在bootstrap.properties中,config部分内容才能被正确加载。因为bootstrap.properties的相关配置会先于application.properties,而bootstrap.properties的加载也是先于application.properties。需要注意的是eureka.client.serviceUrl.defaultZone要配置在bootstrap.properties,不然客户端是无法获取配置中心参数的,会启动失败!

application.properties配置

spring.application.name=springcloud-config-client
server.port=9006

配置说明:

  • spring.application.name: 这个是指定服务名称。
  • server.port:服务指定的端口。

程序主类代码,和之前的基本一致。代码如下:

代码示例:


@EnableDiscoveryClient
@SpringBootApplication
public class ConfigClientApplication { public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
System.out.println("配置中心客户端启动成功!");
}
}

为了方便查询,在控制中进行参数的获取,并返回。@Value注解是默认是从application.properties配置文件获取参数,但是这里我们在客户端并没有进行配置,该配置在配置中心服务端,我们只需指定好了配置文件之后即可进行使用。

代码示例:


@RestController
public class ClientController { @Value("${word}")
private String word; @RequestMapping("/hello")
public String index(@RequestParam String name) {
return name+","+this.word;
}
}

到此,客户端项目也就构建完成了。

功能测试

完成如上的工程开发之后,我们来进行测试。

本地测试

首先我们把springcloud-config-server项目的application.properties配置文件添加spring.profiles.active=native配置,注释掉spring.cloud.config.server.git相关的配置,然后在src/main/resources目录下新建一个configtest.properties文件,然后在里面添加一个配置 word=hello world

添加完成之后,我们依次启动springcloud-config-eurekaspringcloud-config-serverspringcloud-config-client这三个项目。启动成功之前,先看来看看配置中心服务端的配置文件获取,在浏览器输入:

http://localhost:9005/configtest-1.properties

查看该文件的配置信息。

:配置文件的名称是configtest.properties,但是如果直接该名称的话是获取不到的,因为在配置文件名需要通过-来进行获取,如果配置文件名称没有-,那么添加了-之后,会自动进行匹配搜索。

springcloud config 的URL与配置文件的映射关系如下:

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

上面的url会映射{application}-{profile}.properties对应的配置文件,{label}对应git上不同的分支,默认为master。

界面返回:

word: hello world

然后调用客户端的接口,查看是否能够获取配置信息。在浏览器上输入:

http://localhost:9006//hello?name=pancm

界面返回:

pancm,hello world

示例图:



Git测试

在完成本地测试之后,我们把这个spring.profiles.active=native配置注释掉,解除spring.cloud.config.server.git相关的注释(账号和密码要填写真实的),然后在git仓库上建立一个config-repo 文件夹,新建configtest-pro.propertiesconfigtest-dev.properties两个配置,这两个的配置分别是 word=hello world!! word=hello world!, 然后和configtest.properties配置文件一起上传到config-repo 文件夹中。

首先在浏览器输入:

http://localhost:9005/configtest-dev.properties

浏览器返回:

word: hello world!

然后再浏览器输入:

http://localhost:9005/configtest-pro.properties

浏览器返回:

word: hello world!!

上传了configtest.properties文件,但是这个文件名称没有-,我们想获取其中参数的信息的话,可以在然后-随意添加一个参数,它会自动进行匹配,在浏览器输入:

http://localhost:9005/configtest-1.properties

浏览器返回:

word: hello world

然后进行客户端接口调用测试,在浏览器输入:

http://localhost:9006/hello?name=pancm

浏览器返回:

pancm,Hello World!!

由于这里我配置的前缀是 pro ,所以读取的是 configtest-pro.properties 文件的数据,想要获取其他的配置,修改spring.cloud.config.profile配置即可。

示例图:





其他

项目地址

基于SpringBoot2.x、SpringCloud的Finchley版本开发的地址:https://github.com/xuwujing/springcloud-study

基于SpringBoot1.x、SpringCloud 的Dalston版本开发的地址: https://github.com/xuwujing/springcloud-study-old

如果感觉项目不错,希望能给个star,谢谢!

音乐推荐

原创不易,如果感觉不错,希望留言推荐!您的支持是我写作的最大动力!

版权声明:

作者:虚无境

博客园出处:http://www.cnblogs.com/xuwujing

CSDN出处:http://blog.csdn.net/qazwsxpcm    

个人博客出处:http://www.panchengming.com

SpringCloud学习系列之四-----配置中心(Config)使用详解的更多相关文章

  1. SpringCloud学习系列之五-----配置中心(Config)和消息总线(Bus)完美使用版

    前言 在上篇中介绍了SpringCloud Config的使用,本篇则介绍基于SpringCloud(基于SpringBoot2.x,.SpringCloud Finchley版)中的分布式配置中心( ...

  2. 学习一下 SpringCloud (五)-- 配置中心 Config、消息总线 Bus、链路追踪 Sleuth、配置中心 Nacos

    (1) 相关博文地址: 学习一下 SpringCloud (一)-- 从单体架构到微服务架构.代码拆分(maven 聚合): https://www.cnblogs.com/l-y-h/p/14105 ...

  3. springcloud(四):应用配置中心config的安全设置

    springcloud应用配置中心config的安全设置 在springcloud应用开发中,为了方便在线管理我们的配置文件,通常会配一个配置中心config-server,这里托管着应用的一些配置文 ...

  4. 大数据学习系列之五 ----- Hive整合HBase图文详解

    引言 在上一篇 大数据学习系列之四 ----- Hadoop+Hive环境搭建图文详解(单机) 和之前的大数据学习系列之二 ----- HBase环境搭建(单机) 中成功搭建了Hive和HBase的环 ...

  5. SpringCloud全家桶学习之分布式配置中心----Config(七)

    一.概述 (1)背景 微服务意味着将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中出现大量的服务.由于每个服务都需要配置必要的配置信息才能运行,所以一套集中式的.动态的配置管理 ...

  6. 大数据学习系列之三 ----- HBase Java Api 图文详解

    版权声明: 作者:虚无境 博客园出处:http://www.cnblogs.com/xuwujing CSDN出处:http://blog.csdn.net/qazwsxpcm 个人博客出处:http ...

  7. 【SignalR学习系列】8. SignalR Hubs Api 详解(.Net C# 客户端)

    建立一个 SignalR 连接 var hubConnection = new HubConnection("http://www.contoso.com/"); IHubProx ...

  8. 【SignalR学习系列】6. SignalR Hubs Api 详解(C# Server 端)

    如何注册 SignalR 中间件 为了让客户端能够连接到 Hub ,当程序启动的时候你需要调用 MapSignalR 方法. 下面代码显示了如何在 OWIN startup 类里面定义 SignalR ...

  9. 【深度学习系列】卷积神经网络CNN原理详解(一)——基本原理

    上篇文章我们给出了用paddlepaddle来做手写数字识别的示例,并对网络结构进行到了调整,提高了识别的精度.有的同学表示不是很理解原理,为什么传统的机器学习算法,简单的神经网络(如多层感知机)都可 ...

随机推荐

  1. logistic 回归

    logistic回归 1.算法思想 根据给定的数据集确定分类的边界.这个分类的边界就是我们所要求的回归函数. 所谓的回归其实就是最佳拟合,回归函数就是确定最佳回归参数,然后对不同的特征赋予不同的权重 ...

  2. __new__()方法的使用和实例化

    Python中__new__()方法的使用和实例化 1 2 new()是在新式类中新出现的方法,它作用在构造方法init()建造实例之前,可以这么理解,在Python 中存在于类里面的构造方法init ...

  3. RNN(Recurrent Neural Network)的几个难点

    1. vanish of gradient RNN的error相对于某个时间点t的梯度为: \(\frac{\partial E_t}{\partial W}=\sum_{k=1}^{t}\frac{ ...

  4. 读《图解HTTP》有感-(确保WEB安全的HTTPS)

    写在前面 该章节分析当前使用的HTTP协议中存在的安全性问题,以及采用HTTPS协议来规避这些可能存在的缺陷 正文 1.HTTP的缺点 1.1.由于HTTP不具备加密功能,所以在通信链路上,报文是以明 ...

  5. redis的持久化之AOF

    AOF Redis 分别提供了 RDB 和 AOF 两种持久化机制: RDB 将数据库的快照(snapshot)以二进制的方式保存到磁盘中. AOF 则以协议文本的方式,将所有对数据库进行过写入的命令 ...

  6. token 防止csrf

    转自:ttps://www.ibm.com/developerworks/cn/web/1102_niugang_csrf/#icomments 当前防御 CSRF 的几种策略 验证 HTTP Ref ...

  7. 第二章:第一个Netty程序

    第一步:设置开发环境 • 安装JDK,下载地址http://www.oracle.com/technetwork/java/javase/archive-139210.html   • 下载netty ...

  8. 推荐一个比crontab更好用的东西:crongo

    This is a crontab service that supports hot plug and high performance. In addition, it supports seco ...

  9. Ubuntu安装和卸载.bundle格式的VMware

    本文由荒原之梦原创,原文链接:http://zhaokaifeng.com/?p=628 前言: 本文中用于演示的.bundle文件是VMware-Workstation-Full-14.1.1-75 ...

  10. 基于Spring Cloud、JWT 的微服务权限系统设计

    基于Spring Cloud.JWT 的微服务权限系统设计 https://gitee.com/log4j/pig https://github.com/kioyong/spring-cloud-de ...