前提版本: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、启动项目测试,搞定!