java容错机制_Spring-Cloud-hystrix-(容错机制)

Spring-cloud-hystrix-容错机制(当服务调用异常时进行响应)

1.在App.java中开启容错保护(加入此注解开启容错机制@EnableCircuitBreaker)

@SpringBootApplication

@EnableEurekaClient

@EnableCircuitBreaker

public class RequestApp {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

SpringApplication.run(RequestApp.class, args);

}

@Bean

@LoadBalanced

public RestTemplate newRestTemplate(){

return new RestTemplate();

}

}

2.pom.xml中引用hystrix依赖

org.springframework.boot

spring-boot-starter-parent

1.3.7.RELEASE

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

org.springframework.cloud

spring-cloud-starter-eureka

org.springframework.cloud

spring-cloud-starter-hystrix

org.springframework.cloud

spring-cloud-dependencies

Camden.SR3

pom

import

3。Controller

@RequestMapping(value = "CircuitBreakerInfo")

public List circuitBreakerInfo(){

return circuitBreakerserver.circuitBreakerInfoService();

}

Service

@HystrixCommand(fallbackMethod = "circuiterrorMethod")

public List circuitBreakerInfoService(){

System.out.println("测试Hystrix / circuitBreaker 方法");

Map maps = new HashMap();

maps.put("name", "request");

return restTemplate.getForObject(ribbonUrl, List.class,maps);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值