spring cloud 三

微信图片_20221026132251

spring cloud

分布式配置中心

Config

当需要服务器特别特别多的时候,我们不能每次都配置yml,然后再重新启动,因为太多了,这是我们可以集中管理配置文件,根据不同环境不同配置,动态化的配置更新,当配置信息改变时,不需要重启即可更新配置信息到服务

config配置中心流程

config使用

配置config-server服务模块

  • pom
1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
  • application.yml
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

# spring cloud config
cloud:
config:
server:
# git 的 远程仓库地址
git:
uri: https://gitee.com/codexiaobo/config-server.git
label: master # 分支配置
  • app
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即可查看到远程的配置文件

其他模块读取配置文件

  • pom
1
2
3
4
5
<!--config client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
  • application.yml
1
2
3
4
5
6
7
8
9
10
11
# 配置config-server地址
# 配置获得配置文件的名称等信息
spring:
cloud:
config:
# 配置config-server地址
uri: http://localhost:9527
# 配置获得配置文件的名称等信息
name: provider # 文件名
profile: dev # profile指定
label: master # 分支

但是这样只是静态的读取,因为当远程仓库修改时,我们不能将几千个机器上的服务重启啊,这样运维人员就傻了,效率太低,所以我们要让他们动态的读取到远程仓库的配置文件

Config刷新读取远程配置文件

客户端引入

  • pom
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注册到注册中心

  • pom
1
2
3
4
5
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

从注册中心中读到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
# 配置config-server地址
# 配置获得配置文件的名称等信息
spring:
cloud:
config:
# 配置config-server地址
#uri: http://localhost:9527
# 配置获得配置文件的名称等信息
name: config # 文件名
profile: dev # profile指定, config-dev.yml
label: master # 分支
# 在注册中心中拿到config-server
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
<!-- bus -->
<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>
  • bootstrap.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
# 配置config-server地址
# 配置获得配置文件的名称等信息
spring:
cloud:
config:
# 配置config-server地址
# uri: http://localhost:9527
# 配置获得配置文件的名称等信息
name: provider # 文件名
profile: dev # profile指定, config-dev.yml
label: master # 分支
# 在注册中心中拿到config-server
discovery:
enabled: true
service-id: CONFIG-SERVER
#配置rabbitmq信息
rabbitmq:
host: localhost
port: 5672
username: xiaobo
password: xiaobo
virtual-host: /

# 暴露bus的刷新端点
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信息
rabbitmq:
host: localhost
port: 5672
username: xiaobo
password: xiaobo
virtual-host: /

# spring cloud config
cloud:
config:
server:
# git 的 远程仓库地址
git:
uri: https://gitee.com/codexiaobo/config-server.git
label: master # 分支配置

eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka

# 暴露bus的刷新端点
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 组件

stream组件

binding :通过配置把应用和spring cloud stream 的 binder 绑定在一起

output :发送消息 Channel,内置 Source接口

input :接收消息 Channel,内置 Sink接口

模拟

stream-provider模块

  • pom
1
2
3
4
5
6
7
8
9
10
11
12
<!--spring boot web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>


<!-- stream -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
  • application.yml
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: # 指定mq的环境
spring:
rabbitmq:
host: localhost
port: 5672
username: xiaobo
password: xiaobo
virtual-host: /
bindings:
output: # channel名称
binder: xbclass_binder #指定使用哪一个binder
destination: xblclass_exchange # 消息目的地
  • MessageProvider
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
// 声明这个类是stream的发送者
@EnableBinding(Source.class)
public class MessageProvider {

@Autowired
private MessageChannel output;

/**
* 发送消息至rabbitmq
* @param msg
*/
public void send(String msg){

// 发送消息
boolean send = output.send(MessageBuilder.withPayload(msg).build());

if (send) {
System.out.println("发送消息成功");
}else{
System.out.println("发送消息失败");
}
}
}
  • ProviderController
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";
}

}
  • StreamProviderApp
1
2
3
4
5
6
7
@SpringBootApplication
public class StreamProviderApp {

public static void main(String[] args) {
SpringApplication.run(StreamProviderApp.class, args);
}
}

stream-consumer模块

  • application.yml
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: # 指定mq的环境
spring:
rabbitmq:
host: localhost
port: 5672
username: xiaobo
password: xiaobo
virtual-host: /
bindings:
input: # channel名称
binder: xbclass_binder #指定使用哪一个binder
destination: xblclass_exchange # 消息目的地
  • StreamConsumer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* @author xiaobo
* @date 2022/10/26 - 11:20
*/
@Component
@EnableBinding(Sink.class)
public class StreamConsumer {

@StreamListener(Sink.INPUT)
public void listenerConsumer(Message message){

System.out.println(message);
System.out.println(message.getPayload());
}
}
  • App
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/

  • 运行
1
java -jar ***.jar
  • 服务提供方和消费方分别引入 sleuth 和 zipkin 依赖
1
2
3
4
5
6
7
8
9
10
<!-- 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>
  • 配置服务提供方和服务消费方
1
2
3
4
5
6
spring:
zipkin:
base-url: http://localhost:9411/ # 设置zipkin的服务端路径
sleuth:
sampler:
probability: 1 # 采集率 默认 0.1 百分之十。
  • 访问

http://localhost:9411/

正确的开始,微小的长进,然后持续,嘿,我是小博,带你一起看我目之所及的世界……

-------------本文结束 感谢您的阅读-------------

本文标题:spring cloud 三

文章作者:小博

发布时间:2022年10月26日 - 13:24

最后更新:2022年10月26日 - 13:29

原始链接:https://codexiaobo.github.io/posts/2002239265/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。