当前位置:网站首页>Openfeign uses
Openfeign uses
2022-06-25 11:30:00 【sermonlizhi】
One 、OpenFeign
1.1 Basic use
OpenFeign yes Spring Cloud stay Feign Based on the support of Spring MVC Annotations , Columns such as @RequestMapping etc. ,OpenFeign Of @FeignClient Can be parsed SpringMVC Of @RequestMapping Interface under annotation , And through the way of dynamic proxy to produce the implementation class , Load balancing in the implementation class and calling other services .
stay shop-common
Modular pom
Add... To the file OpenFeign
Dependency package
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
establish OpenFeign
Interface , Add... To the interface @FeignClient
annotation , adopt value
Property specifies the name of the service that provides the service , This name is registered to Nacos
Service name in ,path
Property to set the root path of interface access , In the interface method, you can use @RequestMapping
Annotation to specify the access path , The root path and the specified access path are assembled to be the real access path
If you need to transfer parameters , Can be like Controller
The receiving parameters are the same , adopt @RequestParm、@PathVariable
And so on
@Component
@FeignClient(value = "shop-order",path = "order")
public interface UserToOrderFeignClient {
/** * Order details * * @date 2022/2/8 * @param uid * @return java.util.List<com.lizhi.entity.Order> */
@GetMapping("details")
List<Order> details(@RequestParam("uid") Integer uid);
}
stay shop-order
In service , To write Controller
class , At the same time, add... On the startup class @EnableFeignClients
annotation , Indicates permission to pass OpenFeign
To access the interface
@RestController
@RequestMapping
public class OrderController {
@Resource
OrderService orderService;
@GetMapping("details")
List<Order> details(@RequestParam("uid") Integer uid){
return orderService.details(uid);
}
}
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class,args);
}
}
To configure shop-order
Root path of service access
spring:
mvc:
servlet:
path: /order
stay shop-user
Module Injection UserToOrderFeignClient
Proxy object of , Then you can directly call UserToOrderFeignClient Of details()
Method to call shop-order
Provided interface
@Resource
UserToOrderFeignClient userToOrderFeignClient;
@GetMapping("test")
List<Order> test(){
return userToOrderFeignClient.details(1);
}
1.2 Client parameter configuration
@FeignClient
Annotated configuration
Property to configure client parameters , The default configuration class is FeignClientsConfiguration
, It provides Encoder
、Decoder
、Logger
、Contract
、Retryer
、FeignClientConfigurer
Etc
We can configure these parameters through a custom configuration class , And then in @FeignClient
Configuration in comments configuration
attribute
@Configuration
public class FeignConfiguration {
@Bean
public Contract feignContract(){
...}
@Bean
public Encoder encoder(){
...}
……
}
@Component
@FeignClient(value = "shop-order",path = "order",fallback = OrderFeignClientFallBack.class,configuration = FeignConfiguration.class)
public interface UserToOrderFeignClient {
@GetMapping("details")
List<Order> details(@RequestParam("uid") Integer uid);
}
In addition to configuring classes , You can also configure through the configuration file ,feign.client.config
It's a Map, So you need to customize a KEY, The following example puts the service name shop-user
As KEY, Attributes correspond to FeignClientProperties
The inner class of FeignClientConfiguration
feign:
sentinel:
enabled: true
client:
config:
shop-user:
connectTimeout:
loggerLevel:
readTimeout: 5000
oggerLevel: full
encoder: com.lizhi.config.Encoder
decoder: com.lizhi.config.Decoder
contract: com.lizhi.config.Contract
1.3 Circuit breaker configuration
SpringCloud Circuit breaker support fallBack
The concept of , With Sentinel
For example , demonstration OpenFeign in fallBack
Use
First add... To the configuration file feign.sentinel.enabled=true
Parameters ,Spring The configured metadata describes this parameter in detail
{
"name": "feign.sentinel.enabled",
"type": "java.lang.Boolean",
"description": "If true, an OpenFeign client will be wrapped with a Sentinel circuit breaker.",
"defaultValue": "false"
}
We create a OrderFeignClientFallBack
class , Let it be UserToOrderFeignClient
This FeignClient The interface of , And define this class as a Bean
@Component
public class OrderFeignClientFallBack implements UserToOrderFeignClient {
@Override
public List<Order> details(Integer uid) {
throw new NoFallbackAvailableException("service degrade",new RuntimeException());
}
}
And then in FeignClient Interface @FeignClient
In the annotations , Appoint fallBack
The corresponding processing class
@Component
@FeignClient(value = "shop-order",path = "order",fallback = OrderFeignClientFallBack.class)
public interface UserToOrderFeignClient {
@GetMapping("details")
List<Order> details(@RequestParam("uid") Integer uid);
}
Reintegration Sentinel after , It can be done to /order/detail
Interface setting fusing rules , If the interface call triggers the fusing rule , Will call fallBack
Methods of the corresponding interface in the class
边栏推荐
- Démarrer avec Apache shenyu
- 金仓数据库 KingbaseES 插件DBMS_RANDOM
- 10.1. Oracle constraint deferred, not deferred, initially deferred and initially deferred
- 杭州/北京内推 | 阿里达摩院招聘视觉生成方向学术实习生(人才计划)
- Apache ShenYu 入門
- MySQL and Oracle processing CLOB and blob fields
- Introduction to socket UDP and TCP
- 金仓数据库 KingbaseES 插件force_view
- 基于C语言的图书信息管理系统 课程论文+代码及可执行exe文件
- Socket communication principle
猜你喜欢
记一次给OpenHarmony提交代码的过程
金仓KFS数据级联场景部署
At 16:00 today, Mr. sunxiaoming, a researcher of the Institute of computing, Chinese Academy of Sciences, took you into the quantum world
一个数学难题,难倒两位数学家
CMU提出NLP新范式—重构预训练,高考英语交出134高分
How to start the phpstudy server
Double tampon transparent cryptage et décryptage basé sur le cadre minifilter
Crawler scheduling framework of scratch+scratch+grammar
Use of Presto visualization client-yanagishima20.0
西山科技冲刺科创板:拟募资6.6亿 郭毅军夫妇有60%表决权
随机推荐
Ladder Side-Tuning:预训练模型的“过墙梯”
CMU puts forward a new NLP paradigm - reconstructing pre training, and achieving 134 high scores in college entrance examination English
Spark runs wordcount (case 2)
Spannable 和 Editable、SpannableString 和 SpannableString
金仓数据库 KingbaseES 插件DBMS_OUTPUT
wait()、notify()和notifyAll()、sleep()、Condition、await()、signal()
西山科技冲刺科创板:拟募资6.6亿 郭毅军夫妇有60%表决权
Spark tuning tool -- detailed explanation of sparklens
Introduction to socket UDP and TCP
relu与sigmod的比较
Bayes
A difficult mathematical problem baffles two mathematicians
Use of Presto visualization client-yanagishima20.0
[maintain cluster case set] gaussdb query user space usage
10.1. Oracle constraint deferred, not deferred, initially deferred and initially deferred
Shichuang energy rushes to the scientific innovation board: it plans to raise 1.1 billion yuan, with an annual revenue of 700million yuan and a 36% decrease in net profit
GC
Writing wechat applet with uni app
Geographic location system based on openstreetmap+postgis paper documents + reference papers + project source code and database files
Niuke: rotation array