SpringCloud & SpringCloud Alibaba(2)
前言:
在信息技术的浩瀚海洋中,技术文档不仅是知识的载体,更是每个人挑战技术难题的智慧结晶。
本人不才,文档编写水平和代码能力有限,但是每份文档经过精益求精和明察秋毫过程的编写,让每个读者通俗易懂。
尽管有时困惑、疲惫,但每当看到文档的浏览量扶摇直上,那份成就感便油然而生。
你们的关注和点赞是我持续更新的动力!
感谢大家的支持!
1. Config
1.1 简介
Config组件称为统一配置中心,它的核心作用是将配置进行统一管理。
1)作用与意义
- 便于在大规模集群部署服务应用时,确保相同的服务配置一致。
- 修改配置时,只需统一修改,全部服务会同步更新,无需手动逐个服务维护。
2)组成
-
Config Server(配置服务器):作为独立的微服务应用,用于连接配置仓库(如Git、SVN等)并为客户端提供配置信息的访问接口。它支持横向扩展,集中管理应用程序各个环境下的配置。
-
Config Client(配置客户端):指的是微服务架构中的各个微服务,它们通过Config Server 对配置进行管理,并在启动时从 Config Server 获取和加载配置信息。
3)特点与优势
-
集中式管理:将配置文件集中存储在一个外部的存储仓库或系统中,方便统一管理。
-
动态化配置更新:服务在运行期间可动态调整配置,无需重启服务即可感知配置变化并应用新配置。
-
多环境支持:可支持不同环境(如开发/测试/生产等)的配置管理。
-
版本管理:默认使用Git存储配置信息,支持对微服务配置的版本管理。
4)与其他配置中心的比较
- 虽然 Spring Cloud Config 是 Spring Cloud 家族中较早的配置中心,但当前已有其他更为强大的开源配置中心,如携程的 Apollo、蚂蚁金服的 Disconf 等,这些配置中心功能更加丰富和强大。
5)应用场景
- 特别适用于微服务架构中,需要统一管理、动态更新大量服务配置的场景。
1.2 Git 与 Config 的联系
config client —>config server----> 远端git仓库
1)选择—个远端 Git 仓库
- 使用 github gitee
- 创建新仓库
- 获取仓库地址 https://gitee.com/xxxxda1/config.git(可以尝试自己创建Git账户,之后会更新Git使用的)
2)搭建config server统—配置中心服务
- 独立 springboot 应用
- 引入 config server 依赖这是服务端的依赖,启动类还需要添加注解,客户端的依赖不一样
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</ artifactId>
</dependency>
- 配置 application.properties 远程仓库地址(以公有的为例)
server.port=8848 # Git使用客户端的时候端口不能再用8848
spring.application.name=CONFIGSERVER
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.config.server.git.uri=https://gitee.com/chenyn-java_admin/configs.git
spring.cloud.config.server.git.default-label=master
#如果库是私有的 还要添加
#spring.cloud.config.server.git.username=
#spring.cloud.config.server.git.password=
- 开启统─配置中心,在入口类加入注解
@springBootApplication
EnableDiscoveryclient //可加可不加
@EnableConfigserverl//代表我是统—配置中心服务
public class ConfigApplication(..........)
1.3 测试
现在在 demo 类中写入一个变量 name。
@Value("${name}")
public String name;
因为在生产和测试环境下,name的值是不一样的。所以在增添两个配置类 dev 和 prod 分别写入 name = wxz 和 lly。由于使用的是 Git 仓库就要把这些信息写在仓库里 而不能写在配置类中。
1.3.1 创建─个独立 SpringBoot 应用
根据上一篇文档的介绍,有详细过程。
1.3.2 将自身配置交给远端 Git 仓库管理
每一个文件中写入配置类的信息,但是注册中心和指定环境配置不用写。
1.3.3 引入 Config Client 相关依赖
这是客户端的依赖,服务端的不一样!
<!--引入config client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</ artifactId>
</dependency>
1.3.4 创建一个配置类
这个配置类主要是告诉 Config Server 的地址。
#告诉当前configclient统一配置中心在注册中心服务id
spring.cloud.config.discovery.service-id=CONFIGSERVER
#开启当前configclient根据服务id去注册中心获取
spring.cloud.config.discovery.enabled=true
#配置注册中心
spring.cloud.consu1.host=localhost
spring.cloud.consul.port=8500
#获取那个配置文件1.确定分支2.确定文件名3.确定环境
spring.cloud.config.label=master
spring.cloud.config,name=configclient
spring.cloud.config.profile=prod
1.3.5 启动 Config Client
不然默认网址端口是8888。
2. SpringCloud Alibaba——Nacos的使用
2.1 SpringCloud Alibaba简介
1)SpringCloud
Spring 团队开源微服务工具集帮助我们快速构建分布式系统(微服务系统)提供 Spring 组织 Netflix 开源解决方案。
2)SpringCloud Alibaba
Alibaba 团队开源微服务工具集帮助我们快速构建分布式系统(微服务系统)提供 Alibaba 解决方案。
3)SpringCloud Alibaba
最新版本: xxxxx.RELEASE
官方定义:SpringClond Alibaba provides a one-stop solution(解决方案) for distributed application development(分布式应用开发) 。
Spring Cloud 阿里巴巴为分布式应用开发了提供—站式解决方案 。
包含开发分布式应用的所有组件。
4)SpringCloud Alibaba 特性,特点,提供组件
- Flow control and service degradation 服务流量控制和服务降级(熔断) ==> Sentinel——替换SpringCloud原有Hystrix组件
- service registration and discovery 服务注册和发现组件 ==> Hacos—— 替换SpringClond原有Consul和Eureka组件
- Distributed Configuration 统—配置中心组件 ==> Hacos—— 替换SpringCloud Config组件 自动配置刷新</