Spring Cloud构建微服务架构(三)断路器,Java开发新手入门教程

本文介绍了如何在Spring Cloud微服务架构中使用Hystrix作为断路器,通过示例展示了如何在服务消费方启用断路器功能,以及在Feign客户端中利用Hystrix进行服务降级处理。当服务提供方不可用时,断路器会触发回调方法,提供默认响应。此外,文章还提及了如何通过注解配置Feign客户端的回退策略。

org.springframework.cloud

spring-cloud-starter-hystrix

  • 在eureka-ribbon的主类RibbonApplication中使用@EnableCircuitBreaker注解开启断路器功能:

@SpringBootApplication

@EnableDiscoveryClient

@EnableCircuitBreaker

public class RibbonApplication {

@Bean

@LoadBalanced

RestTemplate restTemplate() {

return new RestTemplate();

}

public static void main(String[] args) {

SpringApplication.run(RibbonApplication.class, args);

}

}

  • 改造原来的服务消费方式,新增ComputeService类,在使用ribbon消费服务的函数上增加@HystrixCommand注解来指定回调方法。

@Service

public class ComputeService {

@Autowired

RestTemplate restTemplate;

@HystrixCommand(fallbackMethod = “addServiceFallback”)

public String addService() {

return restTemplate.getForEntity(“http://COM

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值