• 9、OpenFeign服务接口调用


    1、概述

    OpenFeign是什么

    能干吗


    Feign和OpenFeign的区别

    2、Feign使用步骤

    pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>springcloud2021</artifactId>
            <groupId>com.test.springcloud</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>cloud-consumer-feign-order80</artifactId>
    
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.cloud</groupId>
                        <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>com.test.springcloud</groupId>
                <artifactId>cloud-api-commons</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
            <!--  open feign      -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-loadbalancer</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
        </dependencies>
    </project>
    

    application.yml

    server:
      port: 80
    spring:
      application:
        name: consumer-feign-order
      cloud:
        nacos:
          discovery:
            server-addr: localhost:8848
    management:
      endpoints:
        web:
          exposure:
            include: "*"
    feign:
      okhttp: true
      httpclient: false
    

    PaymentFeignService

    @Component
    @FeignClient(name = "nacos-payment-provider")
    public interface PaymentFeignService {
    
        @GetMapping(value = "/payment/nacos/{id}")
        public String getPayment(@PathVariable("id") Long id);
    }
    
    

    OrderFeignController

    @RestController
    public class OrderFeignController {
        @Resource
        private PaymentFeignService paymentFeignService;
    
        @GetMapping(value = "/consumer/payment/nacos/{id}")
        public String getPayment(@PathVariable("id") Long id) {
            return paymentFeignService.getPayment(id);
        }
    }
    

    3、Feign超时时间

    feign:
      client:
        config:
          default:
            #建立连接所用的时间,适用于网络状况正常的情况下,两端连接所需要的时间
            ConnectTimeOut: 1000
            #指建立连接后从服务端读取到可用资源所用的时间
            ReadTimeOut: 1000
    

    4、Feign日志

    application.yml

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

    config

    @Configuration
    public class OpenFeignConfig {
        @Bean
        Logger.Level feignLoggerLevel() {
            return Logger.Level.FULL;
        }
    }
    


  • 相关阅读:
    电子邮件的工作原理
    常用邮箱服务器地址端口
    wpf \silverlight 保存控件为图片
    GIS理论(墨卡托投影、地理坐标系、地面分辨率、地图比例尺、Bing Maps Tile System)【转载】
    Visifire图表控件官网地址
    Ado方式导入excel混用数据类型引起数据缺失问题解决方法
    c#日期时间的操作
    获得excel的sheet名字
    正则表达式验证可发短信的号码,如手机号和小灵通号码(106+区号+号码)
    验证多行文本框输入长度的正则表达式
  • 原文地址:https://www.cnblogs.com/xidianzxm/p/14355589.html
Copyright © 2020-2023  润新知