1. Hystrix对Feign的支持
添加Feign中IUserBiz的实现类HystrixFallBack:
package com.wangx.cloud.springcloud02consumer.configure; import com.wangx.cloud.springcloud02consumer.api.UserApi; import org.springframework.stereotype.Component; @Component public class HystrixFallBack implements UserApi { @Override public String getUser(Integer id) { System.out.println("连接超时....."); return "system error"; } }
使用@component注解注册成组件。在@FeignClient注解里面添加fallback属性即可。
如下:
package com.wangx.cloud.springcloud02consumer.api; import com.wangx.cloud.springcloud02consumer.configure.FooConfiguration; import com.wangx.cloud.springcloud02consumer.configure.HystrixFallBack; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @FeignClient(value = "spring-cloud-provider", configuration = FooConfiguration.class, fallback = HystrixFallBack.class) public interface UserApi { @RequestMapping(value = "/user/getUser") String getUser(@RequestParam(value = "id") Integer id); }
当出现请求错误或超时时,就会执行实现类中的方法。
熔断设置
#默认为false,如果想用断路由,要打开这个设置 feign.hystrix.enabled=true #断路器线程池超时时间,这个值一定要比ribbon超时时间长,毫秒 hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=16000
单个应用禁用Hystrix
在FooConfiguration中配置一个bean
package com.wangx.cloud.springcloud02consumer.configure; import feign.Feign; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; @Configuration public class FooConfiguration { @Bean @Scope("prototype") public Feign.Builder feignBuilder() { return Feign.builder(); } }
配置隔离级别
# 在feign和Ribbon里面配置隔离策略(全局配置) #hystrix.command.default.execution.isolation.strategy=SEMAPHORE # 配置单个 ystrixCommandKey在Ribbon下面默认为方法名,在feign下面默认为类名#方法名(参数类型) #hystrix.command.HystrixCommandKey.execution.isolation.strategy=SEMAPHORE
说明:HystrixCommandKey在Ribbon下面默认为方法名,在feign下面默认为类名#方法名(参数类型)
Feign下的HystrixCommandKey类的配置
package com.wangx.cloud.springcloud02consumer.configure; import com.netflix.hystrix.HystrixCommand; import com.netflix.hystrix.HystrixCommand.Setter; import com.netflix.hystrix.HystrixCommandGroupKey; import com.netflix.hystrix.HystrixCommandKey; import feign.Feign; import feign.Target; import feign.hystrix.HystrixFeign; import feign.hystrix.SetterFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; import java.lang.reflect.Method; @Configuration public class FooConfiguration { @Bean @Scope("prototype") public Feign.Builder feignBuilder() { class RcSetterFactory implements SetterFactory { @Override public Setter create(Target<?> target, Method method) { String groupKey = target.name(); String commandKey = method.getName(); return HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey)) .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)); } } return HystrixFeign.builder().setterFactory(new RcSetterFactory()); //return Feign.builder(); } }