feign:声明式服务调用

1.feign是springcloud提供的声明式(接口)的http客户端(工作在consumer端)
        feign支持springmvc注解
        feign支持负载均衡(集成了ribbon)

2.启动器:依赖

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

3.案例搭建

提供者,feign接口,消费者

feign-provider:

controller :/provider/getUserById/{id}

serviceImpl:return new User(id,"王铁柱-provider",18)

application.yml

server:
  port: 8083
spring:
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.83.131:8848
  application:
    name: feign-provider

feign-interface

@FeignClient("feign-provider")
@RequestMapping("/provider")
public interface UserFeign {

    @RequestMapping("/getUserById/{id}")
    public User getUserById(@PathVariable("id") Integer id);
}

@feignclient必须填写一个值,不然无法启动.该值调用的服务名。

feign-consumer:

@Autowired
    private UserFeign userFeign;

    @RequestMapping("/getUserById/{id}")
    public User getUserById(@PathVariable Integer id){
        return userFeign.getUserById(id);
    }

feign-consumer得启动类上一定要加:@EnableFeignClients

application.yml

server:
  port: 8084
spring:
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.83.131:8848
  application:
    name: feign-consumer

4.测试

启动mysql和nacos

 5.feign原理

首先将feign接口注入spring容器:consumer得启动类上的注解@EnableFeignClients在启动时,会调用方法FeignClientsRegistrar.registerFeignClients()扫描@FeignClient注解的接口,再将这些接口注入到Spring IOC容器中,方便后续被调用。

然后requestTemplate封装请求信息:SynchronousMethodHandler.invoke(): 当定义的的Feign接口中的方法被调用时,通过JDK的代理方式为Feign接口生成了一个动态代理类,当生成代理时,Feign会为每个接口方法创建一个RequestTemplate。该对象封装了HTTP请求需要的全部信息,如请url、参数,请求方式等信息都是在这个过程中确定的。

最后发送请求:SynchronousMethodHandler.executeAndDecode():通过RequestTemplate生成Request,然后把Request交给Client去处理,Client可以是JDK原生的URLConnection,Apache的HttpClient,也可以时OKhttp,最后Client结合Ribbon负载均衡发起服务调用。

6.feign传参:feign_interface工程中接口传递参数的方式。必须加注解而且注解中必须有值

这个值代表了发送请求之后参数的key。

    1、restful传参
        @PathVariable("id")
    2、?传参:也包括了数组
        @RequestParam("id")
    3、pojo:也包括了,map,set,list
        @RequestBody

7.feign优化:

在application.yml中开启feign日志,log4j也必须存在
        feign:
          client:
            config:
              #default:
              feign-provider:
                loggerLevel: full #输出feign远程调用的详细信息
        logging:
          level:
            com.bjpowernode.feign: debug #log4j的日志级别

7.1:feign优化--日志级别:

        loggerLevel: full 打印全部日志,会降低feign得性能。日志等级开启为basic即可。

7.2:feign优化--http连接池:

        两个主机建立连接的过程是很复杂的一个过程,涉及到多个数据包的交换,并且也很耗时间。使用http连接池可以提高性能。

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

7.3:feign优化--gzip压缩:

gzip压缩文本之后进行网络传输,网络数据经过压缩后实际上 降低了网络传输的字节数 ,最明显的好处就是可以 加快网页加载的速度 

      server:
          port: 80
          compression:
            enabled: true #开启浏览器<---->consumer的gzip压缩
        feign:
          compression: #开启feign<---->provider的gzip压缩
            request:
              enabled: true
            response:
              enabled: true

7.4:设置feign超时时间:

feign默认超时时间时一秒,如果遇到网络阻塞或者线程休眠,会出现timeoutexception,影响程序的正常运行能降低效率。

方式一:  hystrix结合ribbon

hystrix:
  command:
    default:
      execution:
        timeout:
          enabled: true
        isolation:
          thread:
            timeoutInMilliseconds: 30000
#ribbon的超时时间
ribbon:
  ReadTimeout: 30000
  ConnectTimeout: 30000


  方式二:

            feign:
              client:
                config:
                  #default:
                  feign-provider:
                    ConnectionTimeout: 5000 #请求连接的超时时候
                    ReadTimeout: 5000 #请求响应的超时时间

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值