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