栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Hystrix断路器

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Hystrix断路器

分布式系统面临的问题:

首先先了解下分布式系统面临的问题:

Hystrix是什么:

Hystrix有何用?
  • 服务降级
  • 服务熔断
  • 实时监控

可惜的是目前已经进入停更状态

 Hystrix重要三个概念:

实操:

新建一个模块cloud-provider-hystrix-payment8001

pom

  
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-hystrix
        
    
        org.springframework.boot
        spring-boot-starter-web
    

    
    
        org.springframework.boot
        spring-boot-starter-actuator
    


    
    
        com.alibaba
        druid-spring-boot-starter
        1.1.10
    
    
    
        mysql
        mysql-connector-java
    

    
    
        org.springframework.boot
        spring-boot-starter-jdbc
    

    
    
        org.springframework.boot
        spring-boot-devtools
        runtime
        true
    

    
    
        org.projectlombok
        lombok
        true
    

    
    
        org.springframework.boot
        spring-boot-starter-test
        test
    

    
    
        org.springframework.boot
        spring-boot-devtools
        runtime
        true
    
    
        com.atgugui.springcloud
        cloud-api-commons
        ${project.version}
    
    

    
    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client
    2.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-hystrix
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            com.atgugui.springcloud
            cloud-api-commons
            ${project.version}
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            com.alibaba
            druid-spring-boot-starter
            1.1.10
        
        
        
            mysql
            mysql-connector-java
        

        
        
            org.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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/488910.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号