当前位置:网站首页>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
边栏推荐
- SQL injection vulnerability (type chapter)
- 金仓KFS数据集中场景(多对一)部署
- SystemVerilog (XIII) - enumerate data types
- Niuke.com: host scheduling
- GC
- Kingbasees plug-in DBMS of Jincang database_ RANDOM
- 金太阳教育美股上市:市值3.6亿美元 成小盘中概股
- How PHP extracts image addresses from strings
- 基于OpenStreetMap+PostGIS的地理位置系统 论文文档+参考论文文献+项目源码及数据库文件
- 查询法,中断法实现USART通信
猜你喜欢

Source code analysis of AQS & reentrantlock

CFCA安心签接入

基於Minifilter框架的雙緩沖透明加解密驅動 課程論文+項目源碼

一个数学难题,难倒两位数学家

What is the development history, specific uses and structure of the chip

基于OpenStreetMap+PostGIS的地理位置系统 论文文档+参考论文文献+项目源码及数据库文件

What are the ways to simulate and burn programs? (including common tools and usage)

c盘使用100%清理方法

杭州/北京内推 | 阿里达摩院招聘视觉生成方向学术实习生(人才计划)

SQL注入漏洞(类型篇)
随机推荐
Jincang database kingbasees plug-in force_ view
A program reflecting the characteristics of C language program structure
反应c语言程序结构特点的程序
Keywords serializable serialization and deserialization
JVM shutdown hook details
Big endian and little endian
Upload and modify the use of avatars
Wait (), notify (), notifyAll (), sleep (), condition, await (), signal()
Handler、Message、Looper、MessageQueue
wait()、notify()和notifyAll()、sleep()、Condition、await()、signal()
10.1. Oracle constraint deferred, not deferred, initially deferred and initially deferred
Démarrer avec Apache shenyu
Spark runs wordcount (case 1)
金仓数据库 KingbaseES 插件identity_pwdexp
c盘使用100%清理方法
Double tampon transparent cryptage et décryptage basé sur le cadre minifilter
Comparator(用于Arrays.sort)
动态规划解决股票问题(上)
记一次给OpenHarmony提交代码的过程
16 enterprise architecture strategies