Spring Cloud 之 Feign常用配置与基本使用

Spring Cloud 之 Feign

提示:本文章仅供参考(因为我也是刚学习)

Feign介绍

Feign是Spring Cloud中服务消费端的调用组件,通常与Ribbon,Hystrix等组合使用

创建Feign Client

构建模块选择依赖时选择Spring Cloud Discovery中的Eureka Discovery Client和Spring Cloud Routing中的OpenFeign

启动类添加注解

在启动类上添加Eureka客户端@EnableEurekaClient注解和Eeign客户端注解@EnableFeignClients

配置application.yml

基础配置

spring:
  application:
    name: eureka-feign-client

eureka:
  client:
    service-url:
      defaultZone: http://eurekaserver1:8761/eureka/,http://eurekaserver2:8760/eureka/,http://eurekaserver3:8759/eureka/

server:
  port: 8765

Feign常用配置
注意:在properties或yml中配置的优先级高于配置类中的配置

#Feign更换client(支持httpClient,httpURLConnection(默认),OKHttp) 只需要在pom中添加对应的依赖就好(添加了依赖还不行的话再在yml中配置)
#feign:httpclient:enabled:true 或 feign:okhttp:enabled:true

#feign client 基本配置 在yml或properties中的配置优先级高于config类中的配置,如果要修改配置优先级
#在yml或properties中配置feign.client.default-to-properties=false
#feign:
#  client:
#    config:
#      feignName:
#        connectTimeout: 5000
#        readTimeout: 5000
#        loggerLevel: full
#        errorDecoder: com.example.SimpleErrorDecoder
#        retryer: com.example.SimpleRetryer
#        requestInterceptors:
#          - com.example.FooRequestInterceptor
#          - com.example.BarRequestInterceptor
#        decode404: false
#        encoder: com.example.SimpleEncoder
#        decoder: com.example.SimpleDecoder
#        contract: com.example.SimpleContract


#关闭 Hystrix配置 feign: hystrix: enabled: false

#Feign请求压缩
#feign.compression.request.enabled=true
#feign.compression.response.enabled=true
#feign.compression.request.mime-types=text/xml,application/xml,application/json  #设置Feign请求mime类型
#feign.compression.request.min-request-size=2048   #设置最小请求大小
#logging.level.project.user.UserClient: DEBUG    #配置日志输出等级 NONE BASIC HEADERS FULL

Feign常用配置官方文档

测试Feign

(1)编写Feign配置类

@Configuration
public class FeignConfig {

	// period=100 发起当前请求的时间间隔,单位毫秒
    // maxPeriod=1000 发起当前请求的最大时间间隔,单位毫秒
    // maxAttempts=5 最多请求次数,包括第一次
    @Bean
    public Retryer feignRetryer(){
        return new Retryer.Default(100,SECONDS.toMillis(1),5);
    }
}

看一下源码

	public Default() {
      this(100, SECONDS.toMillis(1), 5);
    }

    public Default(long period, long maxPeriod, int maxAttempts) {
      this.period = period;
      this.maxPeriod = maxPeriod;
      this.maxAttempts = maxAttempts;
      this.attempt = 1;
    }

总结:所以我配置了个寂寞

(2)编写Feign接口

创建feign包,在包下创建TestFeign类

@FeignClient(value = "eureka-client",configuration = FeignConfig.class)
public interface EurekaClientFeign {

    @GetMapping(value = "/test/info")
    String testFromEurekaClient();
}

其中@FeignClient中value属性和name属性互通,configuration为你的feign配置类,还有一个contextId作为服务id

源码
源码

(3)编写service

创建service包,并编写TestService类

@Service
public class TestService {
    @Autowired
    EurekaClientFeign eurekaClientFeign;

    public String test(){
        return eurekaClientFeign.testFromEurekaClient();
    }
}

总结:服务消费端相当于用Client请求数据代替了以前的dao层从数据库获取数据

(4)编写controller

不想写了,自己写

(5)运行

运行结果发现居然有负载均衡的效果
结果一:I am from port:8763
结果二:I am from port:8762

原因:因为spring-cloud-starter-feign依赖中默认引入Ribbon依赖和Hystrix依赖

所以我们可以在properties或yml中配置负载均衡策略和基础的ribbon配置

总结

作为新手多看源码 多看官方文档最好!

OpenFeign官方文档


版权声明:本文为qq_49810363原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。