目录
Config 分布式配置中心
Config 概述
- Spring Cloud Config 解决了在分布式场景下多环境配置文件的管理和维护。
- 好处:
- 集中管理配置文件
- 不同环境不同配置,动态化的配置更新
- 配置信息改变时,不需要重启即可更新配置信息到服务
外部配置文件,放着A、B、C等服务的配置文件,其放的位置,可能是在本地目录或者云端(如gitee)
Config 快速入门
config server:
- 使用gitee创建远程仓库,上传配置文件
- 搭建 config server 模块
- 导入 config-server 依赖
- 编写配置,设置 gitee 远程仓库地址
- 测试访问远程配置文件
config client:
- 导入 starter-config 依赖
- 配置config server 地址,读取配置文件名称等信息
- 获取配置值
- 启动测试
Config 客户端刷新
外部配置文件发生了变更,Config Server不需要更改,可以直接访问获取,但A、B和C等服务客服端想获取最新的配置信息,必须要配置。
- 在 config 客户端引入 actuator 依赖
- 获取配置信息类上,添加 @RefreshScope 注解
- 添加配置
management:
endpoints:
web:
exposure:
include: refresh
- 使用curl工具发送post请求 在dos窗口中输入命令
curl -X POST http://localhost:8001/actuator/refresh
Config 集成Eureka
之前 A、B和C获取Config Server地址静态的,若Config Server地址发生变更,那么就要一个一个修改访问Config Server地址,麻烦!可以有一个简单的方法,就是把Config Server注册到Eureka上,如上所示。
实验十
项目结构
config-server模块
1、pom.xml引入config-server依赖
<dependencies>
<!-- config-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
2、application.yml 文件
server:
port: 9527
spring:
application:
name: config-server
# spring cloud config
cloud:
config:
server:
# git 的 远程仓库地址
git:
uri: https://gitee.com/itheima_cch/itheima-configs.git
label: master # 分支配置
# 将自己注册到ureka中
ureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
2、启动ConfigServerApp
@SpringBootApplication
@EnableConfigServer // 启用config server功能
@EnableEurekaClient
public class ConfigServerApp {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApp.class,args);
}
}
eureka-server-config模块
和实验九eureka-server-gateway模块一样
config-provider模块
1.pom.xml引入依赖
<dependencies>
<!--spring boot web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--config client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
2、application.yml和bootstrap.yml文件
server:
port: 8002
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
spring:
application:
name: config-provider
# 配置config-server地址
# 配置获得配置文件的名称等信息
spring:
cloud:
config:
#####下列两种方法获取config-server地址#####
# 配置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: '*'
3、GoodsController类 @RefreshScope
@RestController
@RequestMapping("/goods")
@RefreshScope // 开启刷新功能
public class GoodsController {
@Autowired
private GoodsService goodsService;
@Value("${server.port}")
private int port;
@Value("${itheima}")
private String itheima;
/**
* 降级:
* 1. 出现异常
* 2. 服务调用超时
* * 默认1s超时
*
* @HystrixCommand(fallbackMethod = "findOne_fallback")
* fallbackMethod:指定降级后调用的方法名称
*/
@GetMapping("/findOne/{id}")
@HystrixCommand(fallbackMethod = "findOne_fallback",commandProperties = {
//设置Hystrix的超时时间,默认1s
@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000"),
//监控时间 默认5000 毫秒
@HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value = "5000"),
//失败次数。默认20次
@HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value = "20"),
//失败率 默认50%
@HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value = "50")
})
public Goods findOne(@PathVariable("id") int id){
//如果id == 1 ,则出现异常,id != 1 则正常访问
if(id == 1){
//1.造个异常
int i = 3/0;
}
/*try {
//2. 休眠2秒
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
Goods goods = goodsService.findOne(id);
goods.setTitle(goods.getTitle() + ":" + port+":"+itheima);//将端口号,设置到了 商品标题上
return goods;
}
/**
* 定义降级方法:
* 1. 方法的返回值需要和原方法一样
* 2. 方法的参数需要和原方法一样
*/
public Goods findOne_fallback(int id){
Goods goods = new Goods();
goods.setTitle("降级了~~~");
return goods;
}
}
4、Goods、GoodsService和GoodsDao
和实验一 一样
5、ProviderApp启动类
@EnableEurekaClient //该注解 在新版本中可以省略
@SpringBootApplication
@EnableCircuitBreaker // 开启Hystrix功能
public class ProviderApp {
public static void main(String[] args) {
SpringApplication.run(ProviderApp.class,args);
}
}
config-consumer 模块
1、pom.xml文件
<dependencies>
<!--spring boot web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
2、application.yml 文件
server:
port: 9000
eureka:
instance:
hostname: localhost # 主机名
client:
service-url:
defaultZone: http://localhost:8761/eureka
spring:
application:
name: config-consumer
# 开启feign对hystrix的支持
feign:
hystrix:
enabled: true
3、GoodsFeignClient类
@FeignClient(value = "CONFIG-PROVIDER",fallback = GoodsFeignClientFallback.class)
public interface GoodsFeignClient {
@GetMapping("/goods/findOne/{id}")
public Goods findGoodsById(@PathVariable("id") int id);
}
其他和实验七 hystrix-consumer 模块 一样
运行结果:
Bus 消息总线
Bus 概述
- Spring Cloud Bus 是用轻量的消息中间件将分布式的节点连接起来,可以用于广播配置文件的更改或者服务的监控管理。关键的思想就是,消息总线可以为微服务做监控,也可以实现应用程序之间相通信。
- Spring Cloud Bus 可选的消息中间件包括 RabbitMQ 和 Kafka 。
运维人员输入:/actuator/bus-refresh命令,Config Server会发出消息给Bus,接着Bus会广播配置文件的更改。
curl -X POST http://localhost:9527/actuator/refresh
RabbitMQ 回顾
A服务将消息给消息队列,B服务从消息队列中拿消息。
RabbitMQ的结构
RabbitMQ的工作模式
RabbitMQ 提供了 6 种工作模式:简单模式、work queues、Publish/Subscribe 发布与订阅模式、Routing 路由模式、Topics 主题模式、RPC 远程调用模式(远程调用,不太算 MQ;暂不作介绍)。
RabbitMQ Windows安装
详细看资料
Bus 快速入门
bus组件不需要你创建任何模块,它就是RabbitMQ代码的封装。config-server要发消息,config-client要收消息。
- 分别在 config-server 和 config-client中引入bus依赖:bus-amqp
- 分别在 config-server 和 config-client中配置 RabbitMQ
- 在config-server中设置暴露监控断点:bus-refresh
- 启动测试
实验十一
项目结构
config-server-1模块
1、pom.xml 引入相关依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- config-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- bus -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
</dependencies>
2、application.yml文件
server:
port: 9527
spring:
application:
name: config-server
# spring cloud config
cloud:
config:
server:
# git 的 远程仓库地址
git:
uri: https://gitee.com/itheima_cch/itheima-configs.git
label: master # 分支配置
#配置rabbitmq信息
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
virtual-host: /
# 将自己注册到eureka中
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
# 暴露bus的刷新端点
management:
endpoints:
web:
exposure:
include: 'bus-refresh'
3、ConfigServerApp启动类
@SpringBootApplication
@EnableConfigServer // 启用config server功能
@EnableEurekaClient
public class ConfigServerApp {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApp.class,args);
}
}
eureka-server-config-1模块
和实验九 eureka-server-gateway模块 一样
config-provider-1模块
1、pom.xml模块
<dependencies>
<!--spring boot web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--config client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- bus -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
2、application.yml和bootstrap.yml文件
server:
port: 8002
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
spring:
application:
name: config-provider
# 配置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
#配置rabbitmq信息
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
virtual-host: /
management:
endpoints:
web:
exposure:
include: '*'
3、GoodsDao、GoodsService、Goods、GoodsController和ProviderApp
看实验十模块
config-consumer-1模块
1、pom.xml文件依赖
<dependencies>
<!--spring boot web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--config client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- bus -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
</dependencies>
2.application.yml 文件、bootstrap.yml文件
server:
port: 9001
eureka:
instance:
hostname: localhost # 主机名
client:
service-url:
defaultZone: http://localhost:8761/eureka
spring:
application:
name: config-consumer
# 开启feign对hystrix的支持
feign:
hystrix:
enabled: true
# 配置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
#配置rabbitmq信息
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
virtual-host: /
management:
endpoints:
web:
exposure:
include: '*'
3、其余模块看实验十
运行结果
Stream 消息驱动
Stream 概述
- Spring Cloud Stream 是一个构建消息驱动微服务应用的框架。如左图
- Stream 解决了开发人员无感知的使用消息中间件的问题,因为Stream对消息中间件的进一步封装,可以做到代码层面对中间件的无感知,甚至于动态的切换中间件,使得微服务开发的高度解耦,服务可以关注更多自己的业务流程。类似于右图
- Spring Cloud Stream目前支持两种消息中间件RabbitMQ和Kafka
Stream 组件
- Spring Cloud Stream 构建的应用程序与消息中间件之间是通过绑定器 Binder 相关联的。绑定器对于应用程序而言起到了隔离作用, 它使得不同消息中间件的实现细节对应用程序来说是透明的。
- binding 是我们通过配置把应用和spring cloud stream 的 binder 绑定在一起
- output:发送消息 Channel,内置 Source接口
- input:接收消息 Channel,内置 Sink接口
Stream 消息生产者
- 创建消息生产者模块,引入依赖 starter-stream-rabbit
- 编写配置,定义 binder,和 bingings
- 定义消息发送业务类。添加 @EnableBinding(Source.class),注入MessageChannel output ,完成消息发送
- 编写启动类,测试
Stream 消息消费者
- 创建消息消费者模块,引入依赖 starter-stream-rabbit
- 编写配置,定义 binder,和 bingings
- 定义消息接收业务类。添加 @EnableBinding(Sink.class),使用@StreamListener(Sink.INPUT),完成消息接收。
- 编写启动类,测试
实验十二
项目结构
stream-producer模块
1、pom.xml 文件引入依赖
<dependencies>
<!--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>
</dependencies>
2、application.yml文件
server:
port: 8000
spring:
cloud:
stream:
# 定义绑定器,绑定到哪个消息中间件上
binders:
itheima_binder: # 自定义的绑定器名称
type: rabbit # 绑定器类型
environment: # 指定mq的环境
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
virtual-host: /
bindings:
output: # channel名称
binder: itheima_binder #指定使用哪一个binder
destination: itheima_exchange # 消息目的地
3、MessageProducer类、ProducerController类
@Component
@EnableBinding(Source.class)
public class MessageProducer {
@Autowired
private MessageChannel output;
public void send(){
String msessage = "hello stream~~~";
//发送消息
output.send(MessageBuilder.withPayload(msessage).build());
System.out.println("消息发送成功~~~");
}
}
@RestController
public class ProducerController {
@Autowired
private MessageProducer producer;
@RequestMapping("/send")
public String sendMsg(){
producer.send();
return "success";
}
}
4、启动类ProducerApp
@SpringBootApplication
public class ProducerApp {
public static void main(String[] args) {
SpringApplication.run(ProducerApp.class,args);
}
}
stream-consumer 模块
1、pom.xml 文件
<dependencies>
<!--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>
</dependencies>
2.application.yml文件
server:
port: 9001
spring:
cloud:
stream:
# 定义绑定器,绑定到哪个消息中间件上
binders:
itheima_binder: # 自定义的绑定器名称
type: rabbit # 绑定器类型
environment: # 指定mq的环境
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
virtual-host: /
bindings:
input: # channel名称
binder: itheima_binder #指定使用哪一个binder
destination: itheima_exchange # 消息目的地
3.MessageListener类
**
* 消息接收类
*/
@EnableBinding({Sink.class})
@Component
public class MessageListener {
@StreamListener(Sink.INPUT)
public void receive(Message message){
System.out.println(message);
System.out.println(message.getPayload());
}
}
4.启动类ConsumerApp
@SpringBootApplication
public class ConsumerApp {
public static void main(String[] args) {
SpringApplication.run(ConsumerApp.class,args);
}
}
运行结果:
Sleuth+Zipkin 链路追踪
概述
Spring Cloud Sleuth 其实是一个工具,它在整个分布式系统中能跟踪一个用户请求的过程,捕获这些跟踪数据,就能构建微服务的整个调用链的视图,这是调试和监控微服务的关键工具。
- 耗时分析
- 可视化错误
- 链路优化
- Zipkin 是 Twitter 的一个开源项目,它致力于收集服务的定时数据,以解决微服务架构中的延迟问题,包括数据的收集、存储、查找和展现。
Sleuth+Zipkin 快速入门
- 安装启动zipkin。
- java –jar zipkin.jar 访问zipkin web界面。
- http://localhost:9411/
- 在服务提供方和消费方分别引入 sleuth 和 zipkin 依赖
- 分别配置服务提供方和消费方。
- 启动,测试
启动zipkin,就是有该jar包的目录下,运行命令:java -jar .\zipkin-server-2.12.9-exec.jar
实验十三
略