hytrix以及feign使用

单独使用hytrix

依赖

 <dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-metrics-event-stream</artifactId>
            <version>1.5.12</version>
        </dependency>
        <dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-javanica</artifactId>
            <version>1.5.12</version>
        </dependency>

配置文件

@Configuration
public class HystrixConfig {

    //用来拦截处理HystrixCommand注解
    @Bean
    public HystrixCommandAspect hystrixAspect() {
        return new HystrixCommandAspect();
    }

    //用来像监控中心Dashboard发送stream信息
    @Bean
    public ServletRegistrationBean hystrixMetricsStreamServlet() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
        registration.addUrlMappings("/hystrix.stream");
        return registration;
    }
}

使用

只要在需要使用的方法上加上@HystrixCommand注解,并加上配置就可以了。 fallbackMethod = “getOrderPageListFallback”,这边引号里的就是执行降解会执行的方法。

@Controller
public  class GateWayService {

    Logger logger = LoggerFactory.getLogger(this.getClass());


    @HystrixCommand(
            fallbackMethod = "getOrderPageListFallback",
            threadPoolProperties = {  //10个核心线程池,超过20个的队列外的请求被拒绝; 当一切都是正常的时候,线程池一般仅会有1到2个线程激活来提供服务
                    @HystrixProperty(name = "coreSize", value = "5"),
                    @HystrixProperty(name = "maxQueueSize", value = "5"),
                    @HystrixProperty(name = "queueSizeRejectionThreshold", value = "10")},
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.strategy", value = "THREAD"),
                    @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000"), //命令执行超时时间
                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "2"), //若干10s一个窗口内失败三次, 则达到触发熔断的最少请求量
                    @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "30000") //断路30s后尝试执行, 默认为5s
            })
            @RestController("/test")
    public Object getRemote() {
//        return restTemplate.getForEntity("http://localhost:8080/adp/exportExcel",Object.class);
        return null;
    }







    public String getOrderPageListFallback() {
        logger.error("===================== 执行降级策略");
        return "调用失败" ;
    }

集成feign后使用hytrix
使用feign的话需要使用eureka来作为注册中心

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>

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

需要在properties中指定 注册进入eureka的名字 用于其他服务调用本服务
spring.application.name=test

@FeignClient注解在接口上 interface,fallback指定熔断执行方法。

@FeignClient(name="dataresource-adp4",fallback = DataResourceServiceImpl.class) //name中的名字就是在eureka中注册的名字
public interface DataResourceService {

    @RequestMapping("/dr/dataSources")
    AdpResponse getDataSources();

    @RequestMapping("/dr/tables/bySourceId")
    AdpResponse bySourceId(@RequestParam(value = "sourceId") String sourceId);//有参数的需要用@RequestParam来指定参数名称
}

fallback方法

@Component
public class DataResourceServiceImpl implements DataResourceService {
    @Override
    public AdpResponse getDataSources() {
        AdpResponse adpResponse = new AdpResponse();
        adpResponse.setDesc("服务器似乎已经爆炸,请稍后重试");
        adpResponse.setCode(500);
        return adpResponse;
    }

    @Override
    public AdpResponse bySourceId(String id) {
        AdpResponse adpResponse = new AdpResponse();
        adpResponse.setDesc("服务器似乎已经爆炸,请稍后重试");
        adpResponse.setCode(500);
        return adpResponse;
    }
}

 	 @Autowired
    DataResourceService dataResourceService;//这就是上面的接口,和普通的service调用方式一样
    
    @RequestMapping("/dr/tables/bySourceId")
    public AdpResponse getTableBySourceId(HttpServletRequest request){
        String id = request.getParameter("id");
        AdpResponse notice = dataResourceService.bySourceId(id); //调用feign的接口
        return notice;
    }

可能不全,后面还会继续补充,有疑惑欢迎提问,内容有什么问题欢迎指出。。。。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值