当前位置:网站首页>Feign remote call
Feign remote call
2022-06-26 18:33:00 【cc_ nanke dream】
1、 brief introduction
Feign yes Netflix Developed ⼀ It's a lightweight RESTful Of HTTP Service client (⽤ It makes requests , Remote dispatching ⽤ Of ), In order to Java Pick up ⼝ Annotated ⽅ It's the tone ⽤Http request ,⽽ No ⽤ image Java Through encapsulation HTTP Ask for the newspaper ⽂ Of ⽅ Direct adjustment ⽤,Feign By ⼴ Pan Ying ⽤ stay Spring Cloud The solution of the ⽅ In the case .
Be similar to Dubbo, The service consumer gets the service provider's answer ⼝, And then it's like ⽤ Local connection ⼝⽅ Law ⼀ sample Detuning ⽤, It's actually a remote request
Feign Can help us more convenient , Elegant tone ⽤HTTP API: We don't need to splice url then Tweed tune ⽤restTemplate Of api, stay SpringCloud in , send ⽤Feign⾮ Often simple , establish ⼀ individual Pick up ⼝( In consumer -- Service transfer ⽤⽅ this ⼀ End ), And pick up ⼝ Add ⼀ Some notes , The code is complete 了
SpringCloud Yes Feign Into the ⾏ It enhances , send Feign⽀ Hold on SpringMVC annotation (OpenFeign)
The essence : Encapsulates the Http transfer ⽤ technological process , More in line with ⾯ To connect ⼝ The programming habit of Standardization , Be similar to Dubbo Service for transfer ⽤
2、 To configure
【1】:maven rely on
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>【2】: Start class enable ⽤ annotation @EnableFeignClients add to Feign⽀ a

【3】: establish Feign Pick up ⼝
package com.cc.gateway.feign;
import com.cc.common.dto.UserDto;
import com.cc.common.vo.ReturnVo;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author cc
* @data 2021 year 06 month 30 Japan 18:01
*/
// "project-oauth" The service name
@FeignClient(name = "project-oauth", fallback = OauthFallback.class)
public interface OauthFeign {
/**
* token verification
* @author cc
* @date 2021/6/30 22:59
* @param dto
* @return com.cc.common.vo.ReturnVo
*/
// "/oauth/checkToken" Call path
@RequestMapping("/oauth/checkToken")
public ReturnVo<Boolean> checkToken(@RequestBody UserDto dto);
}
3、 Load balancing
Feign It's integrated Ribbon Dependence and ⾃ Dynamic configuration , So we don't need to introduce ⼊ rely on , can In order to pass the ribbon.xx Come on in ⾏ Global configuration , You can also use the service name .ribbon.xx To enter... For the specified service ⾏ Detailed configuration
ribbon:
# Request connection timeout
ConnectTimeout: 3000
# Request processing timeout
ReadTimeout: 15000
# For all operations ⾏ retry
OkToRetryOnAllOperations: false
#### According to the above configuration , When accessing a fault request , It will try again to access ⼀ Second current instance ( The number of times by MaxAutoRetries To configure ),
#### If you don't ⾏, Just change it ⼀ This is an example ⾏ visit , If not ⾏, Change again ⼀ Instance access ( The replacement times are determined by MaxAutoRetriesNextServer To configure ),
#### If still not ⾏, Return failure information .
MaxAutoRetries: 0 # Number of retries for the currently selected instance , Excluding the ⼀ Secondary modulation ⽤
MaxAutoRetriesNextServer: 0 # Number of retries to switch instances
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule # Load policy adjustment 
4、 Fuse support
【1】: stay Feign client ⼯ Process configuration ⽂ Pieces of (application.yml) In the open Feign For fuses ⽀ a
# Turn on Feign Fusing function of
feign:
hystrix:
enabled: true
Hystrix timeout
hystrix:
command:
default:
execution:
isolation:
thread:
##########################################Hystrix It's super time ⻓ Set up
timeoutInMilliseconds: 15000
Be careful :
Turn on Hystrix after ,Feign Medium ⽅ The law will be entered ⾏⼀ A manager ,⼀ Once there is a problem, go into ⼊ Corresponding fallback logic processing
This is for timeout ⼀ spot , There are currently two timeout settings (Feign/hystrix), When the fuse is blown, it is a root According to the most ⼩ It's worth entering ⾏ Of , That is, when processing ⻓ The time-out exceeds the shortest time ⼊ Fallback demotion logic
⾃ Definition FallBack Processing class
package com.cc.gateway.feign;
import com.cc.common.dto.UserDto;
import com.cc.common.utils.ReturnVoUtil;
import com.cc.common.vo.ReturnVo;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
/**
* @author cc
* @data 2021 year 06 month 30 Japan 18:02
*/
@Component
public class OauthFallback implements OauthFeign {
@Override
public ReturnVo<Boolean> checkToken(@RequestBody UserDto dto) {
return ReturnVoUtil.success(" Please login again ", false);
}
}
5、Feign For request compression and response compression ⽀ a
Feign ⽀ Keep up with requests and responses ⾏GZIP Compress , To reduce the performance loss in the communication process . Through the next ⾯ Parameters of You can turn on the compression function of request and response :
feign:
compression:
request:
enabled: true # Turn on request compression
mime-types: text/html,application/xml,application/json # Set compressed data type , This is also the default
min-request-size: 2048 # Set the ⼤⼩ Lower limit , This is also the default
response:
enabled: true # Turn on response compression 边栏推荐
- 转:苹果CEO库克:伟大的想法来自不断拒绝接受现状
- 限流设计及实现
- 【Kubernetes】Kubernetes 原理剖析与实战应用(更新中)
- 微信小程序 uniapp 左滑 删除 带删除icon
- 刷新三观的HP-UX系统中的强指针赋值出core问题
- Crawl Douban to read top250 and import it into SqList database (or excel table)
- Redis单点登陆系统+投票系统
- Row lock analysis and deadlock
- MySQL download and configuration MySQL remote control
- DVD digital universal disc
猜你喜欢
随机推荐
Filebeat安装及使用
Get and set settings in 26class
50 lines of code to crawl TOP500 books and import TXT documents
JVM entry Door (1)
ARM裸板调试之串口打印及栈初步分析
System table SQLite of SQLite database_ master
Enter n integers and output the number of occurrences greater than or equal to half the length of the array
为什么我不推荐去SAP培训机构参加培训?
Yujun product methodology
Request method 'POST' not supported
sqlite数据库的系统表sqlite_master
[kubernetes] kubernetes principle analysis and practical application (under update)
Handwritten promise all
Usage and difference between ros:: spinonce() and ros:: spin()
Ethereum技术架构介绍
成功解决之微服务@Value获取配置文件乱码问题
CLion断点单步调试
sql 中的alter操作总结
Case study of row lock and isolation level
爬取豆瓣读书Top250,导入sqlist数据库(或excel表格)中







