Spring Cloud Config提供了分布式系统中配置功能的服务端与客户端支持。对于不同环境的应用程序它的服务端提供了一种中心化的配置管理方式。并且其不仅适用于Spring的应用程序,其它语言开发的程序同样能够使用(这点很重要)。

环境要求

  • 首先需要安装JDK1.8或以上版本
  • IDE可以选用Spring Tool Suite(STS)或者IntelliJ IDEA,本文选用STS作为例子

服务端

在文件菜单,点击新建Spring Starter Project。

在输入工程名(这里是configuration-service)后选择下一步(Next),在依赖项中选中Config Server。

之后,点击Finish,即可完成工程的创建。

接着,在ConfigurationServiceApplication类上添加@EnableConfigServer注解。

package com.example.demo;

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

最后一步,在application.properties文件里配置可用的端口及用于保存服务端配置的仓库地址。

server.port=9555

#spring.profiles.active=native
#spring.cloud.config.server.native.searchLocations=file:///${USERPROFILE}/Desktop/config
spring.cloud.config.server.git.uri=file:///${USERPROFILE}/Desktop/config

Spring Cloud Config中最常用的是git仓库,这种场景下通过对spring.cloud.config.server.git.uri指定具体路径即可达成配置目的。

当然也可以不用git仓库,比如使用单纯的文件管理。

这时就需要同时使用spring.profiles.active=nativespring.cloud.config.server.native.searchLocations={配置文件目录路径}这两条配置项。

做为例子,在file:///${USERPROFILE}/Desktop/config目录下建立了一个application.properties文件,里面加入一条配置message=Hello World!

启动这个应用,一个Spring Cloud Server便产生了。

测试下其是否正常工作,可以在浏览器中输入http://localhost:9555/application/dev地址查看。如果是类似下列的结果,说明是正常的。

{"name":"application","profiles":["dev"],"label":null,"version":"c0f022755482d4a98f66dc19c8c4e0af512dc4f2","state":null,"propertySources":[{"name":"file:///C:\\Users\\Ken/Desktop/config/application.properties","source":{"message":"Hello World!"}}]}

客户端

同样是选择新建Spring Starter Project。但这次在依赖项中选择Config Client。

完成创建后,再在pom.xml中追加两个依赖。

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

接着在ConfigurationClientApplication文件里加入一个Controller。

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.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @SpringBootApplication
public class ConfigurationClientApplication { public static void main(String[] args) {
SpringApplication.run(ConfigurationClientApplication.class, args);
}
} @RefreshScope
@RestController
class MessageRestController { @Value("${message:Hello default}")
private String message; @RequestMapping("/message")
String getMessage() {
return this.message;
}
}

如果此时没有运行Config Server而是单独运行Config Client的话,可以看到以下的结果:

说明程序中获取的是代码里默认的配置项。

下面,开始打通与Config Server的连接。

在Config Client的application.properties文件配置应用程序启动端口server.port=9666

再在相同resources目录下新建一个bootstrap.propertiess文件,加入与Config Server关联配置。

spring.cloud.config.uri=http://localhost:9555

然后,先启动Config Server应用程序,再启动Config Client应用程序。

结果说明Config Client成功从Config Server获得配置内容。

接下来,尝试将配置仓库目录下application.properties文件内的配置项改成message=Hello World, Spring Cloud!

刷新http://localhost:9666/message页面。结果未做变化。这是因为默认场景下,Config Client只在启动时向Config Server获取一次配置。

先把message配置改回message=Hello World!

在Config Client的application.properties中添加management.endpoints.web.exposure.include=*配置。

重新启动Config Client。

再次将message配置改成message=Hello World, Spring Cloud!

然后在命令行中执行curl localhost:9666/actuator/refresh -d {} -H "Content-Type: application/json"更新配置。

刷新页面后可以看到预期的结果:

初探Spring Cloud Config的更多相关文章

  1. spring cloud config 入门

    简介 Spring cloud config 分为两部分 server client config-server 配置服务端,服务管理配置信息 config-client 客户端,客户端调用serve ...

  2. Spring Cloud Config

    Spring Cloud Config provides server and client-side support for externalized configuration in a dist ...

  3. Spring Cloud官方文档中文版-Spring Cloud Config(上)

    官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#spring-cloud-feign 文中例子我做了一些测试在:http ...

  4. Spring Cloud官方文档中文版-Spring Cloud Config(下)-客户端等

    官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#_serving_alternative_formats 文中例子我做了 ...

  5. SpringCloud的配置管理:Spring Cloud Config

    演示如何使用ConfigServer提供统一的参数配置服务 ###################################################################一.概 ...

  6. 搭建spring cloud config

    很久没更新了,因为不是专职研究spring cloud,因此更新速度得看工作强度大不大,每天能抽出的时间不多,如果更新太慢了,并且有小伙伴看的话,请见谅了. Spring Cloud简介 Spring ...

  7. Spring Cloud Config - RSA简介以及使用RSA加密配置文件

    简介 RSA非对称加密有着非常强大的安全性,HTTPS的SSL加密就是使用这种方法进行HTTPS请求加密传输的.因为RSA算法会涉及Private Key和Public Key分别用来加密和解密,所以 ...

  8. Spring Cloud Config 分布式配置中心使用教程

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

  9. 【spring实战第五版遇到的坑】第14章spring.cloud.config.uri和token配置项无效

    本文使用的Spring Boot版本为:2.1.4.RELEASE Spring Cloud版本为:Greenwich.SR1 按照书上的做法,在application.yml中配置配置服务器的地址和 ...

随机推荐

  1. nginx https 配置样例

    站点nginx https 配置模板 第一章 nginx 支持https 配置样例 其他 相关链接地址 第一章 nginx 支持https 配置样例 说明:https 段配置参数说明 Server 段 ...

  2. JDK自带的运行监控工具JConsole观察分析Java程序的运行

    原文地址:https://blog.csdn.net/libaolin198706231987/article/details/55057149 一.JConsole是什么 从Java 5开始 引入了 ...

  3. c# 创建项目时提示:未能正确加载“microsoft.data.entity.design.bootstrappackage.。。。。

    google了下, 果然找到办法了.看有的说要卸载,再清理注册表,真心不愿意啊,这安装一次就得好长时间.还好找到了这篇博文,无需卸载重安装,很简单的解决的方式,具体见http://blog.sina. ...

  4. Android Studio配置Android Annotations框架详解--说说那些坑

    我们开发过程中都需要写些findViewByid.serOnclickListener等类似的代码,虽然不费事,但是一个项目下来,工作量还是很大的.为了节省工作量,运生了很多对应的注解框架.网上的博客 ...

  5. ipa重签名

    为什么要研究重签名问题?将程序打包成ipa包后,ipa包中会包含Provisioning Profile和_CodeSignature等文件,里面包含了对整个ipa的签名信息. 一旦改动ipa中的不论 ...

  6. pandas DataFrame(4)-向量化运算

    pandas DataFrame进行向量化运算时,是根据行和列的索引值进行计算的,而不是行和列的位置: 1. 行和列索引一致: import pandas as pd df1 = pd.DataFra ...

  7. 消息推送之百度云推送Android集成与使用方法

    这两天由于项目需要,研究了一下百度云推送,本来这事没什么多大工作量的,但注册百度开发者账户创建应用令我蛋疼菊紧了好一阵,这些东西做了对技术没啥提升,不做又不行,必经之路.好在我耗费了N多个毫毫秒秒后稀 ...

  8. java框架篇---hibernate之session状态

    Session接口是Hibernate向程序提供操纵数据库的最主要接口,是单线程对象,它提供了基本的保存.更新.删除和查询方法.它有一个缓存,保存了持久化对象,当清理缓存时,按照这些持久化对象同步更新 ...

  9. 性能优化系列八:MYSQL的配置优化

    一.关键配置 1. 配置文件的位置 MySQL配置文件 /etc/my.cnf 或者 /etc/my.cnf.d/server.cnf 几个关键的文件:.pid文件,记录了进程id.sock文件,是内 ...

  10. SQL导出数据到EXCEL的问题

    DTS导出向导 不会 我这有个是用C#语言写的 try { Excel.Application xApp = new Excel.ApplicationClass(); xApp.Visible = ...