2--SpringCloud熔断器hystrix

什么是断路器

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

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

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

 

使用断路器

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

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

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

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

  若您尚未使用Spring Cloud的经验 请先阅读 1--SpringCloud的服务注册与发现Eureka架构

 

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

<!-- 新增断路器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

相关文章
相关标签/搜索