Spring Cloud 熔断器

Spring Cloud 熔断器

在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以通过 RPC 相互调用,在 Spring Cloud 中可以用 RestTemplate + Ribbon 和 Feign 来调用。为了保证其高可用,单个服务通常会集群部署。由于网络原因或者自身的原因,服务并不能保证 100% 可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,Servlet 容器的线程资源会被消耗完毕,导致服务瘫痪。服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是服务故障的 “雪崩” 效应。
即为了避免一个故障导致请求积压阻塞而挤垮整个系统,当有一个服务不可用时,比如到出现5秒内出现N次无法访问,则熔断器打开不再往后请求而直接返回。

Hystrix

Netflix 开源了 Hystrix 组件,实现了熔断器模式,Spring Cloud 对这一组件进行了整合。

ribbon中使用hystrix

pom中增加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

启动类中增加hystrix注解

@EnableHystrix

package com.outlook.liufei32.spring.cloud.web.admin.ribbon;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;

@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class WebAdminRibbonApplication {
    public static void main( String[] args ) {        SpringApplication.run(WebAdminRibbonApplication.class, args);
    }
}

在service中的方法增加@HystrixCommand注解并在配置返回方法(fallbackMethod = "hiError")

package com.outlook.liufei32.spring.cloud.web.admin.ribbon.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class AdminService {
    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "hiError")
    public String sayHi( String message ) {
        return restTemplate.getForObject("http://SPRING-CLOUD-SERVICE-ADMIN/hi?message=" + message, String.class);
    }

    public String hiError(String message) {
        return "Hi,your message is :"" + message + "" but request error.";
    }
}

feign中使用hystrix

打开feign的熔断器配置

Feign 是自带熔断器的,但默认是关闭的。需要在配置文件中配置打开它,在配置文件增加以下代码:

feign:
  hystrix:
    enabled: true

启动类中增加hystrix注解

package com.outlook.liufe32.spring.cloud.web.admin.feign;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class WebAdminFeignApplication {
    public static void main( String[] args ) {
        SpringApplication.run(WebAdminFeignApplication.class, args);
    }
}

servie接口中增加fallback类

service接口:

package com.outlook.liufe32.spring.cloud.web.admin.feign.service;

import com.outlook.liufe32.spring.cloud.web.admin.feign.service.hystrix.AdminServiceHystrix;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value = "spring-cloud-service-admin",fallback = AdminServiceHystrix.class)//指定fallback类来设置熔断器
public interface AdminService {

    @RequestMapping(value = "hi", method = RequestMethod.GET)
    public String sayHi( @RequestParam(value = "message") String message );

}

service接口实现类,这个实现类override的方法就是feign熔断返回的方法

package com.outlook.liufe32.spring.cloud.web.admin.feign.service.hystrix;

import com.outlook.liufe32.spring.cloud.web.admin.feign.service.AdminService;
import org.springframework.stereotype.Component;

@Component
public class AdminServiceHystrix implements AdminService {

    @Override
    public String sayHi( String message ) {
        return "Hi,your message is :"" + message + "" but request error.";
    }
}

本博客为Swagger-Ranger的笔记分享,文章会持续更新
文中源码地址: https://github.com/Swagger-Ranger
欢迎交流指正,如有侵权请联系作者确认删除: liufei32@outlook.com

原文地址:https://www.cnblogs.com/Swagger-Ranger/p/10671499.html