Java17 --- SpringCloud之OpenFeign

目录

一、OpenFeign实现服务调用

1.1、创建openfeign微服务

二、Openfeign超时控制

2.1、全局默认配置

2.2、单个微服务配置

三、重试机制

四、替换openfeign默认的HttpClient

五、请求响应压缩

六、日志打印


一、OpenFeign实现服务调用

1.1、创建openfeign微服务

1、导入pom依赖

XML 复制代码
<dependencies>
    <!--openfeign-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <!--SpringCloud consul discovery -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>com.cjc.cloud</groupId>
        <artifactId>cloud-api-commons</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <!--web + actuator-->
    <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>
    <!--lombok-->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <!--hutool-all-->
    <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
    </dependency>
    <!--fastjson2-->
    <dependency>
        <groupId>com.alibaba.fastjson2</groupId>
        <artifactId>fastjson2</artifactId>
    </dependency>
    <!-- swagger3 调用方式 http://你的主机IP地址:5555/swagger-ui/index.html -->
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    </dependency>
</dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

2、配置yml及主启动类

java 复制代码
server:
  port: 80

spring:
  application:
    name: cloud-consumer-openfeign-order
  ####Spring Cloud Consul for Service Discovery
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        prefer-ip-address: true #优先使用服务ip进行注册
        service-name: ${spring.application.name}
java 复制代码
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients //开启openfeign
public class MainOpenFeign80 {
    public static void main(String[] args) {
        SpringApplication.run(MainOpenFeign80.class,args);
    }
}

3、相关代码及接口

java 复制代码
@FeignClient(value = "cloud-payment-service")
public interface PayFeignApi {
    //添加
    @GetMapping("/pay/add")
    public ResultData addOrder(PayDTO payDTO);
    //查询
    @GetMapping("/pay/getById/{id}")
    public ResultData getByIdOrder(@PathVariable("id") Integer id);
    //负载均衡
    @GetMapping("/pay/info")
    public ResultData getOrLoad();
}
java 复制代码
@RestController
public class FeignController {
    @Autowired
    private PayFeignApi payFeignApi;

    @PostMapping("/feign/pay/add")
    public ResultData addPay(@RequestBody PayDTO payDTO){
        System.out.println("添加调用成功");
        return payFeignApi.addOrder(payDTO);
    }
    @GetMapping("/feign/pay/get/{id}")
    public ResultData getByPayId(@PathVariable("id") Integer id){
        System.out.println("查询调用成功");
        return payFeignApi.getByIdOrder(id);
    }
    @GetMapping("/pay/get/info")
    public ResultData getInfo(){
        System.out.println("负载均衡调用成功");
        return payFeignApi.getOrLoad();
    }
}

二、Openfeign超时控制

客户端默认超时时间是60s

2.1、全局默认配置

在yml文件中配置

java 复制代码
spring:
  application:
    name: cloud-consumer-openfeign-order
  ####Spring Cloud Consul for Service Discovery
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        prefer-ip-address: true #优先使用服务ip进行注册
        service-name: ${spring.application.name}
    openfeign:
      client:
        config:
          default:
            #链接超时时间
            connect-timeout: 3000
            #读取超时时间
            read-timeout: 3000

2.2、单个微服务配置

java 复制代码
spring:
  application:
    name: cloud-consumer-openfeign-order
  ####Spring Cloud Consul for Service Discovery
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        prefer-ip-address: true #优先使用服务ip进行注册
        service-name: ${spring.application.name}
    openfeign:
      client:
        config:
          #单个微服务配置
          cloud-payment-service:
            #链接超时时间
            connect-timeout: 3000
            #读取超时时间
            read-timeout: 3000

三、重试机制

使用配置类开启

java 复制代码
@Configuration
public class FeignConfig {
    @Bean
    public Retryer retryer(){
        //return Retryer.NEVER_RETRY;//默认不重试
        //最大请求次数为3(1+2),初始间隔时间为100ms,重试间最大间隔时间为1s
        return new Retryer.Default(100,1,3);
    }
}

四、替换openfeign默认的HttpClient

导入pom

XML 复制代码
<!-- httpclient5-->
    <dependency>
        <groupId>org.apache.httpcomponents.client5</groupId>
        <artifactId>httpclient5</artifactId>
        <version>5.3</version>
    </dependency>
    <!-- feign-hc5-->
    <dependency>
        <groupId>io.github.openfeign</groupId>
        <artifactId>feign-hc5</artifactId>
        <version>13.1</version>
    </dependency>

修改yml文件

java 复制代码
  application:
    name: cloud-consumer-openfeign-order
  ####Spring Cloud Consul for Service Discovery
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        prefer-ip-address: true #优先使用服务ip进行注册
        service-name: ${spring.application.name}
    openfeign:
      client:
        config:
          #单个微服务配置
          cloud-payment-service:
            #链接超时时间
            connect-timeout: 3000
            #读取超时时间
            read-timeout: 3000
      httpclient: #开启H5
        hc5:
          enabled: true

开启后:

五、请求响应压缩

修改yml配置文件

java 复制代码
spring:
  application:
    name: cloud-consumer-openfeign-order
  ####Spring Cloud Consul for Service Discovery
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        prefer-ip-address: true #优先使用服务ip进行注册
        service-name: ${spring.application.name}
    openfeign:
      client:
        config:
          #单个微服务配置
          cloud-payment-service:
            #链接超时时间
            connect-timeout: 3000
            #读取超时时间
            read-timeout: 3000
      httpclient: #开启H5
        hc5:
          enabled: true
      compression:
        request:
          enabled: true
          min-request-size: 2048 #最小触发压缩的大小
          mime-types: text/xml,application/xml,application/json #触发压缩数据类型
        response:
          enabled: true

六、日志打印

修改配置类

java 复制代码
//feign日志功能等级
    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }

修改yml配置文件

java 复制代码
spring:
  application:
    name: cloud-consumer-openfeign-order
  ####Spring Cloud Consul for Service Discovery
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        prefer-ip-address: true #优先使用服务ip进行注册
        service-name: ${spring.application.name}
    openfeign:
      client:
        config:
          #单个微服务配置
          cloud-payment-service:
            #链接超时时间
            connect-timeout: 3000
            #读取超时时间
            read-timeout: 3000
      httpclient: #开启H5
        hc5:
          enabled: true
      compression:
        request:
          enabled: true
          min-request-size: 2048 #最小触发压缩的大小
          mime-types: text/xml,application/xml,application/json #触发压缩数据类型
        response:
          enabled: true
# feign日志以什么级别监控哪个接口
logging:
  level:
    com:
      cjc:
        cloud:
          apis:
            PayFeignApi: debug
相关推荐
无聊看看天T^T2 分钟前
Linux中线程的基本概念与线程控制
java·开发语言
木子020411 分钟前
idea项目运行时 java: 错误: 不支持发行版本 21
java·ide·intellij-idea
王俊山IT14 分钟前
C++学习笔记----11、模块、头文件及各种主题(二)---- 函数模板(2)
开发语言·c++·笔记·学习
t5y2216 分钟前
【C语言】Union
c语言·开发语言·算法
没有黑科技20 分钟前
仓库管理系统设计与实现(Java+Mysql)
java·开发语言·mysql
LG.YDX28 分钟前
java:接口,抽象,多态的综合小练习
java·开发语言
刘瑾言37 分钟前
ES操作命令
java·elasticsearch
计算机-秋大田37 分钟前
基于Spring Boot的船运物流管理系统的设计与实现,LW+源码+讲解
java·论文阅读·spring boot·后端·vue
灰灰的辉43 分钟前
统信操作系统离线安装JDK、Nginx、elasticsearch、kibana、ik、pinyin
java·开发语言·elasticsearch