spring cloud
分布式配置中心
Config
当需要服务器特别特别多的时候,我们不能每次都配置yml,然后再重新启动,因为太多了,这是我们可以集中管理配置文件,根据不同环境不同配置,动态化的配置更新,当配置信息改变时,不需要重启即可更新配置信息到服务
config使用
配置config-server服务模块
1 2 3 4
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| server: port: 9527
spring: application: name: config-server
cloud: config: server: git: uri: https://gitee.com/codexiaobo/config-server.git label: master
|
1 2 3 4 5 6 7 8 9
| @SpringBootApplication @EnableConfigServer public class ConfigServerApp {
public static void main(String[] args) { SpringApplication.run(ConfigServerApp.class, args); }
}
|
http://localhost:9527/master/provider-dev.yml
即可查看到远程的配置文件
其他模块读取配置文件
1 2 3 4 5
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
|
1 2 3 4 5 6 7 8 9 10 11
|
spring: cloud: config: uri: http://localhost:9527 name: provider profile: dev label: master
|
但是这样只是静态的读取,因为当远程仓库修改时,我们不能将几千个机器上的服务重启啊,这样运维人员就傻了,效率太低,所以我们要让他们动态的读取到远程仓库的配置文件
Config刷新读取远程配置文件
客户端引入
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
|
controller使用 @RefreshScope
bootstrap.yml
添加
1 2 3 4 5
| management: endpoints: web: exposure: include: '*'
|
cmd 中发送请求 刷新 curl -X POST http://localhost:8080/actuator/refresh
这里我们只是配置了一个config-server,如果挂了,就发生了单点故障了,所以我们不能写死,要从注册中心中拿到config-server
Config 集成
将config-server注册到注册中心
1 2 3 4 5
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
|
从注册中心中读到config-server
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
spring: cloud: config: name: config profile: dev label: master
discovery: enabled: true service-id: CONFIG-SERVER
management: endpoints: web: exposure: include: '*'
|
SpringCloud Bus消息总线
远程的配置文件更新了,运维只需要发一个请求,所有用到这个配置文件的几百个应用更新了
消息总线可以为微服务做监控,也可以实现应用程序之间相通信
原理
就是发送一个请求,然后运用rabbitmq的订阅发布模式通知多份配置文件更新
模拟
- config-server和config-client
1 2 3 4 5 6 7 8 9
| <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>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
spring: cloud: config:
name: provider profile: dev label: master
discovery: enabled: true service-id: CONFIG-SERVER rabbitmq: host: localhost port: 5672 username: xiaobo password: xiaobo virtual-host: /
management: endpoints: web: exposure: include: 'bus-refresh'
|
- config-server模块application.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| server: port: 9527
spring: application: name: config-server rabbitmq: host: localhost port: 5672 username: xiaobo password: xiaobo virtual-host: /
cloud: config: server: git: uri: https://gitee.com/codexiaobo/config-server.git label: master
eureka: client: service-url: defaultZone: http://localhost:8761/eureka
management: endpoints: web: exposure: include: 'bus-refresh'
|
curl -X POST http://localhost:9527/actuator/bus-refresh
模拟发送请求修改
SpringCloud Stream消息驱动
Spring Cloud Stream 是一个构建消息驱动微服务应用的框架
Stream对消息中间件的进一步封装,可以做到代码层面对中间件的无感知,甚至于动态的切换中间件,就类似JDBC,使得微服务开发的高度解耦
Spring Cloud Stream目前支持两种消息中间件RabbitMQ和Kafka
stream 组件
binding
:通过配置把应用和spring cloud stream 的 binder 绑定在一起
output
:发送消息 Channel,内置 Source接口
input
:接收消息 Channel,内置 Sink接口
模拟
stream-provider模块
1 2 3 4 5 6 7 8 9 10 11 12
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-stream-rabbit</artifactId> </dependency>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| server: port: 8000 spring: cloud: stream: binders: xbclass_binder: type: rabbit environment: spring: rabbitmq: host: localhost port: 5672 username: xiaobo password: xiaobo virtual-host: / bindings: output: binder: xbclass_binder destination: xblclass_exchange
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| @Component
@EnableBinding(Source.class) public class MessageProvider {
@Autowired private MessageChannel output;
public void send(String msg){
boolean send = output.send(MessageBuilder.withPayload(msg).build());
if (send) { System.out.println("发送消息成功"); }else{ System.out.println("发送消息失败"); } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @RestController @RequestMapping(value = "/stream") public class ProviderController {
@Autowired private MessageProvider messageProvider;
@GetMapping(value = "/send") public String send(){
messageProvider.send("我发送一条消息");
return "success"; }
}
|
1 2 3 4 5 6 7
| @SpringBootApplication public class StreamProviderApp {
public static void main(String[] args) { SpringApplication.run(StreamProviderApp.class, args); } }
|
stream-consumer模块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| server: port: 9000 spring: cloud: stream: binders: xbclass_binder: type: rabbit environment: spring: rabbitmq: host: localhost port: 5672 username: xiaobo password: xiaobo virtual-host: / bindings: input: binder: xbclass_binder destination: xblclass_exchange
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
@Component @EnableBinding(Sink.class) public class StreamConsumer {
@StreamListener(Sink.INPUT) public void listenerConsumer(Message message){
System.out.println(message); System.out.println(message.getPayload()); } }
|
1 2 3 4 5 6 7
| @SpringBootApplication public class StreamConsumerApp {
public static void main(String[] args) { SpringApplication.run(StreamConsumerApp.class, args); } }
|
SpringCloud Sleuth分布式请求链路追踪
一个工具,在整个分布式系统中能跟踪一个用户请求的过程,捕获这些跟踪数据,构建微服务的整个调用链的视图
下载zipkin.jar
点击下载
https://repo1.maven.org/maven2/io/zipkin/zipkin-server/
- 服务提供方和消费方分别引入 sleuth 和 zipkin 依赖
1 2 3 4 5 6 7 8 9 10
|
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency>
|
1 2 3 4 5 6
| spring: zipkin: base-url: http: sleuth: sampler: probability: 1 # 采集率 默认 0.1 百分之十。
|
http://localhost:9411/
正确的开始,微小的长进,然后持续,嘿,我是小博,带你一起看我目之所及的世界……