spring cloud hystrix在普通应用和feign上使用

hystrix普通用法
  • 引入包
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
  • 在main类中添加注解@EnableCircuitBreaker
@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class MicroserviceSimpleConsumerApplication {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(MicroserviceSimpleConsumerApplication.class, args);
  • 在接口处实现fallback方法使用@HystrixCommand注解,指明方法名
 @GetMapping("/consumer/{id}")
    @HystrixCommand(fallbackMethod = "findByIdFallback", commandProperties = @HystrixProperty(name = "execution.isolation.strategy", value = "SEMAPHORE"))
    public User findById(@PathVariable("id") Long id) {
        return this.restTemplate.getForObject("http://SIMPLE-PROVIDER/provider/" + id, User.class);
    }

    public User findByIdFallback(Long id) {
        User u = new User();
        u.setId(-1L);
        return u;
    }
hystrix在feign中应用

注意的是:fegin中默认hystrix功能是关闭的,具体见https://github.com/spring-cloud/spring-cloud-netflix/issues/1277

  • 首先开启feign中的hystrix功能

    feign:
    hystrix:
    enabled: true
  • 使用 @FeignClient 注解属性进行设置fallback

@FeignClient(name = "SIMPLE-PROVIDER", fallback = HystrixFallBackClient.class)
public interface UserFeignClient {

    @GetMapping("/provider/{id}")
    public User findUserById(@PathVariable("id") Long id);
}
  • 写fallback的类
package com.example.demo.client;

import org.springframework.stereotype.Component;

import com.example.demo.entity.User;

@Component
public class HystrixFallBackClient implements UserFeignClient {

    @Override
    public User findUserById(Long id) {
        User user = new User();
        user.setId(0L);
        return user;
    }
}
另外一种写fallback方法
  • 利用fallbackFactory进行编写(原理和直接写fallback类似,但factory可以提供异常信息)
@FeignClient(name = "SIMPLE-PROVIDER", configuration = UserFeignClientConfig.class, /**fallback = UserFeignClientFallback.class,**/ fallbackFactory = UserClientfallbackFactory.class)
public interface UserFeignClient {

    @GetMapping("/provider/{id}")
    @HystrixCommand(fallbackMethod = "findUserByIdFallback")
    public User findUserById(@PathVariable("id") Long id);
}
  • 编写factory类实现client接口
package com.example.demo.client;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.example.demo.entity.User;

import feign.hystrix.FallbackFactory;

@Component
public class UserClientfallbackFactory implements FallbackFactory<UserFeignClient> {

    Logger log = LoggerFactory.getLogger(UserClientfallbackFactory.class);

    @Override
    public UserFeignClient create(Throwable cause) {
        return new UserFeignClient() {

            @Override
            public User findUserById(Long id) {
                log.info("fall back cause :{}", cause.getMessage());
                User u = new User();
                u.setId(-2L);
                u.setName("fallbackFactory");
                return u;
            }

        };

    }

}

注意的是:@FeignClient 中的fallback和fallbackFactory只能同时配一个

hystrix用法总结

– 使用hystrix自生的注解
– 使用feign注解中的设置

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值