当前位置:网站首页>Openfeign service interface call
Openfeign service interface call
2022-06-27 12:53:00 【RB_ VER】
summary
Feign It's a statement WebService client . Use Feign Can make writing WebService The client is simpler .
It's used by defining a service interface and then adding annotations to it .Feign It also supports pluggable encoders and decoders .Spring Cloud Yes Feign It was packaged , To support Spring MVC Standard notes and HttpMessageConverters.Feign It can be done with Eureka and Ribbon Combined to support load balancing .
One sentence summary :Feign It's a declarative one Web Service client , Let's write web Service clients become very easy , Just create an interface and annotate it .
Feign Designed to make writing Java Http The client becomes easier .
Use Ribbon+RestTemplate when , utilize RestTemplate Yes Http Encapsulation processing of requests , It forms a set of template calling methods , But in actual development , Because there may be more than one call to a service , Often an interface is called in multiple places , So we usually encapsulate some client classes for each microservice to package the calls of these dependent services . therefore ,Feign On this basis, we make further encapsulation , It helps us define and implement the definition of dependent service interface . stay Feign Under the realization of , We just need to create an interface and use annotations to configure it , You can complete the interface binding to the service provider , Simplified use spring cloud ribbon when , Development volume of auto encapsulation service calling client .
Feign Integrated Ribbon, adopt Feign Just define the service binding interface and use declarative methods , Elegant and simple implementation of service invocation .
Feign Has been OpenFeign replace .
| Feign | OpenFeign |
|---|---|
| Feign yes spring cloud One of the lightweight components RESTful Of HTTP Service client Feign Built in Ribbon, It is used for client load balancing , To call the service in the service registry .Feign Is used in the following way : Use Feign The annotation defines the interface , Call this interface , You can call the service in the service registry . | OpenFeign yes spring cloud stay Feign Based on the support of SpringMVC Annotations , Such as @RequestMapping wait .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 . |
Use steps
newly build cloud-consumer-feign-order80 modular .
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud2022</artifactId>
<groupId>com.qrxqrx.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-consumer-feign-order80</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.qrxqrx.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
</dependencies>
</project>
application.yml
server:
port: 80
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
OrderFeignMain80
package com.qrxqrx.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class OrderFeignMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderFeignMain80.class,args);
}
}
PaymentFeignService
package com.qrxqrx.springcloud.service;
import com.qrxqrx.springcloud.entities.CommonResult;
import com.qrxqrx.springcloud.entities.Payment;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {
@GetMapping(value = "/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);
}
OrderFeignController
package com.qrxqrx.springcloud.controller;
import com.qrxqrx.springcloud.entities.CommonResult;
import com.qrxqrx.springcloud.entities.Payment;
import com.qrxqrx.springcloud.service.PaymentFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@Slf4j
public class OrderFeignController {
@Resource
private PaymentFeignService paymentFeignService;
@GetMapping(value = "/consumer/payment/get/{id}")
public CommonResult<Payment> getPaymenyById(@PathVariable("id") Long id) {
return paymentFeignService.getPaymentById(id);
}
}
OpenFeign Timeout control
Default feign The client just waits 1 second , But the server-side processing needs more than 1 second , Lead to feign The client doesn't want to wait , Direct return error . To avoid such a situation , Sometimes you need to set up feign Timeout control of client .
server:
port: 80
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
# Set up feign Client timeout
ribbon:
# It refers to the time taken to read the available resources from the server after the connection is established
ReadTimeout: 5000
# The time taken to establish a connection , It is suitable for normal network conditions , The time taken to connect the two ends
ConnectTimeout: 5000
OpenFeign Log printing function
Feign Provides log printing function , The log level can be adjusted through configuration , Thus understand Feign in Http Details of the request . Yes Feign Monitor and output the interface call .
The level of logging :
- NONE: default , Don't show any logs .
- BASIC: Only the request method is recorded 、URL、 Response status code and execution time .
- HEADERS: except BASIC In addition to the information defined in , And the request and response headers .
- FULL: except HEADERS In addition to the information defined in , And the body and metadata of the request and response .
Add configuration class :
FeignConfig
package com.qrxqrx.springcloud.config;
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeignConfig {
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
Modify the configuration :
application.yml
server:
port: 80
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
# Set up feign Client timeout
ribbon:
# It refers to the time taken to read the available resources from the server after the connection is established
ReadTimeout: 5000
# The time taken to establish a connection , It is suitable for normal network conditions , The time taken to connect the two ends
ConnectTimeout: 5000
logging:
level:
# feign At what level does the log monitor which interface
com.qrxqrx.springcloud.service.PaymentFeignService: debug
边栏推荐
- 关闭windows defender安全中心的方法
- Interview shock 60: what will cause MySQL index invalidation?
- Tidb 6.0: making Tso more efficient tidb Book rush
- OpenFeign服务接口调用
- Stack calculation (whether the order of entering and leaving the stack is legal) - Code
- Industry insight - how should brand e-commerce reshape growth under the new retail format?
- Configuration management center of microservices
- 阿胖的操作记录
- Script defer async mode
- 让学指针变得更简单(二)
猜你喜欢

How to participate in openharmony code contribution

AI for Science: scientific research paradigm, open source platform and industrial form

It is so simple to remove the payment restrictions on VIP, YuQue and Zhihu in Baidu Library

Bluetooth health management device based on stm32

这是什么空调?
Interview shock 60: what will cause MySQL index invalidation?

Three traversal methods of binary tree

Topic37——64. Minimum path sum

Pycharm in Chinese

【TcaplusDB知识库】TcaplusDB-tcapulogmgr工具介绍(一)
随机推荐
SSH workflow and principle
JSON.stringify用法
MySQL learning 1: installing MySQL
Airbnb复盘微服务
剑指 Offer 04. 二维数组中的查找
不一样的习惯
uniapp下拉弹层选择框效果demo(整理)
picocli-入门
带你认识图数据库性能和场景测试利器LDBC SNB
消息隊列的使用
OpenFeign服务接口调用
中证500股指期货怎么开户,国内正规的股指期货平台有哪些,在哪里开户最安全?
浅谈珂朵莉树
Esp32s3 iperf routine test esp32s3 throughput test
MySQL high level statements (I)
Failed to execute NPM instruction, prompting ssh: Permission denied
GCC compiling dynamic and static libraries
动态规划
word文本框换页
log4j.properties的配置详解