文章目录
6.Config 分布式配置中心
Spring Cloud Config 解决了在分布式场景下多环境配置文件的管理和维护
好处:
- 集中管理配置文件 (本地目录或云端github、gitee)
- 不同环境不同配置,动态化的配置更新
- 配置信息改变时,不需要重启即可更新配置信息到服务
快速入门
6.1 Config server 搭建
-
使用gitee创建远程仓库,上传配置文件config-dev.yml
-
@EnableConfigServer//启动ConfigServer功能
-
导入 config-server依赖
<!--config-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
4.编写配置文件,设置gitee远程仓库地址
server:
port: 9527
spring:
application:
name: config-server
#spring cloud config
cloud:
config:
server:
# git 的远程仓库地址
git:
uri: https://gitee.com/mo-shang-shang/li-config.git
# username: 私有仓库需要配置
# password:
label: master #分支配置
2.测试访问远程配置文件
http://localhost:9527/master/config-dev.yml
6.2 Config client 搭建
bootstrap.yml 通常用户系统配置,其优先级高于application.yml
1.导入starter-config 依赖
<!--config client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
2.配置config server 地址,读取配置文件名称等信息
# 配置config server服务端地址
spring:
cloud:
config:
uri: http://localhost:9527
# 获得配置文件的名称等信息
name: config
profile: dev #指定后会自动进行拼接为config-dev.yml
label: master #主分支
3.获取配置值
4.启动测试
6.3 Config 客户端刷新
由于当git上的配置文件修改时,服务端获取到的配置文件是实时更新的,但客户端不会更新,所以需要刷新。
1.在config客户端(消费方)引入actuator依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.获取配置信息类上,添加注解
@RestController
@RequestMapping("goods")
@RefreshScope//开启刷新功能
public class GoodsController
3.添加配置,监控端点,refresh,记不住可以用*,但可能不安全
management:
endpoints:
web:
exposure:
include: 'refresh'
4.使用curl工具发送post请求
http://localhost:8001/actuator/refresh
6.4 Config集成Eureka
config-server 配置
注册eureka
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka # eureka服务端地址,将来客户端使用该地址和eureka进行通信
config-client 配置
配置发现,不采用固定uri调用
# 配置config server服务端地址
spring:
cloud:
config:
#uri: http://localhost:9527
# 获得配置文件的名称等信息
name: config
profile: dev #指定后会自动进行拼接为config-dev.yml
label: master #主分支
discovery:
enabled: true
service-id: CONFIG-SERVER
7.Bus 消息总线
Spring Cloud Bus 是用轻量的消息中间件将分布式的节点连接起来,可以用于广播配置文件的更改或者服务的监控管理。关键的思想就是,消息总线可以为微服务做监控,也可以实现应用程序直接相通信。
Spring Cloud Bus 可选的消息中间件包括 RabbitMQ 和 Kafka。
由于config配置中心每次刷新只能刷新一个微服务,所以当微服务数量特别多无法解决,此时就可以用bus。
config-server 配置
·1. 导入bus的依赖
<!--bus-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
- 配置rabbitmq
spring:
#配置rabbitmq
rabbitmq:
host: 192.168.25.102
port: 5672
username: root
password: xxx
virtual-host: /
3.暴露bus的刷新端点
#暴露bus的刷新端点
management:
endpoints:
web:
exposure:
include: 'bus-refresh'
4.客户端只需要导入bus的坐标和rabbitmq的配置
5.通过bus用Post请求统一刷新
http://localhost:9527/actuator/bus-refresh
8.Stream 消息驱动
Spring Cloud Stream 是一个构建消息驱动微服务应用的框架。
Stream解决了开发人员无感知的使用消息中间件的问题,因为Stream对消息中间件的进一步封装,可以做到代码层面对中间件的无感知,甚至与动态的切换中间件,使得微服务开发的高度解耦,服务可以关注更多自己的业务流程。
Spring Cloud Stream 目前只支持两种消息中间件RabbitMQ和Kafka
Spring Cloud Stream 构建的应用程序与消息中间件之间是通过绑定器Binder相关联的。绑定器对应应用程序而言起到了隔离作用,它使得不同消息中间件的实现细节对应用程序来说是透明的。
binging 是我们通过配置把应用和spring cloud stream 的 binder 绑定在一起
output:发送消息Channel,内置Source接口
input:接收消息Channel,内置Sink接口
8.1 Stream消息生产者
1.创建消息生产者模块,引入依赖 starter-stream-rabbit
<!--stream-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
2.编写配置,定义binder,和bingings
spring:
cloud:
stream:
# 定义绑定器,绑定到那个消息中间件上
binders:
li_binder: #自定义绑定器的名称
type: rabbit #定义绑定器类型
environment: #指定mq环境
spring:
rabbitmq:
host: 192.168.25.102
port: 5672
username: root
password: xxxx
virtual-host: /
#让应用和该绑定器绑定
bindings:
output: # channel名称
binder: li_binder #指定使用哪一个binder
destination: li_cxchange #消息目的地
3.定义消息发送业务类。添加**@EnableBinding(Source.class),注入MessageChannel output**,完成消息发送。
@Component
@EnableBinding(Source.class)
public class MessageProducer {
@Autowired
private MessageChannel output;
public void send() {
String message = "hello stream";
//发送消息
output.send(MessageBuilder.withPayload(message).build());
System.out.println("消息发送成功");
}
}
4.编写启动类,测试。
8.2 Stream消息消费者
1.创建消息消费者模块,引入依赖starter-stream-rabbit
2.编写配置,定义binder,和bingings,(绑定键都相同,就管道(信道)不同)
spring:
cloud:
stream:
# 定义绑定器,绑定到那个消息中间件上
binders:
li_binder: #自定义绑定器的名称
type: rabbit #定义绑定器类型
environment: #指定mq环境
spring:
rabbitmq:
host: 192.168.25.102
port: 5672
username: root
password: xxx
virtual-host: /
#让应用和该绑定器绑定
bindings:
input: # channel名称
binder: li_binder #指定使用哪一个binder
destination: li_cxchange #消息目的地
3.定义消息接收业务类。添加@EnableBinding(Sink.class),使用@StreamListener(Sink.INPUT),完成消息接收。
@Component
@EnableBinding({Sink.class})
public class MessageListener {
@StreamListener(Sink.INPUT)
public void receive(Message message){
System.out.println(message);
System.out.println(message.getPayload());
}
}
4.编写启动类,测试
9. Sleuth+Zipkin 链路追踪
Spring Cloud Sleuth 其实是一个工具,它在整个分布式系统中能跟踪一个用户请求的过程,捕获这些跟踪数据,就能构建微服务的整个调用链的视图,这是调用和监控微服务的关键工具。
- 耗时分析
- 可视化错误
- 链路优化
Zipkin是一个Twitter的一个开源项目,它致力于收集服务的定时数据,以解决微服务架构中的延迟问题,包括数据的收集、存储、查找和展现。
1.安装启动zipkin。java -jar zipkin.jar
2.访问zipkin web界面。http://localhost:9411/
3.在服务提供方和消费方分别引入 sleuth 和 zipkin 依赖(zipkin中已经依赖了sleuth,所以也可以不导)
<!--sleuth-zipkin-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
4.分别配置服务提供方和消费方
spring:
zipkin:
base-url: http://localhost:9411/ #设置zipkin服务端地址
sleuth:
sampler:
probability: 1 #数据采集率 默认0.1 百分之十