SpringCloud学习(七)—— Spring Cloud Config 分布式配置中心

温故而知新,可以为师矣

本文为拉钩教育java高薪培训班学习笔记与总结

1 分布式配置中心应用场景

往往,我们使⽤配置⽂件管理⼀些配置信息,⽐如 application.yml
单体应⽤架构,配置信息的管理、维护并不会显得特别麻烦,⼿动操作就可以,因为就⼀个⼯程;
微服务架构,因为我们的分布式集群环境中可能有很多个微服务,我们不可能⼀个⼀个去修改配置然后重启⽣效,在⼀定场景下我们还需要在运⾏期间动态调整配置信息,⽐如:根据各个微服务的负载情况,动态调整数据源连接池⼤⼩,我们希望配置内容发⽣变化的时候,微服务可以⾃动更新。
场景总结如下:
1)集中配置管理,⼀个微服务架构中可能有成百上千个微服务,所以集中配置管理是很重要的(⼀次修改、到处⽣效)
2 )不同环境不同配置,⽐如数据源配置在不同环境(开发 dev, 测试 test, ⽣产 prod)中是不同的
3)运⾏期间可动态调整。例如,可根据各个微服务的负载情况,动态调整数据源连接池⼤⼩等配置修改后可⾃动更新
4)如配置内容发⽣变化,微服务可以⾃动更新配置
那么,我们就需要对配置⽂件进⾏ 集中式管理 ,这也是分布式配置中⼼的作⽤。
 

2 Spring Cloud Confifig

2.1 Confifig简介

Spring Cloud Confifig 是⼀个分布式配置管理⽅案,包含了 Server 端和 Client端两个部分。
  • Server 端:提供配置⽂件的存储、以接⼝的形式将配置⽂件的内容提供出去,通过使⽤@EnableConfifigServer注解在 Spring boot 应⽤中⾮常简单的嵌⼊
  • Client 端:通过接⼝获取配置数据并初始化⾃⼰的应⽤

2.2 Confifig分布式配置应用

说明: Confifig Server 是集中式的配置服务,⽤于集中管理应⽤程序各个环境下的配 置。 默认使⽤ Git 存储配置⽂件内容,也可以 SVN
⽐如,我们要对 简历微服务 application.yml进⾏管理(区分开发环境、测试环境、⽣产环境)
1 )登录码云,创建项⽬ lagou-confifig-repo
2 )上传 yml 配置⽂件,命名规则如下: {application}-{profifile}.yml 或者 {application}-{profifile}.properties
其中, application 为应⽤名称, profifile指的是环境(⽤于区分开发环境,测试环境、⽣产环境等)
示例: lagou-service-resume-dev.yml lagou-service-resume-test.yml 、lagou-service-resume-prod.yml
3 构建 Confifig Server 统⼀配置中心
新建SpringBoot⼯程,引⼊依赖坐标(需要注册⾃⼰到Eureka
 
<?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>lagou-parent</artifactId>
 <groupId>com.lagou.edu</groupId>
 <version>1.0-SNAPSHOT</version>
 </parent>
 <modelVersion>4.0.0</modelVersion>
 <artifactId>lagou-config1</artifactId>
 <dependencies>
 <!--eureka client 客户端依赖引⼊-->
 <dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-netflix-eurekaclient</artifactId>
 </dependency>
 <!--config配置中⼼服务端-->
 <dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-config-server</artifactId>
 </dependency>
 </dependencies>
</project>
配置启动类,使⽤注解 @EnableConfifigServer 开启配置中⼼服务器功能
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer // 开启配置服务器功能
public class ConfigApp9006 {
 public static void main(String[] args) {
 SpringApplication.run(ConfigApp9003.class,args);
 }
}
application.yml 配置
server:
 port: 9006
#注册到Eureka服务中⼼
eureka:
 client:
 service-url:
 # 注册到集群,就把多个Eurekaserver地址使⽤逗号连接起来即可;注册到单实例(⾮集群模式),那就写⼀个就ok
 defaultZone:
http://LagouCloudEurekaServerA:8761/eureka,http://LagouCloudEurekaS
erverB:8762/eureka
 instance:
 prefer-ip-address: true #服务实例中显示ip,⽽不是显示主机名(兼容⽼的eureka版本)
 # 实例名称: 192.168.1.103:lagou-service-resume:8080,我们可以⾃定义它
 instance-id: ${spring.cloud.client.ipaddress}:${spring.application.name}:${server.port}:@project.version@
spring:
 application:
 name: lagou-service-autodeliver
 cloud:
 config:
 server:
 git:
 uri: https://github.com/5173098004/lagou-config-repo.git
#配置git服务地址
 username: 517309804@qq.com #配置git⽤户名
 password: yingdian12341 #配置git密码
 search-paths:
 - lagou-config-repo
 # 读取分⽀
 label: master
#针对的被调⽤⽅微服务名称,不加就是全局⽣效
#lagou-service-resume:
# ribbon:
# NFLoadBalancerRuleClassName:
com.netflix.loadbalancer.RoundRobinRule #负载策略调整
# springboot中暴露健康检查等断点接⼝
management:
 endpoints:
 web:
 exposure:
 include: "*"
 # 暴露健康接⼝的细节
 endpoint:
 health:
 show-details: always
测试访问: http://localhost:9006/master/lagou-service-resume-dev.yml ,查看到 配置⽂件内容
 
4 构建 Client 客户端(在已有简历微服务基础上)
已有⼯程中添加依赖坐标
<dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-config-client</artifactId>
</dependency>
application.yml 修改为 bootstrap.yml 配置⽂件
bootstrap.yml 是系统级别的,优先级⽐application.yml⾼,应⽤启动时会检查这个配置⽂件,在这个配置⽂件中指定配置中⼼的服务地址,会⾃动拉取所有应⽤配置并且启⽤。
(主要是把与统⼀配置中⼼连接的配置信息放到 bootstrap.yml
注意:需要统⼀读取的配置信息,从集中配置中心获取
bootstrap.yml
server:
 port: 8080
spring:
 application:
 name: lagou-service-resume
 datasource:
 driver-class-name: com.mysql.jdbc.Driver
 url: jdbc:mysql://localhost:3306/lagou?useUnicode=true&characterEncoding=utf8
 username: root
 password: 123456
 jpa:
 database: MySQL
 show-sql: true
 hibernate:
 naming:
 physical-strategy:org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
#避免将驼峰命名转换为下划线命名
 cloud:
 # config客户端配置,和ConfigServer通信,并告知ConfigServer希望获取的配置信息在哪个⽂件中
 config:
 name: lagou-service-resume #配置⽂件名称
 profile: dev #后缀名称
 label: master #分⽀名称
 uri: http://localhost:9006 #ConfigServer配置中⼼地址
#注册到Eureka服务中⼼
eureka:
 client:
 service-url:
 # 注册到集群,就把多个Eurekaserver地址使⽤逗号连接起来即可;注册到单实例(⾮集群模式),那就写⼀个就ok
defaultZone:http://LagouCloudEurekaServerA:8761/eureka,http://LagouCloudEurekaServerB:8762/eureka
 instance:
 prefer-ip-address: true #服务实例中显示ip,⽽不是显示主机名(兼容⽼的eureka版本)
 # 实例名称: 192.168.1.103:lagou-service-resume:8080,我们可以⾃定义它
 instance-id: ${spring.cloud.client.ipaddress}:${spring.application.name}:${server.port}:@project.version@
 # ⾃定义Eureka元数据
 metadata-map:
 cluster: cl1
 region: rn1
management:
 endpoints:
 web:
 exposure:
 include: "*"

2.3 Confifig配置⼿动刷新

不⽤重启微服务,只需要⼿动的做⼀些其他的操作(访问⼀个地址 /refresh)刷新,之后再访问即可
此时,客户端取到了配置中⼼的值,但当我们修改 GitHub上⾯的值时,服务端( Confifig Server )能实时获取最新的值,但客户端( Confifig Client)读的是缓存,⽆法实时获取最新值。 Spring Cloud已 经为我们解决了这个问题,那就是客户端使 ⽤ post 去触发 refresh ,获取最新数据。
1 Client 客户端添加依赖 springboot-starter-actuator (已添加)
2 Client 客户端 bootstrap.yml 中添加配置(暴露通信端点)
management:
 endpoints:
 web:
 exposure:
 include: refresh
 
也可以暴露所有的端⼝
management:
 endpoints:
 web:
 exposure:
 include: "*"

 

3 Client 客户端使⽤到配置信息的类上添加 @RefreshScope
4 )⼿动向 Client 客户端发起 POST 请求, http://localhost:8080/actuator/refresh ,刷新配置信息
注意:⼿动刷新⽅式避免了服务重启(流程: Git 改配置 —>for 循环脚本⼿动刷新每 个微服务)

2.4 Confifig配置自动更新

实现⼀次通知处处⽣效
拉勾内部做分布式配置,⽤的是 zk (存储 +通知),zk中数据变更,可以通知各个监听的客户端,客户端收到通知之后可以做出相应的操作(内存级别的数据直接⽣效,对于数据库连接信息、连接池等信息变化更新的,那么会在通知逻辑中进⾏处理,⽐如重新初始化连接池)
在微服务架构中,我们可以结合消息总线( Bus)实现分布式配置的⾃动更新 ( Spring Cloud Confifig+Spring Cloud Bus
 

2.4.1 消息总线Bus

所谓消息总线 Bus ,即我们经常会使⽤ MQ 消息代理构建⼀个共⽤的 Topic,通过这个Topic 连接各个微服务实例, MQ⼴播的消息会被所有在注册中⼼的微服务实例监听和消费。 换⾔之就是通过⼀个主题连接各个微服务,打通脉络
Spring Cloud Bus (基于 MQ 的,⽀持 RabbitMq/Kafka ) 是 Spring Cloud中的消息总线⽅案, Spring Cloud Confifig + Spring Cloud Bus 结合可以实现配置信息的⾃动更新。
 

2.4.2 Spring Cloud Confifig+Spring Cloud Bus 实现自动刷新

MQ 消息代理,我们还选择使⽤ RabbitMQ ConfifigServer ConfifigClient都添加都消息总线的⽀持以及与 RabbitMq 的连接信息
1 Confifig Server 服务端添加消息总线⽀持
<dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

2ConfifigServer添加配置

spring:
 rabbitmq:
 host: 127.0.0.1
 port: 5672
 username: guest
 password: guest

3)微服务暴露端⼝

management:
 endpoints:
 web:
 exposure:
 include: bus-refresh
 
建议暴露所有的端⼝
management:
 endpoints:
 web:
 exposure:
 include: "*"

 

4 )重启各个服务,更改配置之后,向配置中⼼服务端发送 post 请求
 
http://localhost:9003/actuator/bus-refresh ,各个客户端配置即可⾃动刷新 在⼴播模式下实现了⼀次请求,处处更新,如果我只想定向更新呢?
在发起刷新请求的时候 http://localhost:9006/actuator/bus-refresh/lagou-service-resume:8081
即为最后⾯跟上要定向刷新的实例的 服务名 : 端⼝号 即可
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值