史上最简单的Spring Cloud (三):Hystrix断路器

前提版本:SpringBoot :2.1.9     spring-cloud.version:Greenwich.SR3

*** 分别在Ribbon和Feign中使用!

一、在Ribbon中使用Hystrix

1、在上一个的ribbon-client项目中,引入Hystrix的maven依赖:

<!-- Hystrix断路器 -->
<dependency>
         <groupId>org.springframework.cloud</groupId>-->
         <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>-->
</dependency>

2、在启动类上加注解@EnableHystrix引入Hystrix,或者也可以用@EnableCircuitBreaker注解。

3、在RibbonService类的方法上加上@HystrixCommand注解,并指定fallbackMethod熔断方法,创建hiError方法:

package com.smiletou.sweet.dream.service;
 
import com.smiletou.sweet.dream.entity.SysUser;
import com.smiletou.sweet.dream.result.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
 
/**
 * Ribbon负载均衡服务调用
 *
 * @author ZhangChao
 * @date 2019/10/31 14:46
 * @since 1.0.0
 */
@Service
public class RibbonService {
    /**
     * 将IOC容器中的负载均衡RestTemplate工具注入进来
     */
    @Autowired
    RestTemplate restTemplate;
 
    @HystrixCommand(fallbackMethod = "hiError")
    public ResponseMessage getOtherServerData(String dictCode){
       
        ResponseEntity<ResponseMessage> responseEntity = restTemplate.getForEntity("http://ENTERPRISE-SPRINGCLOUD-SYSTEM/sys/dict/list?dictCode={1}",ResponseMessage.class,dictCode);
        
        ResponseMessage rm = responseEntity.getBody();
 
        return rm;
    }
 
    public String hiError(String name) {
        return "hi,"+name+",sorry,error!";
    }
 
}

4、启动项目测试,搞定!

二、在Feign中使用Hystrix

1、在上一个的feign-client项目中,引入Hystrix的maven依赖:

<!-- Hystrix断路器 -->
<dependency>
         <groupId>org.springframework.cloud</groupId>-->
         <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>-->
</dependency>

2、需要在application.properties配置文件中开启Hystrix:

feign.hystrix.enabled=true

3、在接口上的@FeignClient注解里,增加fallback = ErrorHandler.class配置。

package com.smiletou.sweet.dream.service;

import com.smiletou.sweet.dream.hystrix.ErrorHandle;
import com.smiletou.sweet.dream.result.ResponseMessage;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Feign声明式服务调用
 *
 * @author ZHANGCHAO
 * @date 2019/11/5 14:32
 * @since 1.0.0
 */
@FeignClient(value = "ZHZY",fallback = ErrorHandle.class)
public interface FeignService {

    @PostMapping(value = "/core/user/login")
    ResponseMessage login(@RequestParam("username") String username,
                          @RequestParam("password") String password,
                          @RequestParam("node") String node,
                          @RequestParam(value = "force", required = false) String force);
}

4、编写熔断处理类ErrorHandle,需要实现@FeignClient注解的接口,并且使用@Component加入IOC容器。

package com.smiletou.sweet.dream.hystrix;

import com.smiletou.sweet.dream.result.ResponseMessage;
import com.smiletou.sweet.dream.service.FeignService;
import org.springframework.stereotype.Component;

/**
 * @author ZHANGCHAO
 * @date 2019/11/6 9:10
 * @since 1.0.0
 */
@Component
public class ErrorHandle implements FeignService {

    @Override
    public ResponseMessage login(String username, String password, String node, String force) {
        return new ResponseMessage(false,"出现错误!",username+";"+password+";"+node+";"+force);
    }
}

5、启动项目测试,搞定!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值