OpenFeign服务接口调用的过程详解_第1页
OpenFeign服务接口调用的过程详解_第2页
OpenFeign服务接口调用的过程详解_第3页
OpenFeign服务接口调用的过程详解_第4页
OpenFeign服务接口调用的过程详解_第5页
已阅读5页,还剩8页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第OpenFeign服务接口调用的过程详解目录OpenFeign服务接口调用1、概述2、OpenFeign使用步骤3、OpenFeign超时控制4、OpenFeign日志打印功能

OpenFeign服务接口调用

1、概述

Feign是一个声明式WebService客户端。使用Feign能让编写WebService客户端更加简单。它的使用方法是定义一个服务接口然后在上面添加注解。

Feign也支持可拔插式的编码器和解码器。SpringCloud对Feign进行了封装,使其支持了SpringMVC标准注解和HttpMessageConverters。

Feign可以与Eureka和Ribbon组合使用以支持负载均衡

源码地址:/spring-cloud/spring-cloud-openfeign

Feign能干什么

Feign旨在使编写JavaHttp客户端变得更容易。前面在使用Ribbon+RestTemplate时,利用RestTemplate对http请求的封装处理,形成了一套模版化的调用方法。但是在实际开发中,由于对服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以通常都会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用。所以,Feign在此基础上做了进一步封装,由他来帮助我们定义和实现依赖服务接口的定义。在Feign的实现下,我们只需创建一个接口并使用注解的方式来配置它(以前是Dao接口上面标注Mapper注解,现在是一个微服务接口上面标注一个Feign注解即可),即可完成对服务提供方的接口绑定,简化了使用SpringcloudRibbon时,自动封装服务调用客户端的开发量。

Feign集成了Ribbon

利用Ribbon维护了Payment的服务列表信息,并且通过轮询实现了客户端的负载均衡。而与Ribbon不同的是,通过feign只需要定义服务绑定接口且以声明式的方法,优雅而简单的实现了服务调用

Feign和OpenFeign两者的区别

FeignOpenFeignFeign是SpringCloud组件中的一个轻量级RESTful的HTTP服务客户端

Feign内置了Ribbon,用来做客户端负载均衡,去调用服务注册中心的服务。Feign的使用方式是:使用Feign的注解定义接口,调用这个接口,就可以调用服务注册中心的服务OpenFeign是SpringCloud在Feign的基础上支持了SpringMVC的注解,如@RequesMapping等等。OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务。dependency

groupIdorg.springframework.cloud/groupId

artifactIdspring-cloud-starter-feign/artifactId

/dependencydependency

groupIdorg.springframework.cloud/groupId

artifactIdspring-cloud-starter-openfeign/artifactId

/dependency

2、OpenFeign使用步骤

1、创建接口和使用注解@FeignClient

2、新建模块cloud-consumer-feign-order80

3、修改pom.xml依赖文件

xmlversion="1.0"encoding="UTF-8"

projectxmlns="/POM/4.0.0"

xmlns:xsi="/2001/XMLSchema-instance"

xsi:schemaLocation="/POM/4.0.0/xsd/maven-4.0.0.xsd"

parent

artifactIdspringcloud2025/artifactId

groupIdcom.zcl.springcloud/groupId

version1.0-SNAPSHOT/version

/parent

modelVersion4.0.0/modelVersion

artifactIdcloud-consumer-feign-order80/artifactId

properties

piler.source8/piler.source

piler.target8/piler.target

/properties

dependencies

!--openfeign--

dependency

groupIdorg.springframework.cloud/groupId

artifactIdspring-cloud-starter-openfeign/artifactId

/dependency

!--eurekaclient--

dependency

groupIdorg.springframework.cloud/groupId

artifactIdspring-cloud-starter-netflix-eureka-client/artifactId

/dependency

!--引入自己定义的api通用包,可以使用Payment支付Entity--

dependency

groupIdcom.zcl.springcloud/groupId

artifactIdcloud-api-commons/artifactId

version1.0-SNAPSHOT/version

/dependency

!--web--

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-web/artifactId

/dependency

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-actuator/artifactId

/dependency

!--一般基础通用配置--

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-devtools/artifactId

scoperuntime/scope

optionaltrue/optional

/dependency

dependency

groupIdjectlombok/groupId

artifactIdlombok/artifactId

optionaltrue/optional

/dependency

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-test/artifactId

scopetest/scope

/dependency

/dependencies

/project

4、添加YAML配置文件

不将模块注册到Eureka服务中心

server:

port:80

eureka:

client:

register-with-eureka:false

service-url:

defaultZone::7001/eureka/,:7002/eureka/

5、创建启动类

既然使用的是OpenFeign那必须使用@EnableFeignClients注解进行开启

packagecom.zcl.springcloud;

importorg.springframework.boot.SpringApplication;

importorg.springframework.boot.autoconfigure.SpringBootApplication;

importorg.springframework.cloud.openfeign.EnableFeignClients;

*描述:Feign项目启动类

*@authorzhong

*@date2025-09-1913:32

@SpringBootApplication

@EnableFeignClients

publicclassOrderFeignMain80{

publicstaticvoidmain(String[]args){

SpringApplication.run(OrderFeignMain80.class,args);

}

6、业务类

业务逻辑接口+@FeignClient注解完成服务调用,找指定微服务的实例和调用地址,访问的就是8001对外暴露的地址

主启动类上必须使用@EnableFeignClients注解进行激活开启

packagecom.zcl.springcloud.service;

importcom.zcl.springcloud.entities.CommonResult;

importorg.springframework.cloud.openfeign.FeignClient;

importorg.springframework.stereotype.Component;

importorg.springframework.web.bind.annotation.GetMapping;

importorg.springframework.web.bind.annotation.PathVariable;

*描述:Feign业务测试接口

*@authorzhong

*@date2025-09-1913:35

@Component

@FeignClient(value="CLOUD-PAYMENT-SERVICE")

publicinterfacePaymentFeignService{

*根据id去调用

*@paramid

*@return

@GetMapping("/payment/get/{id}")

CommonResultgetPaymentById(@PathVariable("id")Longid);

}

上面定义的接口其实就是服务提供者对外暴露的接口

7、创建控制器

packagecom.zcl.springcloud.controller;

importcom.zcl.springcloud.entities.CommonResult;

importcom.zcl.springcloud.entities.Payment;

importcom.zcl.springcloud.service.PaymentFeignService;

importlombok.extern.slf4j.Slf4j;

importorg.springframework.web.bind.annotation.GetMapping;

importorg.springframework.web.bind.annotation.PathVariable;

importorg.springframework.web.bind.annotation.RestController;

importjavax.annotation.Resource;

*描述:测试控制器

*@authorzhong

*@date2025-09-1914:11

@Slf4j

@RestController

publicclassOrderFeignController{

*注入Feign业务接口

@Resource

privatePaymentFeignServicepaymentFeignService;

*测试调用服务接口

*@paramid

*@return

@GetMapping("/consumer/payment/get/{id}")

publicCommonResultPaymentgetPayment(@PathVariable("id")Longid){

//调用服务接口

returnpaymentFeignService.getPaymentById(id);

8、启动项目测试

先启动7001、7002Eureka集群再启动8001、8002两个微服务提供者启动OpenFeign项目80调用接口访问http://localhost/consumer/payment/get/1

多请求两回会发现一样的有轮询负载均衡的效果

Feign自带负载均衡配置项

{

"code":200,

"message":"查询成功,当前提供者端口号为:8002",

"data":{

"id":1,

"serial":"1515154151515"

3、OpenFeign超时控制

1、在8001提供者上添加一个新的控制器

/**

*测试Feign程序访问停止三秒

*@return

@GetMapping("/feign/timeout")

publicStringpaymentFeignTimeOut(){

try{

//程序停止三秒

TimeUnit.SECONDS.sleep(3);

}catch(InterruptedExceptione){

e.printStackTrace();

returnserverPort;

2、在消费者Feign80上添加接口

packagecom.zcl.springcloud.service;

importcom.zcl.springcloud.entities.CommonResult;

importorg.springframework.cloud.openfeign.FeignClient;

importorg.springframework.stereotype.Component;

importorg.springframework.web.bind.annotation.GetMapping;

importorg.springframework.web.bind.annotation.PathVariable;

*描述:Feign业务测试接口

*@authorzhong

*@date2025-09-1913:35

@Component

@FeignClient(value="CLOUD-PAYMENT-SERVICE")

publicinterfacePaymentFeignService{

*测试请求三秒不响应接口

*@return

@GetMapping("/payment/feign/timeout")

StringpaymentFeignTimeOut();

3、在FeignOrder80消费者控制器上添加接口访问

/**

*注入Feign业务接口

@Resource

privatePaymentFeignServicepaymentFeignService;

*测试超时

*@return

@GetMapping("/payment/feign/timeout")

publicStringpaymentFeignTimeOut()

returnpaymentFeignService.paymentFeignTimeOut();

4、启动项目测试

先自测访问提供者的业务是否正常:http://localhost:8001/payment/feign/timeout(等待三秒再响应结果)通过Feign-Order80消费者的接口测试调用提供者的访问是否正常:http://localhost/consumer/payment/feign/timeout

默认连接是1秒钟,程序设置了三秒导致连接超时

后端超时报错

5、在YAML配置文件中开启超时连接

设置配置文件下面的ribbon连接时间后等待三秒访问正常不会再报错

server:

port:80

eureka:

client:

register-with-eureka:false

service-url:

defaultZone::7001/eureka/,:7002/eureka/

#设置feign客户端超时时间(OpenFeign默认支持ribbon)

ribbon:

#指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间

ReadTimeout:5000

#指的是

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论