Feign是Netflix开发的声明式、模板化的HTTP客户端, Feign可以帮助我们更快捷、优雅地实现微服务之间的调用。
1.feign采用的是基于接口的注解;
2.feign整合了ribbon,具有负载均衡的能力;
3.整合了Hystrix,具有熔断的能力。
服务消费者content-center(内容中心微服务)向服务提供者user-center(用户中心微服务)发送请求,获取用户的微信昵称,我们通过Feign来实现该需求。
请求路径:http://localhost:8082/user/1
响应的json数据:
{"id":1,"wxId":"aaa","wxNickname":"大坏蛋","roles":"admin","avatarUrl":"xxx","createTime":"2022-01-14T02:10:02.000+0000","updateTime":"2022-01-14T02:10:02.000+0000","bonus":100}
org.springframework.cloud
spring-cloud-starter-openfeign
io.github.openfeign
feign-httpclient
package personal.qin.contentcenter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class ContentCenterApplication {
public static void main(String[] args) {
SpringApplication.run(ContentCenterApplication.class, args);
}
}
package personal.qin.contentcenter.feignclient;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import personal.qin.contentcenter.domain.dto.UserDTO;
//Feign访问微服务的名称
@FeignClient(name = "user-center")
public interface UserCenterFeignClient {
/**
* Feign会自动构建URL为http://user-center/user/{id}
* @param id
* @return
*/
@GetMapping("/user/{id}")
UserDTO findById(@PathVariable Integer id);
}
package personal.qin.contentcenter.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import personal.qin.contentcenter.domain.dto.UserDTO;
import personal.qin.contentcenter.feignclient.UserCenterFeignClient;
@RestController
@RequestMapping("/content")
public class ContentController {
@Autowired
private UserCenterFeignClient userCenterFeignClient;
@GetMapping("/{id}")
public UserDTO findById(@PathVariable Integer id){
return userCenterFeignClient.findById(id);
}
}
访问UserController可以通过feign顺利调用到user-center中的方法
http://localhost:8012/content/1
级别 | 打印内容 |
NONE(默认值) | 不记录任何日志 |
BASIC | 仅记录请求方法、URL、请求状态码以及执行时间 |
HEADERS | BASIC级别的基础之上,记录请求和响应的header |
FULL | 记录请求和响应的header、body和元数据 |
在application.yml中结合log添加属性
#设置日志输出级别
logging:
level:
com.itmuch.contentcenter.feignclient.UserCenterFeignClient: debug
feign:
client:
config:
#想要调用的微服务的名称
user-center:
loggerLevel: full
配置项 | 作用 |
Logger.Level | 指定日志级别 |
Retryer | 指定重试策略 |
ErrorDecoder | 指定错误解码器 |
Request.Options | 超时时间 |
Collection |
拦截器 |
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
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();
}
@Autowired
private TestBaiduFeignClient testBaiduFeignClient;
@GetMapping("baidu")
public String baiduIndex(){
return testBaiduFeignClient.index();
}
角度 | RestTemplate | Feign |
可读性、可维护性 | 一般 | 极佳 |
开发体验 | 欠佳 | 极佳 |
性能 | 很好 | 中等(RestTemplate的50%左右,但是可以使用连接池将性能提升15%所有) |
灵活性 | 极佳 | 中等(内置功能可以满足绝大多数需求) |
pom.xml中添加依赖
io.github.openfeign
feign-httpclient
application.yml中添加属性配置连接池
feign:
httpclient:
#让feign使用Apache httpclient做请求,而不是默认的urlconnection
enabled: true
#feign的最大连接数
max-connections: 200
#feign单个路径的最大连接数
max-connections-per-route: 50
feign:
client:
config:
#想要调用的微服务的名称或全局配置
default:
loggerLevel: basic
参考:https://www.imooc.com/article/289005
(代码)SpringCloud第07讲:Http模板化客户端Feign
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.5.RELEASE
personal.qin
content-center
0.0.1-SNAPSHOT
content-center
content-center
1.8
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
tk.mybatis
mapper-spring-boot-starter
2.1.5
org.projectlombok
lombok
1.18.8
provided
org.springframework.cloud
spring-cloud-starter-alibaba-nacos-discovery
org.springframework.cloud
spring-cloud-starter-openfeign
io.github.openfeign
feign-httpclient
org.junit.jupiter
junit-jupiter-api
5.5.0
test
org.springframework.cloud
spring-cloud-dependencies
Greenwich.SR1
pom
import
org.springframework.cloud
spring-cloud-alibaba-dependencies
0.9.0.RELEASE
pom
import
org.springframework.boot
spring-boot-maven-plugin
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.6
${basedir}/src/main/resources/generator/generatorConfig.xml
true
true
mysql
mysql-connector-java
8.0.16
tk.mybatis
mapper
4.1.5