Spring Cloud全文目录
源码
什么是微服务?有手就行
SpringCloud简介与5大常用组件
一、手把手教你搭建SpringCloud项目(一)搭建Maven父工程,傻瓜式操作
二、手把手教你搭建SpringCloud项目(二)生产者与消费者
三、手把手教你搭建SpringCloud项目(三)集成Eureka服务注册中心
四、手把手教你搭建SpringCloud项目(四)EurekaServer集群版搭建
五、手把手教你搭建SpringCloud项目(五)生产者集群版搭建
六、手把手教你搭建SpringCloud项目(六)actuator微服务信息完善与Eureka自我保护机制
七、手把手教你搭建SpringCloud项目(七)Eureka实现服务发现(Discovery)
八、手把手教你搭建SpringCloud项目(八)集成Consul服务注册中心
九、手把手教你搭建SpringCloud项目(九)集成Ribbon负载均衡器
十、手把手教你搭建SpringCloud项目(十)集成OpenFeign服务接口调用
十一、手把手教你搭建SpringCloud项目(十一)集成Hystrix之服务降级
十二、手把手教你搭建SpringCloud项目(十二)集成Hystrix之服务熔断
十三、手把手教你搭建SpringCloud项目(十三)集成Hystrix之图形化Dashboard实时监控
十四、手把手教你搭建SpringCloud项目(十四)集成Gateway新一代服务网关
十五、手把手教你搭建SpringCloud项目(十五)集成Config分布式配置中心
十六、手把手教你搭建SpringCloud项目(十六)集成Bus消息总线
十七、手把手教你搭建SpringCloud项目(十七)集成Stream消息驱动
十八、手把手教你搭建SpringCloud项目(十八)集成Sleuth分布式链路跟踪
文章持续更新中,欢迎关注!
为什么会有Spring Cloud Config分布式配置中心?
为什么要有用分布式配置中心这玩意儿?现在这微服务大军已经覆盖了各种大小型企业,每个服务的粒度相对较小,因此系统中会出现大量的服务,每个服务都要有自己都一些配置信息,或者相同的配置信息,可能不同环境每个服务也有单独的一套配置,这种情况配置文件数量比较庞大,维护起来相当费劲,举个栗子:
在开发的过程中,一般数据库是开发环境数据库,所有服务DB的IP配置为:92.168.0.1,突然老大说,开发环境换了,DB的IP要修改,这下可不好受了,所有模块挨个修改DB的配置,就问你难受不难受?
这个时候分布式配置中心就发挥了很大的优势,只需要修改配置中心配置,所有服务即可自动生效,爽不爽!
什么是Spring Cloud Config分布式配置中心?
Spring Cloud Config为微服务架构中的微服务提供了集中化的外部配置支持,配置服务器为各个不同的微服务应用的所有环境提供了中心的化额外部配置。如下图:
Spring Cloud Config分为服务端和客户端两部分,服务端也成为分布式配置中心,他是一个独立的微服务应用,用来连接配置服务器并未客户端提供获取配置信息,加密、解密信息等访问接口。
客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。
Spring Cloud Config都有什么作用?
- 集中管理配置文件,便于管理
- 不同的环境不同的配置,动态化的配置更新,分环境比如:dev/test/prod/bata/release
- 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配- 置中心统计拉去配置自己的信息。
- 当配置发生改变时,服务不需要重启即可感知到配置的变化并应用新的配置
- 将配置信息以Rest接口暴露,post/curi访问刷新即可
实现原理
其实这个实现原理相对比较简单一些,基于git的交互操作。
- 我们把配置文件存放到git上面
- Spring Cloud Config配置中心服务连接git
- 客户端需要配置配置信息从配置中心服务获取
- 当客户端启动,会从配置中心获取git上面的配置信息
Spring Cloud Config入门示例
1.Spring Cloud Config,默认使用GitHub 作为配置文件存储。除了 git 外,还可以用数据库、svn、本地文件等作为存储。而且使用的是http/https访问的形式。用我们自己的账号(如果没有去gitee.com去注册一个账号)在gitee上新建一个名为springcloud-config的Repostitory,如下图:
2.复制刚刚新建仓库的git地址
3.在本地新建git仓库
4.打开git的命令行操作(桌面右键选择Git Base Here),进入我们刚刚新建的git仓库的地址然后执行git clone https://gitee.com/DuanYuHong/springcloud-config.git命令
5.新建开发、生产、测试配置文件,并提交到github上,我这里只新建一个测试文件config-dev.yml进行提交,如下图:
提交成功后我们可以看到github上已经有这个文件了,如下图
6.集成Config分布式配置中心服务端
1.新建配置中心服务端模块: springcloud-config-center
2.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud</artifactId>
<groupId>com.dyh.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-config-center</artifactId>
<dependencies>
<!--spring-config服务端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
3.application.yml
server:
port: 3344
spring:
application:
name: springcloud-config-center #注册进Eureka服务器的服务名称
cloud:
config:
server:
git:
skip-ssl-validation: true # 跳过ssl认证
uri: https://gitee.com/DuanYuHong/springcloud-config.git #Gitee上复制的项目地址
search-paths: #搜索目录
- springcloud-config
label: master #读取分支
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
#单机版
defaultZone: http://localhost:7001/eureka/
4.主启动类 ConfigServerApplication
package com.dyh.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
/**
* @ClassName: ConfigServerApplication
* @author: dyh
* @since: 2021/12/26 18:25
*/
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
5.测试
配置中心的服务端就已经搭建好了,我们先启动eureka7001,然后再启动springcloud-config-center,先访问http://eureka7001.com:7001/,可以看到配置中心已经注册进行/eureka当中,如下图:
再访问 http://localhost:3344/master/config-dev.yml ,我们可以看到3344微服务可以成功的读取到远端的配置文件。如下图:
7.集成Config分布式配置中心客户端
1.新建配置中心客户端模块: springcloud-config-client
2.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud</artifactId>
<groupId>com.dyh.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-config-client</artifactId>
<dependencies>
<!--spring-config客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
3.然后我们新建配置文件bootstrap.yml
,这里为什么不用application.yml
呢???
application.yml
是用户级的资源配置项,而bootstrap.yml
是系统级的资源配置项,bootstrap.yml的优先级更高
,SpringCloud会创建一个"Bootstrap Context",作为Spring应用的“Application Context"的父上下文
。
初始化的时候,“Bootstrap Context"负责从外部源加载配置属性并解析配置,这两个上下文共享一个从外部获取的"Environment”。”Bootstrap“属性有高优先级,默认情况系,它们不会被本地配置覆盖。
"Bootstrap Context"和"Application Context"这两个上下文有不同的约定,所以新增一个bootstrap.yml
文件,保证这两个上下文的配置分离。bootstrap.yml配置文件如下:
server:
port: 3355
spring:
application:
name: springconfig-client
cloud:
#Config客户端配置
config:
label: master # 分支名称
name: config # 配置文件名称
profile: dev #读取后缀名称 上述3个综合:master分支上config-dev.yml的配置文件被读取http://localhost:3344/master/config-dev.yml
uri: http://localhost:3344 #配置中心地址
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
#单机版
defaultZone: http://localhost:7001/eureka/
4.主启动类 ConfigClientApplication
package com.dyh.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @ClassName: ConfigClientApplication
* @author: dyh
* @since: 2021/12/26 19:00
*/
@EnableEurekaClient
@SpringBootApplication
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
5.ConfigClientController
package com.dyh.springcloud.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @ClassName: ConfigClientController
* @author: dyh
* @since: 2021/12/26 19:01
*/
@RestController
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/config")
public String getConfigInfo() {
return configInfo;
}
}
6.测试
配置中心的搭建就完成了,接下来我们就测试客户端是否能够通过访问配置中心获取配置信息,先启动Eureka服务注册中心7001、Config服务配置中心服务端3344后,再启动Config服务配置中心客户端3355。访问http://localhost:3355/config。
我们可以通过客户端获取到远端的配置文件。如下图:
8.但是现在就有这样的问题,当我们在Gitee上修改配置文件内容,刷新配置中心服务端Server,发现服务端Server配置中心立刻响应并刷新了配置信息,但是,我们刷新客户端Client,发现没有任何响应,配置信息仍然是原来的配置信息。总不能每次远端修改了配置文件后,客户端都需要重启来进行对配置信息的重新加载对吧,针对这个问题,我们需要使用动态刷新。只需要在客户端Client,加上actuator监控,我们需要在客户端Client的pom文件中加入这个依赖,如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
9.修改客户端Client的bootstrap.yml配置文件,加入如下配置暴露监控断点:
# 暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
10.在业务类ConfigClientController上添加@RefreshScope注解使客户端服务具有刷新功能,如下:
package com.dyh.springcloud.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @ClassName: ConfigClientController
* @author: dyh
* @since: 2021/12/26 19:01
*/
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/config")
public String getConfigInfo() {
return configInfo;
}
}
11.发送POST请求刷新客户端Client该刷新请求必须发送后,客户端才能获得刷新后的信息,刷新客户端的请求必须是POST请求。cmd命令行执行如下语句:
curl -X POST "http://127.0.0.1:3355/actuator/refresh"
当出现以下信息时说明激活刷新客户端Client成功,再次访问,就可以得到刷新后的配置信息。
我们Spring Cloud Config到这里就学习完毕了,so easy!
但是假设如果我们有多个微服务客户端呢?难道每个微服务都需要执行一次POST请求进行手动刷新吗?我们可以通过广播的方式进行一次通知,处处生效,这里就要下一节要学习的知识消息总线——Spring Cloud Bus。
下一篇文章我们学习Spring Cloud Bus分布式配置中心,持续学习,持续更新,下一节更精彩!欢迎朋友们点赞关注!感谢!