spring cloud hystrix在普通应用和feign上使用
2018-01-29 17:56
741 查看
hystrix普通用法
引入包
在main类中添加注解
在接口处实现fallback方法使用
hystrix在feign中应用
注意的是:fegin中默认hystrix功能是关闭的,具体见https://github.com/spring-cloud/spring-cloud-netflix/issues/1277
首先开启feign中的hystrix功能
使用
写fallback的类
另外一种写fallback方法
利用fallbackFactory进行编写(原理和直接写fallback类似,但factory可以提供异常信息)
编写factory类实现client接口
注意的是:
hystrix用法总结
– 使用hystrix自生的注解
– 使用feign注解中的设置
引入包
<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注解中的设置
相关文章推荐
- SpringCloud 学习 | 第六篇: feign断路器使用(hystrix)
- Spring Cloud 的 Hystrix 在 Feign上使用 Hystrix功能
- 关于在Spring Cloud Feign工程中使用Hystrix配置不生效的问题
- springcloud中使用Hystrix保护应用
- 第十七章 Spring cloud +Feign+Hystrix+fallbackFactory的使用
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- [转]spring cloud之熔断器 Hystrix 的原理与使用
- Spring Cloud Netflix的3大组件应用 Eureka&Ribbon&Hystrix
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- spring-cloud-feign 使用@RequetParam报错QueryMap parameter must be a Map: class java.lang.String
- spring-cloud-starter-feign 组件在spring boot中的应用:访问Oauth2资源服务器的资源接口
- 关于在Spring Cloud Feign工程中使用Ribbon配置不生效的问题
- (三) Spring Cloud - 断路器+(普通调用和feign封装调用)
- springCloud(12):使用Hystrix实现微服务的容错处理-Hystrix的监控
- Spring Cloud Netflix 教程(Feign+Ribbon+Hystrix)
- spring cloud-Feign的Hystrix支持
- SpringCloudNetflix-Feign/Hystrix Setting
- spring Cloud中,解决Feign/Ribbon整合Hystrix第一次请求失败的问题?
- spring cloud-Feign使用中遇到的问题总结
- spring cloud 之 Feign 使用HTTP请求远程服务