复制代码
1
2
3
4<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13@SpringBootApplication @EnableDiscoveryClient @EnableHystrix public class ServiceRibbonApplication { public static void main(String[] args) { SpringApplication.run(ServiceRibbonApplication.class, args); } @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }
复制代码
http://localhost:8764/hello?name=Jack
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16@Service public class HelloService { @Autowired RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "helloError") public String helloService(String name) { return restTemplate.getForObject("http://HELLO-SERVICE/hello?name="+name, String.class); } public String helloError(String name) { return "hello " + name + ", error!"; } }
复制代码
http://localhost:8764/hello?name=Jack
1http://localhost:8764/hello?name=Jack
复制代码
1hello Jack, error!
https://github.com/lingd3/SpringCloudLearning/tree/master/chapter5
最后
以上就是舒适服饰最近收集整理的关于Spring Cloud Learning | 第五篇:服务容错保护(Hystrix)的全部内容,更多相关Spring内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复