注:本人所有的spring-cloud系列的文章均为黑马的《Spring Cloud微服务架构开发》的个人笔记。
分布式配置中心Spring Cloud Config
注:本人所有的spring-cloud系列的文章均为黑马的《Spring Cloud微服务架构开发》的个人笔记。
学习目标:
- 了解SpringCloudConfig的作用及特性
- 掌握使用Config Server 从本地读取配置文件
- 掌握使用Config Server 从远程Git仓库读取配置文件
- 掌握如何搭建高可用的Config Server
- 熟悉如何使用Spring Cloud Bus自动刷新配置文件
1.简介
在实际开发中,集群会有很多微服务,每个微服务都有相应的配置,SpringCloudConfig对集群的配置进行集中管理。并且配置修改后无需重启服务。
Spring Cloud Config 为分布式系统中的外部配置提供了配置服务器(简称服务器)和配置客户端(简称客户端),即Config Server 和Config Client。 通过对Config Server 和Config Client 的配置,可以很好地管理集群中的配置文件。
Config Server服务器的主要用途:
- 分布式配置中心,独立的微服务应用,用来连接配置仓库(Git)并为客户端提供获取配置信息的访问接口
- 对配置文件中的属性进行加密和解密
- 通过使用@EnableConfigServer.注解可以简单地嵌入Spring Boot 的应用中
Condig Client客户端的主要用途:
- 绑定配置服务器,使用远程仓库的配置文件中的属性来初始化Spring容器。
- 对配置文件中的属性进行加密和解密
2.Config Server从本地仓库读取配置文件
此方式将所有的配置文件统一放在ConfigServer项目的resources目录下,
Config Server通过暴露的HTTP API来读取配置文件。
2.1 搭建Config Server项目
搭建一个springboot项目config-server,
2.1.1 pom.xml
添加Config-Server、Test、Eureka-Server依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>3.1.0</version>
</dependency>
2.1.2 在启动类添加注解
@EnableConfigServer
2.1.3 application.yml
spring:
application:
name: config-server
cloud:
config:
server:
native:
search-locations: classpath:/shared #读取路径
profiles:
active: native #Config server从本地读取配置路径
server:
port: 8769
2.1.4 在resources目录新建shared文件夹
用于存放本地配置文件,在此目录下新建config-client-dev.yml文件,用作后序创建的config-client项目的dev开发环境下的配置文件,配置如下:
server:
port: 8762
foo: foo version 1
2.2 搭建Config Client工程
2.2.1 pom.xml
引入Config、web、test依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2.2.2 在resources目录下创建bootstrap.yml文件
bootstrap.yml相对于application.yml执行时优先级更高
spring:
application:
name: config-client
profiles:
active: dev #指定配置文件的开发环境
cloud:
config:
uri: http://localhost:8769 #指定config server的uri
fail-fast: true #是否开启快速失败
注意是uri 不是url
区别:uri与url的区别与联系
2.2.3 在启动类添加hi()方法测试
package com.li.configclient;
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 = "/foo")
public String hi(){
return foo;
}
}
2.2.4 测试
先启动config-server,,后启动config-client,控制台可以看到如下:
3.Config Server从Git仓库读取配置文件
3.1 登录码云创建配置文件
1.新建仓库——仓库名称(hello)——初始化仓库(java)
——添加 .gitignore(默认)——设置模板(Readme)——分支类型(单分支)----点击"创建”
2.文件——新建文件(config-client-dev.yml)——复制2.1.4配置文件----点击“提交”
3.复制仓库地址——打开config-server项目的application.yml,粘贴到uri位置
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: #粘贴地址
username: #
password: #
label: master #Git仓库的分支名
server:
port: 8769
启动运行:
4.搭建高可用的Config Server
4.1 搭建eureka-server注册中心
4.2 在config-server和config-client中添加依赖eureka-client
4.3 修改配置文件
在config-server的application.yml和config-client的bootstrap.yml添加:
eureka:
client:
serviceUrl:
defaultZone:http://localhost:7000/eureka
4.4 在启动类添加@EnableEurekaClient
4.5 复制一份config-server达到集群的作用
修改复制后的config-server的端口号:8768
4.6 测试
依次启动eureka-server,config-server,config-server2,config-client
启动成功后达到高可用的Config Server
.
5.SpringCloudConfig和SpringCloudBus整合实现配置自动刷新
5.1 引入依赖
在config-server和config-client的pom中引入:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
5.2 修改配置文件
在server端application.yml和client端的bootstrap.yml配置文件中暴露消息总线的地址
management: # 暴露触发消息总线的地址
endpoints:
web:
exposure:
include: "bus-refresh"
5.3 在config-client 中的启动类添加@RefreshScope
5.4 测试
启动 config-server 和 config-client
访问 http://localhost:8762/foo
修改config-client-dev.yml 为 foo version 100
打开cmd,输入
curl -x POST http://localhsot:8769/actuator/bus-refresh
(也可以通过Postman发送post请求http://localhsot:8769/actuator/bus-refresh)
此时,config-client就会向SpringCloudBus发送一个配置更新事件并更新配置
再此访问 http://localhost:8762/foo