OpenFegin整理篇

openFegin引入

官网:https://docs.spring.io/spring-cloud-openfeign/docs/3.1.4/reference/html/
github:
需要引入的关键配置,使用eureka做了配置中心,openfegin 使用的是3.1.4

 <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-openfeign</artifactId>
  </dependency>
  <dependency>
     <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  </dependency>

openFegin和fegin之间的区别

fegin不再维护的情况下,spring团队自己维护了一个openFegin,并且支持springmvc等注解;

配置

超时时间配置

feign:
  client:
    config:
      default: #默认情况下的配置
        connect-timeout: 1000 #单位毫秒
        read-timeout: 5000 #单位毫秒
      openfeign-2011-1: #这里对应规则有contextId的对应contextId,无contextId的对应name或者value, 有openfeign-2011-1配置的情况下,openfeign-2011-1配置会覆盖default配置
        connect-timeout: 1000 #单位毫秒
        read-timeout: 10000 #单位毫秒
      openfeign-2011-2:
        connect-timeout: 1000 #单位毫秒
        read-timeout: 10000 #单位毫秒

目前仅这样配置生效,当然没有尝试代码配置;

重试机制

#重试机制
feign:
  client:
    config:
#      default: # 全局生效
#        retryer: com.itchun.retryer.DefaultFeignRetry
      openfeign-2011-2: # 指定 【openfeign-2011-2】
        retryer: com.itchun.retryer.FeginRetry2011_2

需要指定 retryer 配置类(com.itchun.retryer.FeginRetry2011_2)的地址:

package com.itchun.retryer;

import feign.RetryableException;
import feign.Retryer;

public class FeginRetry2011_2 implements Retryer {

    @Override
    public void continueOrPropagate(RetryableException e) {
        throw e;
    }

    @Override
    public Retryer clone() {

        /**
         * period:周期,重试间隔时间,毫秒
         * maxPeriod:最大周期,重试间隔时间按照一定的规则逐渐增大,但不能超过最大周期
         * maxAttempts:最大尝试次数,重试次数,这里2次,是指加上本身主动请求的一次,重试一次,一共两次,所以这里设置1的话,其实就不会重试
         */
        return new Default(5000, Integer.MAX_VALUE, 3);
    }
}

日志配置

feign:
  client:
    config:
      #      defalut: # defalut配置的未生效,全局的使用FeginLoggerConfig配置可以生效
      #        logger-level: FULL
      openfeign-2011-1: # 可用
        logger-level: FULL

logging:
  level:
    com.itchun.feignClient.ProductSuccessFeign: debug

#NONE: 没有日志记录
#BASIC: 日志仅记录请求方法、url、响应状态和执行时间
#HEADERS: 日志记录基本信息(同BASIC)以及请求头和响应头信息
#FULL: 日志记录请求和响应的头(headers)、体(body)、元数据(metadata)信息

如果要进行全局配置,发现使用 feign.client.config.defalut 不生效,需要使用如下代码:

package com.itchun.logger;

import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置全局可以用
 */
@Configuration
public class FeginLoggerConfig {

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

负载均衡

  • Spring Cloud 2020版本以后,默认移除了对Netflix的依赖,其中就包括Ribbon,官方默认推荐使用Spring Cloud Loadbalancer正式替换Ribbon,并成为了Spring Cloud负载均衡器的唯一实现。
  • 默认是轮询机制
  • 还有根据xx算法的随机机制,待深入了解

熔断(hystrix)

涉及到fallback ,fallbackFactory 的配置,待了解

客户端

默认HttpURLConnection

在不引入 httpclient 工具情况下,默认使用 httpURLConnection,

配置 httpClient

听说只要引入 httpclient 配置就默认使用了 httpClient

<dependency>
     <groupId>io.github.openfeign</groupId>
     <artifactId>feign-httpclient</artifactId>
     <version>12.0</version>
 </dependency>

配置如下

#设置了。超时时间啊,都没有什么卵子用,但是确实使用httpclient了
feign:
  httpclient: #httpclient客户端配置(全局)
    enabled: true
    connection-timeout: 1000 #连接超时时间(单位:毫秒)
    time-to-live: 900 #线程存活时间(单位:秒)
    max-connections: 200 #线程池最大连接数(全局)
    max-connections-per-route: 50 #线程池最大连接数(单个HOST)

配置 okHttp

去除httpClient的pom配置,引入okHttp配置,这里没有指定版本,因为发现手动指定版本如果不对的话,发现会报classNotFound的错误,所以索性就直接使用springcloud默认指定的版本,我这里看了一下是11.8

<dependency>
      <groupId>io.github.openfeign</groupId>
      <artifactId>feign-okhttp</artifactId>
  </dependency>

配置

#设置了,okhttp启用了,但是设置的超时时间啊,都没起作用
feign:
  okhttp:
    enabled: true
  httpclient:
    enabled: false
    connection-timeout: 10000 #连接超时时间(单位:毫秒)
    ok-http:
      read-timeout: 12

项目参考地址:https://github.com/itchun/springcloud/tree/master/openfeign

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenFeign 是一个声明式的 Web Service 客户端,它使得编写 Web Service 客户端变得更加简单。它的主要目的是使得将微服务之间的调用变得更加容易。 在 Spring Cloud 应用中,我们经常需要使用 Ribbon 和 Feign 进行服务调用,其中 Feign 封装了 Ribbon,使得我们在使用 Feign 时可以直接使用 Ribbon 的负载均衡能力。使用 Feign 可以让我们更加方便地调用 RESTful 服务。 下面是一个基本的 OpenFeign 示例: 1. 首先需要在 pom.xml 中添加以下依赖: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` 2. 然后在启动类上添加 `@EnableFeignClients` 注解开启 Feign 功能: ``` @SpringBootApplication @EnableFeignClients public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 3. 创建一个 Feign 接口: ``` @FeignClient(name = "service-provider") public interface HelloService { @GetMapping("/hello") String hello(); } ``` 其中 `@FeignClient` 注解用来指定服务提供者的名称,`@GetMapping` 注解用来指定要调用的服务接口。 4. 在需要调用服务的地方注入 `HelloService` 接口即可: ``` @Service public class ConsumerService { @Autowired private HelloService helloService; public String hello() { return helloService.hello(); } } ``` 这样就可以通过 Feign 调用服务提供者的接口了。需要注意的是,Feign 默认使用的是 Spring MVC 的注解和 HttpMessageConverters,因此需要保证服务提供者和服务消费者使用的是相同的注解和 HttpMessageConverters。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值