0
点赞
收藏
分享

微信扫一扫

SpringCloud第07讲:Http模板化客户端Feign

潇湘落木life 2022-01-20 阅读 62

        Feign是Netflix开发的声明式、模板化的HTTP客户端, Feign可以帮助我们更快捷、优雅地实现微服务之间的调用。

一、项目编码

        服务消费者content-center(内容中心微服务)向服务提供者user-center(用户中心微服务)发送请求,获取用户的微信昵称,我们通过Feign来实现该需求。

1.1、在pom.xml中添加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-httpclient</artifactId>
</dependency>

1.2、在BootApplication中添加@EnableFeignClients注解

package com.itmuch.contentcenter;

import org.springframework.cloud.openfeign.EnableFeignClients;
...

@SpringBootApplication
@EnableFeignClients
public class ContentCenterApplication {
...
}

1.3、新建FeignClient实现Feign远程请求

package com.itmuch.contentcenter.feignclient;

import com.itmuch.contentcenter.domain.dto.user.UserDTO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(name = "user-center")//微服务的名称
public interface UserCenterFeignClient {
    /**
     * Feign会自动构建URL为http://user-center/users/{id}
     * @param id
     * @return
     */
    @GetMapping("/users/{id}")
    UserDTO findById(@PathVariable Integer id);
}

在Service中调用方法实现功能即可 

二、Feign自定义日志级别

级别打印内容
NONE(默认值)不记录任何日志
BASIC仅记录请求方法、URL、请求状态码以及执行时间
HEADERSBASIC级别的基础之上,记录请求和响应的header
FULL记录请求和响应的header、body和元数据

2.1、在application.yml添加属性

        在application.yml中结合log添加属性

#设置日志输出级别
logging:
  level:
    com.itmuch.contentcenter.feignclient.UserCenterFeignClient: debug
feign:
  client:
    config:
      #想要调用的微服务的名称
      user-center:
        loggerLevel: full

2.2、测试效果

三、Feign支持的配置项

配置项作用
Logger.Level指定日志级别
Retryer指定重试策略
ErrorDecoder指定错误解码器
Request.Options超时时间
Collection<RequestInterceptor>拦截器
SetterFactory用于设置Hystrix的配置属性,当Feign整合Hystrix时才会使用

 application.yml配置参考

四、多参数请求

        多参数可以使用@SpringQueryMap注解实现

package com.itmuch.contentcenter.feignclient;

import com.itmuch.contentcenter.domain.dto.user.UserDTO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(name = "user-center")
public interface TestUserCenterFeignClient {
    @GetMapping("/q")
    UserDTO query(@SpringQueryMap UserDTO userDTO);
}

 注意,在开发中可能会出现多个Feign接口访问同一个微服务的情况,解决方法:

在application.yml中添加属性配置

spring:
  #解决Feign多个Client接口指向同一个微服务出现异常的问题
  main:
    allow-bean-definition-overriding: true

五、脱离Nacos单独使用Feign

5.1、新建FeignClient,请求百度

package com.itmuch.contentcenter.feignclient;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(name = "baidu", url = "http://www.baidu.com")
public interface TestBaiduFeignClient {
    @GetMapping("")
    public String index();
}

5.2、在测试Controller中调用FeignClient方法

@Autowired
private TestBaiduFeignClient testBaiduFeignClient;

@GetMapping("baidu")
public String baiduIndex(){
    return testBaiduFeignClient.index();
}

5.3、测试效果

 六、RestTemplate和Feign对比

角度RestTemplateFeign
可读性、可维护性一般极佳
开发体验欠佳极佳
性能很好中等(RestTemplate的50%左右,但是可以使用连接池将性能提升15%所有)
灵活性极佳中等(内置功能可以满足绝大多数需求)

七、Feign的性能优化

7.1、使用连接池提升15%性能

pom.xml中添加依赖

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

application.yml中添加属性配置连接池

feign: 
  httpclient:
    #让feign使用Apache httpclient做请求,而不是默认的urlconnection
    enabled: true
    #feign的最大连接数
    max-connections: 200
    #feign单个路径的最大连接数
    max-connections-per-route: 50

7.2、将日志的级别设置为basic,不要使用full

feign:
  client:
    config:
      #想要调用的微服务的名称或全局配置
      default:
        loggerLevel: basic

八、Feign的常见问题总结

参考:https://www.imooc.com/article/289005


特别声明:本系列教程(SpringCloudAlibaba)参考自慕课网大目老师提供的网上视频课程,有需要的同学可以自行搜索学习 

举报

相关推荐

0 条评论