从springboot到springcloud第五篇----springcloud hystrix

接着上面的基础来写hystrix熔断器

至于为什么用熔断器?

比如A服务需要同时调用B、C、D的服务,

如果其中D服务出现问题,那么线程就会阻塞在这里,如果有大量的请求进来,就会导致A服务也会瘫痪。(俗称雪崩)

而hystrix就是为了让出现异常的 服务做自动降级处理,快速返回。

 

一、hystrix和ribbon配合使用

1.1 在之前代码的基础上,在ribbon-client服务的pom文件中添加hystrix的jar

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

1.2 在启动类上添加注解@EnableHystrix

package com.example.ribbonclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;

@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
public class RibbonClientApplication {

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

}

1.3 修改ribbon的服务调用方法

在调用方法上加上@HystrixCommand注解,fallbackMethod 定义异常回调方法,

如果有需要排除不需要熔断的异常可以添加 ignoreExceptions 属性

package com.example.ribbonclient.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class RibbonController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/index")
    @HystrixCommand(fallbackMethod = "indexHistrxErrorMethod"
//        ,ignoreExceptions = {ArithmeticException.class}
    )
    public String index(){
        return restTemplate.getForObject("http://eureka-client/index",String.class);
    }

    private String indexHistrxErrorMethod(){
        return "服务异常,自动降级处理";
    }
}

1.4 为测试熔断机制,修改eureka-client 8763服务的代码

在serviceimpl中添加异常代码后重启

package com.example.eurekaclient.service.impl;

import com.example.eurekaclient.service.IndexService;
import org.springframework.stereotype.Service;

@Service
public class IndexServiceImpl implements IndexService {

    @Override
    public String getMesasge() {
        int number = 1/0;
        return "hello springcloud 8763";
    }
}

1.5 启动ribbon-client服务进行测试

 

二、hystrix和feign配合使用

之前说过feign中已经整合了hystrix,所以不需要再添加hystrix的jar依赖,只需要在配置文件中将熔断机制打开就可以了

1.1 在feign-client服务的application.yml文件中添加

#打开服务熔断机制
feign.hystrix.enabled=true

1.2 在服务调用的方法的注解里加上熔断回调的类

package com.example.feignclient.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient(value = "eureka-client",fallback = EurekaClientServiceHystrixClientFallback.class)
public interface EurekaClientService {

    @GetMapping("/index")
    String indexMessage();
}

1.3 创建这个类,并实现当前接口

package com.example.feignclient.service;

import org.springframework.stereotype.Component;

@Component
public class EurekaClientServiceHystrixClientFallback implements EurekaClientService {
    @Override
    public String indexMessage() {
        return "服务异常,自动降级处理";
    }
}

1.4 如果需要访问引起服务降级的原因,则可以使用@FeignClient中的fallbackFactory属性。(这里略过,贴上使用说明)

@FeignClient(name = "hello", fallbackFactory = HystrixClientFallbackFactory.class)
protected interface HystrixClient {
	@RequestMapping(method = RequestMethod.GET, value = "/hello")
	Hello iFailSometimes();
}

@Component
static class HystrixClientFallbackFactory implements FallbackFactory<HystrixClient> {
	@Override
	public HystrixClient create(Throwable cause) {
		return new HystrixClient() {
			@Override
			public Hello iFailSometimes() {
				return new Hello("fallback; reason was: " + cause.getMessage());
			}
		};
	}
}

1.5 运行测试

 

源码地址: https://github.com/houfanGitHub/springcloud.git

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值