码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • OpenFeign服务接口调用


    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

    文章目录

    • 概述
      • OpenFeign是什么
      • 能干嘛
      • Feign和OpenFeign两者区别
    • OpenFeign使用步骤(Feign在消费端使用)
      • 接口+注解---------微服务调用接口+@FeignClient
      • 引入坐标
      • 将消费者配置到eureka集群中 yml
      • 消费者主启动上加@EnableFeignClients,开启基于OpenFegin的服务调用功能
      • 新建接口加上@FeignClien
      • controller
    • OpenFeign超时控制
      • 服务提供方故意超时执行
      • 服务消费方
      • 测试:http://localhost/consumer/payment/feign/timeout
      • 如何修改默认的超时时?
    • OpenFeign日志打印功能
      • 是什么
      • 日志级别
      • 配置日志bean
      • YML文件里需要开启日志的Feign客户端


    概述

    OpenFeign是什么

    GitHub:https://github.com/spring-cloud/spring-cloud-openfeign

    在这里插入图片描述
    在这里插入图片描述
    Feign是一个声明式的web服务客户端,让编写web服务客户端变得非常容易,只需创建一个接口并在接口上添加注解即可

    能干嘛

    在这里插入图片描述

    Feign和OpenFeign两者区别

    在这里插入图片描述

    OpenFeign使用步骤(Feign在消费端使用)

    接口+注解---------微服务调用接口+@FeignClient

    引入坐标

    
         <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    将消费者配置到eureka集群中 yml

    server:
      port: 80
    eureka:
      client:
        register-with-eureka: false
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    消费者主启动上加@EnableFeignClients,开启基于OpenFegin的服务调用功能

    package com.atguigu.springcloud;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    
    @SpringBootApplication
    @EnableFeignClients
    public class OrderFeignMain80 {
        public static void main(String[] args) {
            SpringApplication.run(OrderFeignMain80.class,args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    新建接口加上@FeignClien

    package com.atguigu.springcloud.service;
    
    import com.atguigu.springcloud.entities.CommonResult;
    import com.atguigu.springcloud.entities.Payment;
    import feign.Param;
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.stereotype.Component;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    
    @Component
    @FeignClient(value = "CLOUD-PAYMENT-SERVICE")
    public interface PaymentFeignService {
         @GetMapping(value = "/payment/feign/timeout")
         public String paymentFeignTimeout();
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    controller

    @RestController
    public class OrderFeignController {
    
        @Resource
        private PaymentFeignService paymentFeignService;
    
        @GetMapping(value = "/consumer/payment/get/{id}")
        public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
           return paymentFeignService.getPaymentById(id);
        }
    
    }
     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述

    OpenFeign超时控制

    在这里插入图片描述

    服务提供方故意超时执行

    @GetMapping(value = "/payment/feign/timeout")
    public String paymentFeignTimeout(){
        try { TimeUnit.SECONDS.sleep(3); }catch (Exception e) {e.printStackTrace();}
        return serverPort;
    }
     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    服务消费方

    @Component
    @FeignClient(value = "CLOUD-PAYMENT-SERVICE")
    public interface PaymentFeignService {
    
         @GetMapping(value = "/payment/feign/timeout")
         public String paymentFeignTimeout();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    @RestController
    public class OrderFeignController {
    
        @Resource
        private PaymentFeignService paymentFeignService;
       @GetMapping(value = "/consumer/payment/feign/timeout")
       public String paymentFeignTimeout(){
          return paymentFeignService.paymentFeignTimeout();
       }
     
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    测试:http://localhost/consumer/payment/feign/timeout

    提示访问超时
    在这里插入图片描述

    如何修改默认的超时时?

    OpenFeign默认支持Ribbon
    在这里插入图片描述
    YML文件里需要开启OpenFeign客户端超时控制

    ribbon:
      ReadTimeout:  5000
      ConnectTimeout: 5000
    
    • 1
    • 2
    • 3

    OpenFeign日志打印功能

    是什么

    在这里插入图片描述

    日志级别

    在这里插入图片描述

    配置日志bean

    package com.atguigu.springcloud.config;
    
    import feign.Logger;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class FeignConfig {
    
        @Bean
        Logger.Level feignLoggerLevel(){
            return Logger.Level.FULL;
        }
    }
     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    YML文件里需要开启日志的Feign客户端

    logging:
      level:
        com.atguigu.springcloud.service.PaymentFeignService: debug
    
    • 1
    • 2
    • 3

    在这里插入图片描述

  • 相关阅读:
    GuavaCache学习三种过期策略的学习
    网络安全(黑客)自学
    集美大学 - 2840 - 实验10和11 - 编程题
    Docker应用下滑、近七成开发者有安全疑虑,中国云原生用户调查报告来了
    K8S:配置资源管理 Secret和configMap
    在微信小程序中如何引入iconfont
    时序与空间结构
    WZOI-256奇怪的数列
    DSI及DPHY的学习
    LT1085稳压器介绍(含数据手册datasheet下载)
  • 原文地址:https://blog.csdn.net/m0_45364328/article/details/125451810
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号