《SpringCloud专题14》-OpenFeign超时控制
原创
文章标签 spring 数据 主键 文章分类 Hadoop 大数据
©著作权归作者所有:来自51CTO博客作者熊猫IT技术的原创作品,请联系作者获取转载授权,否则将追究法律责任
1.超时设置,故意设置超时演示出错情况
1.1.服务提供方8001故意写暂停程序
("feign/timeout") public String getFeignTimeOut() { try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } return serverPort; }
1.2.服务消费方80添加超时方法PaymentFeignService
(value = "cloud-payment-service")
public interface PaymentFeignService {
("payment/get/{id}")
CommonResult<Payment> selectOne(("id") Long id);
("payment/feign/timeout")
String getFeignTimeOut();
}
1.3.服务消费方80添加超时方法OrderFeignController
package com.itxiongmao.controller;
import com.itxiongmao.service.PaymentFeignService;
import com.itxiongmao.springcloud.entities.CommonResult;
import com.itxiongmao.springcloud.entities.Payment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
("order")
public class OrderFeignController {
private PaymentFeignService paymentFeignService;
("selectOne/{id}")
public CommonResult<Payment> selectOne(("id") Long id){
return paymentFeignService.selectOne(id);
}
("/feign/timeout")
public String getFeignTimeOut() {
return paymentFeignService.getFeignTimeOut();
}
}
1.4.测试
OpenFeign默认等待1秒钟,超过后报错
2.超时设置
OpenFeign默认支持Ribbon
YML文件里需要开启OpenFeign客户端超时控制
server: port: 80spring: application: name: cloud-comsumer-feign-order80eureka: client: service-url: # 集群版 defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka# 设置feign客户端超时时间(OpenFeign默认支持ribbon)ribbon: # 指的是建立连接所用的时间,适用于网络状态正常的情况下,两端连接所用的时间 ReadTimeout: 5000 # 指的是建立连接后从服务器读取到可用资源所用的时间 ConnectTimeout: 5000
重新测试
- 赞
- 收藏
- 评论
- *举报
下一篇:FastDFS手把手安装
Original: https://blog.51cto.com/u_13819911/5429736
Author: 熊猫IT
Title: 《SpringCloud专题14》-OpenFeign超时控制
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/517032/
转载文章受原作者版权保护。转载请注明原作者出处!