当前位置:网站首页>微服务 - 远程调用(Feign组件)
微服务 - 远程调用(Feign组件)
2022-07-25 05:19:00 【从零开始的JAVA世界】
1. Feign简介
较RestTemplate相比,Feign提供更优雅的发送Http请求的功能。
public Order queryOrderById(Long orderId) {
// 1.根据订单id查询订单
Order order = orderMapper.findById(orderId);
//2.根据用户id查询用户信息
String url = "http://userserivce/user/" + order.getUserId(); //会变动,且不有优雅。
//远程调用
User user = restTemplate.getForObject(url,User.class).var;
return order;
}
2. 使用Feign代替RestTemplate
- 引入依赖坐标
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
- 开启注解
@SpringCloudApplication
@EnableFeignClients //开启OpenFeign注解
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
- 创建接口、声明远程调用的路径
@FeignClient("userservice") //service id
public interface UserClient {
@GetMapping("/user/{id}")
User findById(@PathVariable("id") Long id);
}
- 使用FeignClient中定义的方法代替RestTemplate
User user = userClient.findById(order.getUserId());
3. Feign配置
| 类型 | 作用 | 说明 |
|---|---|---|
| feign.Logger.Level | 修改日志级别 | 包含四种不同的级别:NONE、BASIC、HEADERS、FULL |
| feign.codec.Decoder | 响应结果的解析器 | http远程调用的结果做解析,例如解析json字符串为java对象 |
| feign.codec.Encoder | 请求参数编码 | 将请求参数编码,便于通过http请求发送 |
| feign. Contract | 支持的注解格式 | 默认是SpringMVC的注解 |
| feign. Retryer | 失败重试机制 | 请求失败的重试机制,默认是没有,不过会使用Ribbon的重试 |
一般情况下,默认值就能满足我们使用,如果要自定义时,只需要创建自定义的@Bean覆盖默认Bean即可。
日志的级别分为四种:
- NONE:不记录任何日志信息,这是默认值。
- BASIC:仅记录请求的方法,URL以及响应状态码和执行时间
- HEADERS:在BASIC的基础上,额外记录了请求和响应的头信息
- FULL:记录所有请求和响应的明细,包括头信息、请求体、元数据。
方式一:配置文件
基于配置文件修改feign的日志级别:
feign:
client:
config:
userservice: # 针对某个微服务的配置
loggerLevel: FULL # 日志级别
feign:
client:
config:
default: # 这里用default就是全局配置,如果是写服务名称,则是针对某个微服务的配置
loggerLevel: FULL # 日志级别
4. Feign使用优化
Feign底层发起http请求,依赖于其它的框架。其底层客户端实现包括:
URLConnection:默认实现,不支持连接池
Apache HttpClient :支持连接池
OKHttp:支持连接池
因此提高Feign的性能主要手段就是使用连接池代替默认的URLConnection。
使用 Apache HttpClient
- 引入依赖
<!--Apache HttpClient的依赖 -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
- 配置连接池
feign:
client:
config:
userservice: # 针对某个微服务的配置
loggerLevel: BASIC # 日志级别
httpclient: #配置连接池
enabled: true # 开启feign对HttpClient的支持
max-connections: 200 # 最大的连接数
max-connections-per-route: 50 # 每个路径的最大连接数
将Feign接口抽到到一个模块中
- 创建一个新模块,专门保存Feign接口,引入依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>

2. 使用时开启注解,添加扫描路径
@SpringCloudApplication
@EnableFeignClients(basePackages = {
"cn.xin.feign.clients"})
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
边栏推荐
- 自己实现is_class
- [live review] AI customer service "changes according to the situation", and man-machine dialogue can be easier
- Document collaboration tool recommendation
- STL notes (IV): String
- Leetcode55. Jumping game
- RHCE first day
- Sword finger offer II 014. anagrams in strings
- Permanent magnet synchronous motor 36 question (1) -- what is the difference between salient pole motor and salient pole motor?
- 如何判断是否遭到DDOS攻击
- "Niuke | daily question" inverse Polish expression
猜你喜欢

Unity LOD

Implement is by yourself_ base_ of

I have seven schemes to realize web real-time message push, seven!

初步了解Panda3d粒子系统

How to carry out the function test with simple appearance and complex interior?

Panda3D keyboard moving scene
[email protected]研发效能度量指标"/>学习记录[email protected]研发效能度量指标
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]

The 6th "Blue Hat Cup" National College Students' Cyber Security Skills Competition writeup

When image component in wechat applet is used as background picture
随机推荐
The second day of rhcsa summer vacation
[globally unique ID] how to handle the ID primary key after dividing the database and table?
VPP不能加载UP状态接口
Solve the problem that uni app applet obtains routes and route parameters
four hundred and forty-four thousand one hundred and forty-one
harbor安装
Special analysis of data security construction in banking industry
Implement is by yourself_ class
Pikachu vulnerability platform exercise
Oracle split branches
一篇文章带你读懂Redis的哨兵模式
Guanghetong and Intel released the global version of 5g communication module
Androd releases jitpack open source project (gradle7.2)
JS common code questions array de duplication - Custom New throttling and anti shake - deep copy - instanceof URL parameter extraction - thousand separator - array to tree structure - array flattening
自己实现is_convertible
How to judge whether it is attacked by DDoS
自己实现is_base_of
Purpose of setting novice task period in the integral system
2022-7-13 summary
STM32 development note 120: solve the problem that%f in printf cannot be output