SpringCloud | 五、熔断器-Hystrix

目录

 

一、改造项目service-ribbon

二、测试

Whitelabel Error Page


一、改造项目service-ribbon

pom.xml

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

启动类增加@EnableHystrix

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableHystrix
@RestController
public class ServiceRibbonApplication {

服务类增加熔断注解

@Service
public class HelloService {

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "hiError")
    public String hiService(String name) {
        return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
    }

    public String hiError(String name) {
        return "hi,"+name+",sorry,error!";
    }

}

二、测试

执行service-ribbon启动类

浏览器访问 http://localhost:8764/hi?name=forezp

hi forezp ,i am from port:8763

关闭service-hi服务

hi,forezp,sorry,error!

在service-hi的服务类里加1秒延迟

开启service-hi服务

hi,forezp,sorry,error!

熔断的时间应该在1秒之内,具体懒得测。如果去掉熔断

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue Aug 20 14:55:17 CST 2019

There was an unexpected error (type=Internal Server Error, status=500).

I/O error on GET request for "http://SERVICE-HI/hi": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect

http空接口错误返回。

参考:
https://blog.csdn.net/forezp/article/details/70148833

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值