首页 > 编程语言 > 详细

spring cloud 的hystrix 熔断器 和feign 调用的使用

时间:2020-10-25 10:59:26      阅读:33      评论:0      收藏:0      [点我收藏+]

1, 添加依赖

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>


2,yml 文件开启熔断
feign:
hystrix:
enabled: true


3,启动类上的注解
@EnableHystrix
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

}

4,feign 的接口
@FeignClient(name = "testInfo", fallback= RemoteHelloCallBack.class)
public interface RemoteHello {
@RequestMapping(value = "/hello/error", method = RequestMethod.GET)
public String getError();

@RequestMapping(value = "/hello/get", method = RequestMethod.GET)
public String getRemote();

}


5,hystrix 的使用
@Component
public class RemoteHelloCallBack implements RemoteHello {
@Override
public String getError() {
return "RemoteError is block";
}

@Override
public String getRemote() {
return "get Remote hello is block";
}
}

6, 控制层简单的调用

@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "helle consul";
}
@Autowired
RemoteHello hello;
@RequestMapping("/getRemote")
public String getRemote() {
return hello.getRemote();
}

@RequestMapping("/geterror")
public String geterror() {
return hello.getError();
//return "this is error message..";
}

}








spring cloud 的hystrix 熔断器 和feign 调用的使用

原文:https://www.cnblogs.com/bruce1992/p/13871892.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!