Spring Cloud(Finchley.RCI) (六) Spring Cloud使用熔断器防止服务雪崩

熔断器简介

在微服务架构中, 根据业务来拆分成一个个的服务, 服务与服务之间可以通过RPC相互调用, 在Spring Cloud中可以用RestTemplate+Ribbon和Feign来调用。为了保证其高可用, 单个服务通常会集群部署。由于网络原因或者自身的原因, 服务并不能保证100%可用, 如果单个服务出现问题, 调用这个服务就会出现线程阻塞, 此时若有大量的请求涌入, Servlet容器的线程资源会被消耗完毕, 导致服务瘫痪。服务与服务之间的依赖性, 故障会传播, 会对整个微服务系统造成灾难性的严重后果, 这就是服务故障的雪崩效应

Ribbon中使用熔断器

在pom中增加依赖

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

在Application中增加@EnableHystrix注解

package com.funtl.hello.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;

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

在Service中增加@HystrixCommand注解, 在Ribbon调用方法上增加@HystrixCommand注解并指定fallbackMethod熔断方法

package com.funtl.hello.spring.cloud.web.admin.ribbon.service;

import com.netflix.discovery.converters.Auto;
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://hello-spring-cloud-service-admin/hi?message="+message, String.class);
    }

    public String hiError(String message){
        return String.format("Hi your message is: %s but request bad", message);
    }
}

Feign中使用熔断器

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

feign:
  hystrix:
    enabled:

在Service中增加fallback指定类

package com.funtl.hello.spring.cloud.web.admin.feign.service;

import com.funtl.hello.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="hello-spring-cloud-service-admin", fallback= AdminServiceHystrix.class)
public interface AdminService {

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

创建熔断器类并实现对应的Feign接口

package com.funtl.hello.spring.cloud.web.admin.feign.service.hystrix;

import com.funtl.hello.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 String.format("Hi your message is: %s but request bad", message);
  

测试熔断器

此时我们关闭服务提供者, 再次请求http://localhost:8765/hi?message=HelloFeign

发布了69 篇原创文章 · 获赞 8 · 访问量 9417

猜你喜欢

转载自blog.csdn.net/u011414629/article/details/101415733