当前位置:网站首页>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
边栏推荐
- 和动态规划的第一次相遇
- Word text box page feed
- VS调试技巧
- Centos7命令行安装Oracle11g
- Full explanation of ThreadLocal source code (threadlocalmap)
- Dm8: Dameng database - lock timeout
- OpenFeign服务接口调用
- convn-N 维卷积
- Cloud native (30) | kubernetes' app store Helm
- Local visualization tool connects to redis of Alibaba cloud CentOS server
猜你喜欢
PyCharm汉化
这是什么空调?
[medical segmentation] unet3+
word文本框换页
Tidb 6.0: making Tso more efficient tidb Book rush
Two TCP flow control problems
uni-app开发微信小程序动态渲染页面,动态改变页面组件模块顺序
【TcaplusDB知识库】TcaplusDB-tcapsvrmgr工具介绍(三)
[fans' welfare] today, I'd like to introduce a method to collect money for nothing - convertible bonds. I personally verified that each person can earn 1500 yuan a year
Interview shock 60: what will cause MySQL index invalidation?
随机推荐
Steps for win10 to completely and permanently turn off automatic updates
Airbnb double disk microservice
[tcapulusdb knowledge base] Introduction to tcapulusdb tcapsvrmgr tool (II)
Centos7命令行安装Oracle11g
MySQL high level statements (I)
如何修改 node_modules 里的文件
带你认识图数据库性能和场景测试利器LDBC SNB
PyCharm汉化
How to modify a node_ Files in modules
本地可视化工具连接阿里云centOS服务器的redis
Ssh server configuration file sshd_ Config and operation
Pycharm in Chinese
log4j.properties的配置详解
convn-N 维卷积
log4j的详情配置
二叉树的三种遍历方式
今天运气不错
Failed to execute NPM instruction, prompting ssh: Permission denied
build. Gradle configuration
nifi从入门到实战(保姆级教程)——身份认证