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

hystrix普通用法
  • 引入包
<dependency>
    <groupId>org.springframework.cloudgroupId>
    <artifactId>spring-cloud-starter-hystrixartifactId>
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注解中的设置

你可能感兴趣的:(spring,cloud)