SpringCloud-整体学习(一)SpringCloud简介+版本选择
SpringCloud-整体学习(二)项目初始构建-加公共部分提取
SpringCloud-整体学习(三)Eureka、zookeeper、Consul(注册中心)
SpringCloud-整体学习(四)Ribbon(负载均衡+手写轮询算法)
SpringCloud-整体学习(五)OpenFeign(服务调用)
SpringCloud-整体学习(六)Hystrix(服务降级)
SpringCloud-整体学习(七)GateWay(服务网关)
SpringCloud-整体学习(八)Config、Bus、Stream(服务配置和消息交互)
SpringCloud-整体学习(九)Sleuth(分布式请求链路追踪)
SpringCloud-整体学习(十)SpringCloudAlibaba(注册中心+配置中心)
SpringCloud-整体学习(十一) Sentinel(服务降级)
SpringCloud-整体学习(十二) Seata(分布式事务)
git :
https://github.com/lucine-maker/cloud2020
gitee:
https://gitee.com/lucine_li_tao/springcloud
springcloud 文档
https://docs.spring.io/spring-cloud-openfeign/docs/2.2.6.RELEASE/reference/html/#netflix-feign-starter
github源码
https://github.com/spring-cloud/spring-cloud-openfeign
这篇文章还不错
https://www.cnblogs.com/codecow/p/12714680.html
新建cloud-consumer-feign-order80
老套路:在启动中加入 @EnableFeignClients 开启服务
pom(其他的跟 cloud-consumer-order80 pom 的一致)
org.springframework.cloud
spring-cloud-starter-openfeign
yml :
server:
port: 80
eureka:
client:
#表示是否将自己注册进入eurekaServer默认为true
register-with-eureka: false
#是否从EurekaServer抓取已有信息,默认为true,单节点无所谓,集群必须设置为true,才能配合ribbon使用负载均衡
# fetchRegistry: true
service-url:
# defaultZone: http://localhost:7001/eureka/
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
项目结构
@Service
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {
@GetMapping("/payment/get/{id}")
CommonResult getPaymentByid(@PathVariable("id") Long id);
}
controller
@RestController
@Slf4j
public class OrderFeignController {
@Autowired
private PaymentFeignService paymentFeignService;
@GetMapping("/consumer/payment/get/{id}")
public CommonResult getPaymentByid(@PathVariable("id") Long id){
return paymentFeignService.getPaymentByid(id);
}
}
8001
@GetMapping(value = "/payment/feign/timeout")
public String paymentFeignTimeout(){
try {
TimeUnit.SECONDS.sleep(3);
// Thread.sleep(3000);
}catch (Exception e){
e.printStackTrace();
}
return serverPort;
}
在service配置对应openfeign
controller
@GetMapping("/consumer/payment/feign/timeout")
public String paymentFeignTimeout(){
return paymentFeignService.paymentFeignTimeout();
}
# 设置feign客户端超时时间(OpenFeign默认支持ribbon)
ribbon:
# 指的是建立连接后从服务器读取到可用资源所用的时间
ReadTimeout: 5000
# 指的是建立连接所用的时间,适用于网络状态正常的情况下,两端连接所用的时间
ConnectTimeout: 5000
yml
logging:
level:
# feign日志以什么级别监控哪个接口
com.lt.springcloud.service.PaymentFeignService: debug
@Bean
Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
服务调用后就会在控制台看见调用的相关信息。
SpringCloud-整体学习(六)Hystrix(服务降级)