SpringCloud-整体学习(五)OpenFeign(服务调用)

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

OpenFeign是什么

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

OpenFeign服务调用

新建cloud-consumer-feign-order80

老套路:在启动中加入 @EnableFeignClients 开启服务

pom(其他的跟 cloud-consumer-order80 pom 的一致)

 <!--openfeign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

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

@Service
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {

    @GetMapping("/payment/get/{id}")
    CommonResult<Payment> getPaymentByid(@PathVariable("id") Long id);

}

controller

@RestController
@Slf4j
public class OrderFeignController {

    @Autowired
    private PaymentFeignService paymentFeignService;

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPaymentByid(@PathVariable("id") Long id){
       return paymentFeignService.getPaymentByid(id);
    }
}

效果:
在这里插入图片描述

OpenFeign超时控制

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

在这里插入图片描述

OpenFeign日志增强

yml

logging:
  level:
    # feign日志以什么级别监控哪个接口
    com.lt.springcloud.service.PaymentFeignService: debug

新建
config
在这里插入图片描述

@Bean
    Logger.Level feignLoggerLevel(){
        return Logger.Level.FULL;
    }

服务调用后就会在控制台看见调用的相关信息。

SpringCloud-整体学习(六)Hystrix(服务降级)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值