2--SpringCloud熔断器hystrix

什么是断路器

  “断路器”本身是一种开关装置,用于在电路上保护线路过载,当线路中有电器发生短路时,“断路器”能够及时的切断故障电路,防止发生过载、发热、甚至起火等严重后果。

  在分布式架构中,断路器模式的作用也是类似的,当某个服务单元发生故障(类似用电器发生短路)之后,通过断路器的故障监控(类似熔断保险丝),向调用方返回一个错误响应,而不是长时间的等待。这样就不会使得线程因调用故障服务被长时间占用不释放,避免了故障在分布式系统中的蔓延。

  在Spring Cloud中使用了Hystrix 来实现断路器的功能。Hystrix是Netflix开源的微服务框架套件之一,该框架目标在于通过控制那些访问远程系统、服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力。Hystrix具备拥有回退机制和断路器功能的线程和信号隔离,请求缓存和请求打包,以及监控和配置等功能。

 

使用断路器

  首先在上一文中已经实现了

  eureka.server工程:服务注册中心  端口1111

  eureka.provider工程:服务提供者  端口2222

  eureka.customer工程:服务消费者  端口3333

  若您还没有使用Spring Cloud的经验 请先阅读 1--SpringCloud的服务注册与发现Eureka

 

  首先我们在eureka.customer工程服务的消费者的pom文件里面加上hystrix依赖

<!-- 新增断路器hystrix的依赖 -->
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
    </dependency>

  然后我们在启动主类上面添加@EnableCircuitBreaker注解开启断路器功能:

package com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker//开启断路器
public class RibbonApplication {

    @Bean
    @LoadBalanced//负载均衡的开启
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

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

}

  新增server业务处理类去调用COMPUTE-SERVICE里面的add方法,在处理方法上面加上@HystrixCommand注解 里面的addServiceFallback表示出错后回调的方法,回调方法必须和调用者在同一类中。

 

package com;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@Service
public class ConsumerService {

    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "addServiceFallback")//如果调用服务失败的话,调用方法addServiceFallback
    public String addService() {
        return restTemplate.getForEntity("http://COMPUTE-SERVICE/add?a=10&b=20", String.class).getBody();
    }
    
    public String addServiceFallback() {
        return "error";
    }
}

  修改ConsumerController

  我们直接调用业务层的方法。

package com;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ConsumerController {

    @Autowired
    private ConsumerService server;

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add() {
        return server.addService();
    }
//
}

断路器的测试

  这样依次启动按道理说应该就实现了hystrix的断路器的功能。

  但是我的 服务提供者并没有下线,中间也没有抛错,也顺利的执行了add方法,但是返回的确实错误回调方法的error

  然后我想到可能是服务在调用的时候发生了超时,hystrix的默认超时时间是1秒钟,可能是机器太慢发生超时。

  可以在配置文件里面设置超时时间,单位是毫秒

#设置断路器超时时间
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000

  这下就正常了。

  源码下载:2--SpringCloud熔断器hystrix

posted @ 2018-11-01 17:42  曾将  阅读(516)  评论(0编辑  收藏  举报
//目录 欢迎评论,一起学习,对大家有用请点个赞。