Spring Cloud Config 使用总结
Spring Cloud Config 使用总结
源码 https://github.com/ChangMuChen/Spring-Boot/tree/master/studies/sourcecode/spring-cloud-config
一、介绍
Spring Cloud Config
Spring Cloud Config为分布式系统中的外部化配置提供服务器和客户端支持。使用Config Server,您可以在所有环境中管理应用程序的外部属性。客户端和服务器上的概念映射与Spring Environment和PropertySource抽象,因此它们非常适合Spring应用程序,但可以与任何语言运行的任何应用程序一起使用。当应用程序通过部署管道从开发到测试并进入生产时,您可以管理这些环境之间的配置,并确保应用程序具有迁移时需要运行的所有内容。服务器存储后端的默认实现使用git,因此它可以轻松支持配置环境的标签版本,以及可用于管理内容的各种工具。添加替代实现并使用Spring配置插入它们很容易。
Spring Cloud Config Server功能
- 用于外部配置的HTTP,基于资源的API(名称 值对或等效的YAML内容)
- 加密和解密属性值(对称或非对称)
- 使用可轻松嵌入Spring Boot应用程序
- 可以轻松的结合Eureka实现高可用
- 可以轻松的结合Spring Cloud Bus实现自动化持续集成
Config Client功能(适用于Spring应用程序)
- 绑定到Config Server并Environment使用远程属性源初始化Spring
- 加密和解密属性值(对称或非对称)
- 结合Eureka实现服务发现
二、基础使用
1. 搭建服务端
Step1. 新建Spring Boot项目 configserver
Step2. 引入主要依赖 spring-cloud-config-server
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
Step3. 引入 spring-boot-starter-web
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Step4. 主程序添加注释 @EnableConfigServer
@SpringBootApplication
@EnableConfigServer
public class ConfigserverApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigserverApplication.class, args);
}
}
Step5. 设置属性
这里使用
.properties
的方式进行配置。你可以自行更换为同等效果的.yml
方式。配置信息需要放在bootstrap.properties
中。
1)Git方式存储配置文件示例
#配置文件存放在Git的情况
spring.cloud.config.server.git.uri=http://172.16.50.98:9999/changdaohang/demo_configs.git
spring.cloud.config.label=master
spring.cloud.config.server.git.search-paths=/**
spring.cloud.config.server.git.username=username
spring.cloud.config.server.git.password=password
2)本地存储配置文件
#配置文件存放在本地的情况
#注意:文件夹要有访问权限
spring.profiles.active=native
spring.cloud.config.server.native.search-locations=C:/IdeaProjets/demo_configs/
Step6. 启动
备注
我们可以直接通过restful api 的方式进行访问配置。路由如下:
#映射说明如下
#/{application}/{profile}[/{label}]
#/{application}-{profile}.yml
#/{label}/{application}-{profile}.yml
#/{application}-{profile}.properties
#/{label}/{application}-{profile}.properties
例如:http://localhost:8080/testapp/dev 或 http://localhost:8080/testapp-dev.properties
对应的配置为/testapp-dev.properties
当你启动服务端,你可以在Mappings中看到接口开放情况,部分如下:
2. 业务服务配置
Step1. 新建Spring Boot项目 servera
Step2. 引入主要依赖 spring-cloud-starter-config
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
Step3. 引入 spring-boot-starter-web
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Step4. 主程序添加注释 @EnableDiscoveryClient
@SpringBootApplication
@EnableDiscoveryClient
public class ServeraApplication {
public static void main(String[] args) {
SpringApplication.run(ServeraApplication.class, args);
}
}
Step5. 创建配置文件
以Git为例:
在git项目中创建servera-dev.properties文件,写入以下示例内容:
name=David
Step6. 配置参数
这里使用
.properties
的方式进行配置。你可以自行更换为同等效果的.yml
方式。配置信息需要放在bootstrap.properties
中。
spring.application.name=servera
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri=http://localhost:8071/
注意:配置中
servera
值与dev
结合对应到servera-dev.properties
文件,并且使用master
对应到Git中的master
分支。http://localhost:8080/
为 配置中心服务地址。
Step7. 新建测试api
新建
TestController
控制器,代码如下:
@RestController
@RequestMapping("api")
@RefreshScope
public class TestController {
@Value("${name}")
private String confignameValue;
@GetMapping("/confignamevalue")
@ResponseBody
public String returnConfignameValue(){
return confignameValue;
}
}
说明:其中
@RefreshScope
注解是以后为了自动更新配置用的。这里可以不加。
通过@Value("${name}")
注解,可以对应到servera-dev.properties
中的name=David
键值对,并将值David
赋给confignameValue
Step8. 启动程序
访问
http://localhost:8081/api/confignamevalue
显示结果为:David
到这里最基本的使用就完成了
三、自动更新
这里介绍使用 Monitor + Actuator + Bus + RabbitMQ + GitLab Webhooks 来实现当提交配置后,通知服务更新配置。
请自行搭建
RabbitMQ
服务环境。
1. 配置中心服务端
Step1. 在pom.xml
中添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-monitor</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
其中
spring-boot-starter-actuator
整合了一些/bus-fresh
的接口,可以用于自主触发刷新服务配置。
spring-cloud-config-monitor
用于对接 gitlab 的webhooks,用于触发刷新服务配置。
spring-cloud-starter-bus-amqp
用于对接rabbitMQ来广播服务更新事件,通知相关的服务进行配置更新。
Step2.配置参数
在
bootstrap.properties
中加入以下配置:
#配置消息中心-rabbitmq
spring.rabbitmq.host=192.168.1.119
spring.rabbitmq.port=5672
spring.rabbitmq.username=username
spring.rabbitmq.password=password
#开放actuator下的所有功能api(你也可以选择性开放)
management.endpoints.web.exposure.include=*
2. 配置客户端
Step1. 在pom.xml
中添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
其中
spring-boot-starter-actuator
配合服务端,处理服务端关播发来的配置更新事件。也可以用于自主触发刷新服务配置。
spring-cloud-starter-bus-amqp
用于接收rabbitMQ消息,通知相关的服务进行配置更新。
Step2.配置参数
在
bootstrap.properties
中加入以下配置:
#配置消息中心-rabbitmq
spring.rabbitmq.host=192.168.1.119
spring.rabbitmq.port=5672
spring.rabbitmq.username=username
spring.rabbitmq.password=password
#修复BUG-github webhook 只能刷新config server 无法刷新config client的问题
spring.cloud.bus.id=${vcap.application.name:${spring.application.name:application}}:${vcap.application.instance_index:${spring.cloud.config.profile:${local.server.port:${server.port:0}}}}:${vcap.application.instance_id:${random.value}}
#开放actuator下的所有功能api(你也可以选择性开放)
management.endpoints.web.exposure.include=*
Step3. 在控制器中添加 @RefreshScope
注解,用于实时刷新
@RestController
@RequestMapping("api")
@RefreshScope
public class TestController {
...
}
3. GitLab Webhooks配置
Step1. 打开GitLab项目主页,依次进入【设置】-【集成】
Step2. 添加WebHook.设置调用时机和回调地址:http://192.168.1.28:8080/monitor?path=*
Step3. 保存
path用于定位哪些服务应该更新配置。这里使用 * 来通知所有服务。
4. 测试
Step1. 依次启动 configserver
和 servera
Step2. 访问 http://localhost:8081/api/confignamevalue
显示结果为:David
Step3. 修改gitlab 中的 servera-dev.properties
为 name=David Mu
并Commit
Step4. 访问 http://localhost:8081/api/confignamevalue
显示结果为:David Mu
这种方式服务有可能不会立马刷新配置,属于正常情况。
四、高可用
这里介绍使用 Config + Eureka 来实现高可用。
请自行搭建
Eureka
服务环境。
1. 服务端配置
Step1. 引入 spring-cloud-starter-netflix-eureka-client
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
Step2. 配置Eureka相关参数
#设置程序基础配置
server.port=8080
spring.application.name=configserver
#eureka 配置
eureka.instance.hostname=127.0.0.1
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${eureka.instance.hostname}:${server.port}
eureka.client.service-url.defaultZone=http://127.0.0.1:8061/eureka/,http://127.0.0.1:8062/eureka/,http://127.0.0.1:8063/eureka/
Step3. 添加注解 @EnableEurekaClient
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigserverApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigserverApplication.class, args);
}
}
启动程序,在eureka注册中就可以看到服务已注册。
2. 客户端配置
Step1. 引入 spring-cloud-starter-netflix-eureka-client
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
Step2. 配置Eureka相关参数
#设置程序基础配置
server.port=8080
spring.application.name=configserver
#eureka 配置
eureka.instance.hostname=127.0.0.1
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${eureka.instance.hostname}:${server.port}
eureka.client.service-url.defaultZone=http://127.0.0.1:8061/eureka/,http://127.0.0.1:8062/eureka/,http://127.0.0.1:8063/eureka/
Step3. 修改原来的参数,如下:
spring.cloud.config.label=master
spring.cloud.config.profile=dev
#启用服务发现
spring.cloud.config.discovery.enabled=true
#指定配置服务id
spring.cloud.config.discovery.service-id=CONFIGSERVER
#撤销原来的指向方式
#spring.cloud.config.uri=http://localhost:8071/
Spring Cloud Config 使用总结的更多相关文章
- spring cloud config 入门
简介 Spring cloud config 分为两部分 server client config-server 配置服务端,服务管理配置信息 config-client 客户端,客户端调用serve ...
- Spring Cloud Config
Spring Cloud Config provides server and client-side support for externalized configuration in a dist ...
- Spring Cloud官方文档中文版-Spring Cloud Config(上)
官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#spring-cloud-feign 文中例子我做了一些测试在:http ...
- Spring Cloud官方文档中文版-Spring Cloud Config(下)-客户端等
官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#_serving_alternative_formats 文中例子我做了 ...
- SpringCloud的配置管理:Spring Cloud Config
演示如何使用ConfigServer提供统一的参数配置服务 ###################################################################一.概 ...
- 搭建spring cloud config
很久没更新了,因为不是专职研究spring cloud,因此更新速度得看工作强度大不大,每天能抽出的时间不多,如果更新太慢了,并且有小伙伴看的话,请见谅了. Spring Cloud简介 Spring ...
- Spring Cloud Config - RSA简介以及使用RSA加密配置文件
简介 RSA非对称加密有着非常强大的安全性,HTTPS的SSL加密就是使用这种方法进行HTTPS请求加密传输的.因为RSA算法会涉及Private Key和Public Key分别用来加密和解密,所以 ...
- Spring Cloud Config 分布式配置中心使用教程
一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud config ...
- 【spring实战第五版遇到的坑】第14章spring.cloud.config.uri和token配置项无效
本文使用的Spring Boot版本为:2.1.4.RELEASE Spring Cloud版本为:Greenwich.SR1 按照书上的做法,在application.yml中配置配置服务器的地址和 ...
- .NET Core微服务之基于Steeltoe使用Spring Cloud Config统一管理配置
Tip: 此篇已加入.NET Core微服务基础系列文章索引 => Steeltoe目录快速导航: 1. 基于Steeltoe使用Spring Cloud Eureka 2. 基于Steelt ...
随机推荐
- 2015/9/21 Python基础(17):绑定和方法调用
绑定和方法调用现在我们需要再次阐述Python中绑定(binding)的概念,它主要与方法调用相关联.方法是类内部定义的函数,这意味着方法是类属性而不是实例属性.其次,方法只有在其所属的类拥有实例时, ...
- Redis实战(六)管道
https://www.cnblogs.com/huangxincheng/p/6212406.html
- 查询PHP版本
查询php版本: phpinfo();
- 聊聊spring的那些扩展机制
1.背景 慎入:本文将会有大量代码出入. 在看一些框架源码的时候,可以看见他们很多都会和Spring去做结合.举个例子dubbo的配置: 很多人其实配置了也就配置了,没有去过多的思考:为什么这么配置s ...
- Sublime之插件的安装(一)
由于最近刚换了一个工作,所以决定重新申请一个blog,把工作当中遇到的一些问题记录下来,方便自己下次忘记,也希望能与一起需要的小伙伴一起共勉. 如果有不同的观点或者是不同的看法,大家都可以畅谈,我一直 ...
- JS设计模式——4.继承(概念)
类式继承 0.构造函数 一个简单的Person类 function Person(name){ this.name = name; } Person.prototype.getName = funct ...
- 初识PDO数据库抽象层
目录: 00x1 php中的pdo是什么? 00x2 pdo创建一个PDO对象 00x1 php中的pdo是什么? 就是操作数据库的方法,pdo就是把操作数据库的函数封装成一个pdo类,其间做了安全验 ...
- python进阶之py文件内置属性
前言 对于任何一个python文件来说,当python解释器运行一个py文件,会自动将一些内容加载到内置的属性中:一个模块我们可以看做是一个比类更大的对象. 查看模块的内置属性 我们先创建一个典型的p ...
- 一起来学redis(一)
redis是一个开源的,高性能的,基于键值对的缓存与存储系统通过提供多种键值数据类型来适应不同场景下的缓存与存储需求. 同时redis的诸多高层级功能使其可以胜任消息队列,任务队列等不同的角色. 特性 ...
- Windows搭建RobotFramework环境(一)
Robot Framework官网 http://robotframework.org/http://robotframework.org/ 安装说明 https://github.com/robot ...