说明
本文演示:notice 无响应,将会导致 ums 无响应,感受 服务雪崩
创建ums_hystrix子模块
根据 《新建ums工程(使用OpenFeign)》的 ums 工程,再创建 ums_hystrix 工程
不要动 ums 工程,万一搞坏了,没有备用方案
创建 ums_hystrix 子模块
略
修改 pom.xml
将 ums 工程的 pom.xml 中的依赖,复制粘贴到 ums_hystrix 子模块中
<dependencies>
<!-- openfeign依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>top.malaoshi</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- springboot依赖,不写版本号表示继承父工程 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
复制 代码、配置文件
复制 ums java代码、配置文件 到 ums_hystrix中
修改 application.yml
由于 本工程 通过 openfeign 调用 notice接口,而notice接口 休眠3秒,openfeign 会因为 超时 而报错(openfeign默认超时时间是1秒),所以需要增加超时配置,如下:
ribbon:
ConnectTimeout: 3000
ReadTimeout: 4000
openfeign 底层 使用 ribbon,所以使用 ribbon 配置
修改 UmsMain 启动类
修改 该类为:UmsHystrixMain
测试启动
启动 UmsHystrixMain 服务
测试访问
确保能够正常访问,访问下面接口:
http://localhost:8081/follow
并携带form数据:
fromUserid=123
toUserid=456
msg=关注
该接口调用 notice工程 的接口,延时很小,能够快速返回响应
jmeter 压力测试
按照 Hystrix创建notice_hystrix子模块,压测感受高并发卡顿(jmeter) ,开启压力测试,模拟 其他服务 大并发量访问 notice工程的接口:http://localhost:8080/getNoticeById/195c6547e5044c759dc39caf317998c5 ,该接口由于休眠 3 秒,tomcat工作线程全部被占用,导致处理慢,甚至超时
访问另一个接口
然后访问下面接口:
http://localhost:8081/follow
并携带form数据:
fromUserid=123
toUserid=456
msg=关注
会发现 该接口 处理慢,甚至超时报错
得出结论
服务提供者 由于 某个接口(如:
getNoticeById) 访问量过大,处理不过来,tomcat工作线程全部被占用,将会 影响 所有接口(如:sendNotice接口)由于 服务提供者 的所有接口慢,将导致 服务调用者 的接口慢,甚至超时报错,即: 服务调用者 的tomcat工作线程全部被占用,导致 所有接口处理慢,甚至超时,甚至让服务崩溃
