首先先了解下分布式系统面临的问题:
Hystrix是什么: Hystrix有何用?- 服务降级
- 服务熔断
- 实时监控
可惜的是目前已经进入停更状态
Hystrix重要三个概念: 实操:新建一个模块cloud-provider-hystrix-payment8001
pom
org.springframework.cloud spring-cloud-starter-netflix-hystrixorg.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-actuatorcom.alibaba druid-spring-boot-starter1.1.10 mysql mysql-connector-javaorg.springframework.boot spring-boot-starter-jdbcorg.springframework.boot spring-boot-devtoolsruntime true org.projectlombok lomboktrue org.springframework.boot spring-boot-starter-testtest org.springframework.boot spring-boot-devtoolsruntime true com.atgugui.springcloud cloud-api-commons${project.version} org.springframework.cloud spring-cloud-starter-netflix-eureka-client2.2.7.RELEASE
yml
server:
port: 8001
spring:
application:
name: cloud-provider-hystrix-payment
datasource:
url: jdbc:mysql://localhost:3306/db2019?useSSL=false&useUnicode=true&characterEncoding=utf-8
driver-class-name: org.gjt.mm.mysql.Driver #mysql驱动包
username: root
password: 123123
type: com.alibaba.druid.pool.DruidDataSource
eureka:
client:
register-with-eureka: true #表示向注册中心注册自己 默认为true
fetch-registry: true #是否从EurekaServer抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
service-url:
defaultZone: http://localhost:7001/eureka/ # 入驻地址 单击版
#defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
#集群版
主启动类:
@SpringBootApplication
@EnableEurekaClient
public class PaymentHystrixMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentHystrixMain8001.class,args);
}
}
service
@Service
public class PaymentService {
//成功
public String paymentInfo_OK(Integer id){
return "线程池:"+Thread.currentThread().getName()+" paymentInfo_OK,id: "+id+"t"+"哈哈哈" ;
}
//失败
public String paymentInfo_TimeOut(Integer id){
int timeNumber = 3;
try { TimeUnit.SECONDS.sleep(timeNumber); }catch (Exception e) {e.printStackTrace();}
return "线程池:"+Thread.currentThread().getName()+" paymentInfo_TimeOut,id: "+id+"t"+"呜呜呜"+" 耗时(秒)"+timeNumber;
}
}
controller
@RestController
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
@Value("${server.port}")
private String serverPort;
@GetMapping("/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id){
String result = paymentService.paymentInfo_OK(id);
log.info("*******result:"+result);
return result;
}
@GetMapping("/payment/hystrix/timeout/{id}")
public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
String result = paymentService.paymentInfo_TimeOut(id);
log.info("*******result:"+result);
return result;
}
}
启动微服务
访问: localhost:8001/payment/hystrix/ok/31、
localhost:8001/payment/hystrix/timeout/31 效果是只有第二个在转圈才出效果,第一个秒出效果
以上述为根基平台,从正确--》错误--》降级熔断--》恢复
效果好像问题不太大,那么我们来个高并发测试吧!
高并发测试!在浏览器再发分别访问
localhost:8001/payment/hystrix/ok/31、
localhost:8001/payment/hystrix/timeout/31
效果:两个都在转圈
为什么第一个也会被卡死?原因是 tomcat默认的工作线程数被打满了,没有多余的线程来分解压力和处理!!!
结论:
看热闹不嫌事大,我们新建cloud-consumer-fegin-order80
pom
org.springframework.cloud spring-cloud-starter-netflix-hystrixorg.springframework.cloud spring-cloud-starter-openfeignorg.springframework.cloud spring-cloud-starter-netflix-eureka-clientcom.atgugui.springcloud cloud-api-commons${project.version} org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-actuatororg.springframework.boot spring-boot-devtoolsruntime true org.projectlombok lomboktrue org.springframework.boot spring-boot-starter-testtest com.alibaba druid-spring-boot-starter1.1.10 mysql mysql-connector-javaorg.springframework.boot spring-boot-starter-jdbc
yml
server:
port: 80
spring:
application:
name: cloud-provider-hystrix-order
datasource:
url: jdbc:mysql://localhost:3306/db2019?useSSL=false&useUnicode=true&characterEncoding=utf-8
driver-class-name: org.gjt.mm.mysql.Driver #mysql驱动包
username: root
password: 123123
type: com.alibaba.druid.pool.DruidDataSource
eureka:
client:
register-with-eureka: false #表示向注册中心注册自己 默认为true
fetch-registry: true #是否从EurekaServer抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
service-url:
defaultZone: http://localhost:7001/eureka/ # 入驻地址 单击版
# defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
#集群版
主启动类
@SpringBootApplication
@EnableFeignClients
public class OrderHystrixMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderHystrixMain80.class,args);
}
}
service
@Component
@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT",fallback = PaaymentFallbackService.class)
public interface PaymentHystrixService {
@GetMapping("/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id);
@GetMapping("/payment/hystrix/timeout/{id}")
public String paymentInfo_TimeOut(@PathVariable("id") Integer id);
}
@Component
public class PaaymentFallbackService implements PaymentHystrixService{
@Override
public String paymentInfo_OK(Integer id) {
return "-----paymentFallbackservice fall back ok";
}
@Override
public String paymentInfo_TimeOut(Integer id) {
return "-----paymentFallbackservice fall back timeout";
}
}
controller
@RestController
@Slf4j
public class OrderHystrixController {
@Resource
private PaymentHystrixService paymentHystrixService;
@Value("${server.port}")
private String serverPort;
@GetMapping("/consumer/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id){
String result = paymentHystrixService.paymentInfo_OK(id);
log.info("*******result:"+result);
return result;
}
@GetMapping("/consumer/payment/hystrix/timeout/{id}")
public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
String result = paymentHystrixService.paymentInfo_TimeOut(id);
log.info("*******result:"+result);
return result;
}
}
测试访问 localhost/payment/hystrix/ok/31 发现正常
再测试一波高并发:
2w个线程同时压8001的timeout方法
再开个浏览器去访问正常的方法,发现转圈等待 然后
正因为有了上诉故障或不佳表现才有了降级/容错/限流等技术产生
后面具体的代码看脑图10



