feign使用步骤:
a. 引入依赖:org.springframework.cloud
spring-cloud-starter-openfeign
b. 添加注解:启动类:@EnableFeignClients()
接口类:@FeignClient(“指定的访问的服务名称”)
例:
@FeignClient(“userservice”)
public interface UserClient {
@GetMapping(“/user/{id}”)
User findById(@PathVariable(“id”) Long id);
**}
2.自定义配置:日志
**
| 类型 | 作用 | 说明 |
|---|---|---|
| feign.Logger.Level | 修改日志级别 | 包含四种不同的级别:NONE、BASIC、HEADERS、FULL |
| feign.codec.Decoder | 响应结果的解析器 | http远程调用的结果做解析,例如解析json字符串为java对象 |
| feign.codec.Encoder | 请求参数编码 | 将请求参数编码,便于通过http请求发送 |
| feign. Contract | 支持的注解格式 | 默认是SpringMVC的注解 |
| feign. Retryer | 失败重试机制 | 请求失败的重试机制,默认是没有,不过会使用Ribbon的重试 |
对应的配置
feign:
client:
config:
userservice: # 针对某个微服务的配置 # 这里用default就是全局配置,如果是写服务名称,则是针对某个微服务的配置
** loggerLevel: FULL # 日志级别
- feign使用优化: feign底层发起http请求,依赖于其他的框架 ,其底层客户端实现包括:
a. URLConnection,默认的实现 不支持连接池
b. Apache HttpClient:支持连接池
c. OKhttp:支持连接池 **
feign:
client:
config:
default: # default全局的配置
loggerLevel: BASIC # 日志级别,BASIC就是基本的请求和响应信息
httpclient:
enabled: true # 开启feign对HttpClient的支持
max-connections: 200 # 最大的连接数
max-connections-per-route: 50
