Spring Cloud Hystrix

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

1 . Ribbon中的配置 : 添加依赖 pom.xml :
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
2 . 修改ComputeService
package com.wuxicloud.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;
/**
 * #Author : EalenXie
 * #CreateTime : 2017-04-28 13:56
 */
@Service
public class ComputeService {
    @Autowired
    RestTemplate restTemplate;
    @HystrixCommand(fallbackMethod = "addServiceFallBack")  //fallbackMethod方法指定回调方法
    public String addService() {
        return restTemplate.getForEntity("http://SPRINGCLOUD-HELLOWORLD-SERVICE/add?a=10&b=20", String.class).getBody();
    }
    public String addServiceFallBack() {
        return "error";
    }
}
3 . 在eureka-ribbon的主类RibbonApplication中使用@EnableCircuitBreaker注解开启断路器功能
package com.wuxicloud;
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;
/**
 * #Author : EalenXie
 * #CreateTime : 2017-04-27 16:01
 * @EnableDiscoveryClient 注解来添加发现服务能力
 * @EnableCircuitBreaker 开启断路器功能
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public class RibbonApplication {
    /**
     * 创建RestTemplate实例
     * @LoadBalanced 此注解开启负载均衡能力
     */
    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
    public static void main(String[] args) {
        SpringApplication.run (RibbonApplication.class, args);
    }
}
4 . 改造原来的服务消费方式,新增ComputeService类,在使用ribbon消费服务的函数上增加@HystrixCommand注解来指定回调方法。
package com.wuxicloud.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;
/**
 * #Author : EalenXie
 * #CreateTime : 2017-04-28 13:56
 */
@Service
public class ComputeService {
    @Autowired
    RestTemplate restTemplate;
//fallbackMethod方法指定回调方法
    @HystrixCommand(fallbackMethod = "addServiceFallBack")  
    public String addService() {
        return restTemplate.getForEntity("http://SPRINGCLOUD-HELLOWORLD-SERVICE/add?a=10&b=20", String.class).getBody();
    }
    public String addServiceFallBack() {
        return "error";
    }
}
5 . 提供rest接口的Controller改为调用ComputeService的addService:
package com.wuxicloud.web;
import com.wuxicloud.service.ComputeService;
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;
import javax.annotation.Resource;
/**
 * #Author : EalenXie
 * #CreateTime : 2017-04-27 16:12
 */
@RestController
public class ConsumerController {
    @Resource
    ComputeService computeService;
    @Autowired
    RestTemplate restTemplate;
    @RequestMapping(value = "/add", method = RequestMethod.GET )
    public String add() {
        return computeService.addService();
    }
}
6 . 关闭compute-service服务后再访问http://localhost:3333/add,页面显示:error
Feign使用Hystrix ,使用@FeignClient注解中的fallback属性指定回调类 :
package com.wuxicloud.service;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
/**
 * #Author : EalenXie
 * #CreateTime : 2017-04-27 16:49
 * @FeignClient : 绑定该接口对应的服务,fallback是请求失败的回调类
 */
@FeignClient(value = "SpringCloud-HelloWorld-service", fallback = ComputeClientHystrix.class)
public interface ComputeService {
    // 请求参数和服务方法的请求参数名字相同
    @RequestMapping(method = RequestMethod.GET , value = "/add")
    Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b);
}
创建回调类,并实现ComputeService :
package com.wuxicloud.service;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;
/**
 * #Author : EalenXie
 * #CreateTime : 2017-04-28 14:54
 */
@Component
public class ComputeClientHystrix implements ComputeService {
    @Override
    public Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b) {
        return -9999;
    }
}
再用之前的方法验证一下,是否在compute-service服务不可用的情况下,页面返回了-9999。
posted @ 2024-01-10 14:10  EalenXie  阅读(14)  评论(0编辑  收藏  举报