一: 什么是SpringCloud
Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed systems (e.g. configuration management, service discovery, circuit breakers, intelligent routing, micro-proxy, control bus, one-time tokens, global locks, leadership election, distributed sessions, cluster state). Coordination of distributed systems leads to boiler plate patterns, and using Spring Cloud developers can quickly stand up services and applications that implement those patterns. They will work well in any distributed environment, including the developer’s own laptop, bare metal data centres, and managed platforms such as Cloud Foundry.
翻译如下:
Spring Cloud为开发人员提供了快速构建分布式系统中的一些常见模式的工具(例如配置管理、服务发现、断路器、智能路由、微代理、控制总线、一次性令牌、全局锁、领导层选举、分布式会话、集群状态)。分布式系统的协调导致了锅炉板模式,使用Spring Cloud开发人员可以快速建立实现这些模式的服务和应用程序。它们在任何分布式环境中都能很好地工作,包括开发人员自己的笔记本电脑、裸机数据中心和云计算等托管平台。
SpringCloud主要框架:
- 服务发现——Netflix Eureka
- 服务调用——Netflix Feign
- 熔断器——Netflix Hystrix
- 服务网关——Netflix Zuul
- 分布式配置——Spring Cloud Config
- 消息总线 —— Spring Cloud Bus
1.1 为什么要使用熔断器(雪崩效应)
在微服务架构中通常会有多个服务层调用,基础服务的故障可能会导致级联故障,进而造成整个系统不可用的情况,这种现象被称为服务雪崩效应。
服务雪崩效应是一种因“服务提供者”的不可用导致“服务消费者”的不可用,并将不可用逐渐放大的过程。
如果下图所示:A作为服务提供者,B为A的服务消费者,C和D是B的服务消费者。A不可用引起了B的不可用,并将不可用像滚雪球一样放大到C和D时,雪崩效应就形成了。
使用Hystrix
熔断器可以避免雪崩效应
1.2 什么是Hystrix
在分布式环境中,许多服务依赖项中的一些必然会失败。Hystrix是一个库,通过添加延迟容忍和容错逻辑,帮助你控制这些分布式服务之间的交互。Hystrix通过隔离服务之间的访问点、停止级联失败和提供回退选项来实现这一点,所有这些都可以提高系统的整体弹性。
下面是熔断器的机制:如下
二:熔断器的简单示例:
1、pom`导入依赖(在Feign中已经包含Hystrix了)
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2、在pin_teacher模块的application.yml
中,开启Hystrix:
feign:
#熔断器配置
hystrix:
#开启熔断器
enabled: true
完整如下:
server:
port: 8001
spring:
application:
name: pin-teacher
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ml?characterEncoding=UTF8&serverTimezone=UTC
username: root
password: 123456
profiles:
active: dev
feign:
#熔断器配置
hystrix:
#开启熔断器
enabled: true
mybatis:
mapper-locations: classpath:mapper/**/*.xml
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
3、在pin_teacher模块中,com.pin_teacher.feign包下创建impl包,创建熔断器类,实现TeacherFeign 接口(点击查看TeacherFeign 接口)
注意:TeacherFeign的实现类上必须加上@Component,否则会出现以下错误:
No fallback instance of type class com.**.impl.TeacherFeignImpl found for feign client pin-user
import com.ml.pin_teacher.feign.TeacherFeign;
import com.ml.pin_teacher.model.Student;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/**
* 熔断器类
*/
@Component
public class TeacherFeignImpl implements TeacherFeign {
@Override
public List<Student> findAll() {
System.out.println("teacher调用了熔断器。。。");
List<Student> students = new ArrayList<>();
Student student = new Student();
student.setId(22);
student.setAge(18);
student.setName("我是熔断器");
students.add(student);
return students;
}
}
4、修改pin_teacher
模块中的TeacherFeign
接口
import com.ml.pin_teacher.feign.impl.TeacherFeignImpl;
import com.ml.pin_teacher.model.Student;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
//fallback = TeacherFeignImpl.class 一旦熔断器打开,则执行该实现类
@FeignClient(value = "pin-user",fallback = TeacherFeignImpl.class)
public interface TeacherFeign {
@RequestMapping("student")
List<Student> findAll();
}
三:测试熔断器:
1.启动所有程序,查看eureka注册中心:
2、分别调用
A. 通过pin_user
服务直接调用查询所有学生接口:如下图
B. 通过pin_teacher
服务直接调用查询所有学生接口:如下图
此时我们把pin_user服务停掉,看下调用情况:
C. 通过pin_user
服务直接调用查询所有学生接口:如下图
D. 通过pin_teacher
服务直接调用查询所有学生接口:如下图
pin_teacher
服务也打印了teacher调用了熔断器。。。
开心一刻
老鼠没女朋友特别郁闷,终于一只蝙蝠答应嫁给他,老鼠十分高兴。
别人笑他没眼光,老鼠:你们懂什么,她好歹是个空姐。
如果觉得不错,帮忙点个赞,您的点赞将是我的动力!