springcloud-熔断器

服务雪崩效应

当一个请求依赖多个服务的时候:

正常情况下的访问

springcloud-熔断器_第1张图片
但是,当请求的服务中出现无法访问、异常、超时等问题时(图中的I),那么用户的请求将会被阻塞。

springcloud-熔断器_第2张图片
如果多个用户的请求中,都存在无法访问的服务,那么他们都将陷入阻塞的状态中。
springcloud-熔断器_第3张图片
Hystrix的引入,可以通过服务熔断和服务降级来解决这个问题。

服务熔断服务降级

Hystrix断路器简介

hystrix对应的中文名字是“豪猪”,豪猪周身长满了刺,能保护自己不受天敌的伤害,代表了一种防御机制,这与hystrix本身的功能不谋而合,因此Netflix团队将该框架命名为Hystrix,并使用了对应的卡通形象做作为logo。
springcloud-熔断器_第4张图片
在一个分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等,如何能够保证在一个依赖出问题的情况下,不会导致整体服务失败,这个就是Hystrix需要做的事情。Hystrix提供了熔断、隔离、Fallback、cache、监控等功能,能够在一个、或多个依赖同时出现问题时保证系统依然可用。

Hystrix服务熔断服务降级@HystrixCommand fallbackMethod

熔断机制是应对雪崩效应的一种微服务链路保护机制。

当某个服务不可用或者响应时间超时,会进行服务降级,进而熔断该节点的服务调用,快速返回自定义的错误影响页面信息。

我们写个项目来测试下;

新建一个microservice-student-provider-hystrix-1004项目

springcloud-熔断器_第5张图片
导入依赖



    4.0.0
    
        com.yq
        microservice
        1.0-SNAPSHOT
    


    microservice-student-provider-hystrix-1004


    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            mysql
            mysql-connector-java
        
        
            org.springframework.boot
            spring-boot-starter-tomcat
        
        
            com.alibaba
            druid-spring-boot-starter
        
        
            com.yq
            microservice-common
            1.0-SNAPSHOT
            compile
        

        
        
            org.springframework.cloud
            spring-cloud-starter-eureka
        
        
            org.springframework.cloud
            spring-cloud-starter-config
        

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

        
        
            org.springframework.cloud
            spring-cloud-starter-hystrix
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



yml文件

server:
  port: 1004
  context-path: /
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
    username: root
    password: 123
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
  application:
    name: microservice-student
  profiles: provider-hystrix-1004

eureka:
  instance:
    hostname: localhost
    appname: microservice-student
    instance-id: microservice-student:1004
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://eureka2001.yq.com:2001/eureka/,http://eureka2002.yq.com:2002/eureka/,http://eureka2003.yq.com:2003/eureka/

info:
  groupId: com.yq.testSpringcloud
  artifactId: microservice-student-provider-hystrix-1004
  version: 1.0-SNAPSHOT
  userName: http://yq.com
  phone: 123456

MicroserviceStudentProviderHystrix1004Application

package com.yq.microservicestudentproviderhystrix1004;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableCircuitBreaker
@EntityScan("com.yq.*.*")
@EnableEurekaClient
@SpringBootApplication
public class MicroserviceStudentProviderHystrix1004Application {

    public static void main(String[] args) {
        SpringApplication.run(MicroserviceStudentProviderHystrix1004Application.class, args);
    }

}

StudentProviderController

package com.yq.microservicestudentproviderhystrix1004.controller;


import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.yq.microservicecommon.entity.Student;

import com.yq.microservicestudentproviderhystrix1004.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/student")
public class StudentProviderController {

    @Autowired
    private StudentService studentService;

    @PostMapping(value="/save")
    public boolean save(Student student){
        try{
            studentService.save(student);
            return true;
        }catch(Exception e){
            return false;
        }
    }

    @GetMapping(value="/list")
    public List list(){
        return studentService.list();
    }

    @GetMapping(value="/get/{id}")
    public Student get(@PathVariable("id") Integer id){
        return studentService.findById(id);
    }

    @GetMapping(value="/delete/{id}")
    public boolean delete(@PathVariable("id") Integer id){
        try{
            studentService.delete(id);
            return true;
        }catch(Exception e){
            return false;
        }


    }
    @Value("${server.port}")
    private String port;

    @RequestMapping("/ribbon")
    public String ribbon(){
        return "工号【"+port+"】正在为您服务";
    }


    /**
     * 测试Hystrix服务降级
     * @return
     * @throws InterruptedException
     */
    @GetMapping(value="/hystrix")
    @HystrixCommand(fallbackMethod="hystrixFallback")
    public Map hystrix() throws InterruptedException{
//        Thread.sleep(2000);
        Map map=new HashMap();
        map.put("code", 200);
        map.put("info","工号【"+port+"】正在为您服务");
        return map;
    }

    public Map hystrixFallback() throws InterruptedException{
        Map map=new HashMap();
        map.put("code", 500);
        map.put("info", "系统【"+port+"】繁忙,稍后重试");
        return map;
    }

}


在80端口也要加一个对应的方法

StudentConsumerController

package com.yq.microservicestudentconsumer80.controller;

import com.yq.microservicecommon.entity.Student;
import com.yq.microservicecommon.service.StudentClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/student")
public class StudentConsumerController {

    private final static String SERVER_IP_PORT = "http://MICROSERVICE-STUDENT";

 
     @Autowired
     private RestTemplate restTemplate;
      
     @PostMapping(value="/save")
     private boolean save(Student student){
         return restTemplate.postForObject(SERVER_IP_PORT+"/student/save", student, Boolean.class);
     }
      
    @GetMapping(value="/list")
    public List list(){
        return restTemplate.getForObject(SERVER_IP_PORT+"/student/list", List.class);
    }
     
    @GetMapping(value="/get/{id}")
    public Student get(@PathVariable("id") Integer id){
        return restTemplate.getForObject(SERVER_IP_PORT+"/student/get/"+id, Student.class);
    }
     
    @GetMapping(value="/delete/{id}")
    public boolean delete(@PathVariable("id") Integer id){
        try{
            restTemplate.getForObject(SERVER_IP_PORT+"/student/delete/"+id, Boolean.class);
            return true;
        }catch(Exception e){
            return false;
        }
    }
    @RequestMapping("/ribbon")
    public String ribbon(){
        return restTemplate.getForObject(SERVER_IP_PORT + "/student/ribbon", String.class);
    }


    /**
     * 测试Hystrix服务降级
     * @return
     */
    @GetMapping(value="/hystrix")
    @ResponseBody
    public Map hystrix(){
        return restTemplate.getForObject(SERVER_IP_PORT+"/student/hystrix/", Map.class);
    }
}

springcloud-熔断器_第6张图片
这里改成1.1秒就会雪崩
springcloud-熔断器_第7张图片

springcloud-熔断器_第8张图片

Hystrix默认超时时间设置

yml中加配置

server:
  port: 1004
  context-path: /
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
    username: root
    password: 123
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
  application:
    name: microservice-student
  profiles: provider-hystrix-1004

eureka:
  instance:
    hostname: localhost
    appname: microservice-student
    instance-id: microservice-student:1004
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://eureka2001.yq.com:2001/eureka/,http://eureka2002.yq.com:2002/eureka/,http://eureka2003.yq.com:2003/eureka/

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 3000

info:
  groupId: com.yq.testSpringcloud
  artifactId: microservice-student-provider-hystrix-1004
  version: 1.0-SNAPSHOT
  userName: http://yq.com
  phone: 123456

2秒的
springcloud-熔断器_第9张图片
这里改成1.5秒
springcloud-熔断器_第10张图片
springcloud-熔断器_第11张图片

Hystrix服务监控Dashboard

Hystrix服务监控Dashboard仪表盘

Hystrix提供了 准实时的服务调用监控项目Dashboard,能够实时记录通过Hystrix发起的请求执行情况,
可以通过图表的形式展现给用户看。

我们新建项目:microservice-student-consumer-hystrix-dashboard-90

springcloud-熔断器_第12张图片
导入依赖



    4.0.0
    
        com.yq
        microservice
        1.0-SNAPSHOT
    

    microservice-student-consumer-hystrix-dashboard-90


    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        

        
        
            org.springframework.cloud
            spring-cloud-starter-hystrix
        
        
            org.springframework.cloud
            spring-cloud-starter-hystrix-dashboard
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



yml配置

server:
  port: 90
  context-path: /

MicroserviceStudentConsumerHystrixDashboard90Application

package com.yq.microservicestudentconsumerhystrixdashboard90;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@EnableHystrixDashboard
public class MicroserviceStudentConsumerHystrixDashboard90Application {

    public static void main(String[] args) {
        SpringApplication.run(MicroserviceStudentConsumerHystrixDashboard90Application.class, args);
    }

}

springcloud-熔断器_第13张图片
springcloud-熔断器_第14张图片
springcloud-熔断器_第15张图片
刷一下这边
springcloud-熔断器_第16张图片
然后这边就会显示百分之百错误
springcloud-熔断器_第17张图片

你可能感兴趣的:(springcloud-熔断器)