使用Feign REST客户端进行服务调用

在本节中,我们将开始使用Spring Cloud组件之一,即Feign

Feign

Feign是由Netflix开发的声明式Web服务(HTTP客户端)。它的目标是简化HTTP API客户端。它是Java到HTTP客户端的绑定程序。如果要使用Feign,请创建一个接口并对其进行注释。它提供可插拔的注解支持,包括Feign注解和JAX-RS注解。

它是用于创建REST API客户端的库。它使Web服务客户端更容易。开发人员可以使用声明性注解调用REST服务,而不是编写冗长的样板代码。

Spring Cloud OpenFeign

Spring Cloud OpenFeign通过自动配置和绑定到Spring环境为Spring Boot应用程序提供了OpenFeign集成。在Spring Boot应用程序中,如果没有Feign,我们使用RestTemplate来调用用户服务。要使用Feign,我们需要在pom.xml文件中添加spring-cloud-starter-openfeign依赖项。

让我们使用Spring Cloud OpenFeign简化先前开发的代码。

在上一节中,我们已经遇到的一个问题是当我们开发currency-conversion-service时,调用REST服务是多么困难。我们必须要做很多手工操作才能调用一个非常简单的服务。但是我们仍然为此编写了很多代码。

当我们使用微服务时,将会有很多调用其他服务的情况。我们不需要像以前那样编写代码。Feign是解决这个问题的一个组件。Feign使得调用其他微服务变得容易。

Feign提供的另一个附加功能是:它集成了Ribbon(客户端负载均衡框架)。

让我们在项目中实现Feign并使用Feign调用其他微服务。

步骤1: 选择currency-conversion-service项目。

步骤2: 打开pom.xml并添加Feign依赖项。Feign继承自Netflix

<dependency>  
<groupId>org.springframework.cloud</groupId>    
<artifactId>spring-cloud-starter-feign</artifactId>  
<version>1.4.4.RELEASE</version>  
</dependency>  

步骤3: 添加依赖项后,在CurrencyConversionServiceApplication.java文件中通过添加注解@EnableFeignClients启用Feign以扫描客户端。

步骤4:@EnableFeignClients注解中定义一个属性。属性是我们要扫描的包的名称。

package cn.javatiku.microservices.currencyconversionservice;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
import org.springframework.cloud.openfeign.EnableFeignClients;  
@SpringBootApplication  
@EnableFeignClients("cn.javatiku.microservices.currencyconversionservice")  
public class CurrencyConversionServiceApplication   
{  
public static void main(String[] args)   
{  
SpringApplication.run(CurrencyConversionServiceApplication.class, args);  
}  
}  

我们已经在项目中启用了Feign。现在,我们将使用Feign来调用服务。

步骤5: 创建一个Feign代理,使我们能够与外部微服务通信。让我们创建一个名为CurrencyExchangeServiceProxy的接口。

步骤6: 添加注解@FeignClient。传递属性nameURL

name属性中,写入我们要使用的服务的名称。在我们的情况下,我们将使用currency-exchange-service。在URL属性中,写入currency-exchange-service正在运行的端口。

@FeignClient(name="currency-exchange-service", url="localhost:8000")  

步骤7: 现在,我们需要定义一个方法,该方法与currency-exchange-controller通信。打开Currency-ConverterController.java文件。复制currency-converter映射并将其粘贴到同一个文件中。

步骤8: 将映射名称更改为/currency-converter-feign/from/{from}/to/{to}/quantity/{quantity},将方法名称更改为convertCurrencyFeign

步骤9: 使用CurrencyExchangeServiceProxy并将其注入。

@Autowired
private CurrencyExchangeServiceProxy proxy;

步骤10: 首先,通过调用URL http://localhost:8000/currency-exchange/from/USD/to/INR 运行currency-exchange-service,然后通过使用URL http://localhost:8100/currency-converter/from/USD/to/INR/quantity/1000 运行currency-conversion-service

如果不按顺序运行服务,currency-conversion-service将显示Whitelabel错误页面。这是因为currency-conversion-service使用currency-exchange-service的conversionMultiple。

步骤11: 使用URL http://localhost:8100/currency-converter-feign/from/USD/to/INR/quantity/1000 执行Feign服务。它返回与currency-converter-service相同的响应。

using-feign-rest-client-for-service-invocation.png

在上述URL中将货币USD更改为EUR,然后再次调用相同的URL。它获取currency-exchange-service中的"from"变量并返回totalCalculatedAmount

using-feign-rest-client-for-service-invocation-1.png

我们发送的请求使用了Feign。Feign是一个REST服务客户端。Feign可以轻松调用RESTful Web服务。当我们使用RestTemplate调用RESTful服务时,会创建与RESTful服务通信的重复代码

当我们定义Feign时,只需定义一个代理并在其中定义一个方法即可。Feign帮助我们简化了与RESTful Web服务通信的客户端代码。

考虑一种情况,currency-exchange-service提供了十五种不同的服务。所有与这些服务相关的详细信息必须在一个地方定义,即CurrencyExchangeServiceProxy接口。

标签: spring, Spring教程, spring cloud, spring cloud教程, spring cloud框架, spring cloud面试题, springcloud组件, springcloud微服务架构, springcloud入门教程, springcloud主件, spring cloud架构图