【Spring Cloud Alibaba】OpenFeign的使用

230 阅读3分钟

【Spring Cloud Alibaba】系列文章

标题链接
【Spring Cloud Alibaba】Nacos的安装与介绍以及Nacos集群的安装masiyi.blog.csdn.net/article/det…
【Spring Cloud Alibaba】Nacos config的使用和高阶用法masiyi.blog.csdn.net/article/det…
【Spring Cloud】Gateway的配置与使用masiyi.blog.csdn.net/article/det…
【Spring Cloud Alibaba】OpenFeign的使用masiyi.blog.csdn.net/article/det…

Feign is a Java to HTTP client binder inspired by Retrofit, JAXRS-2.0, and WebSocket. Feign's first goal was reducing the complexity of binding Denominator uniformly to HTTP APIs regardless of ReSTfulness. Feign是一个Java到HTTP的客户端绑定器,灵感来自于Retrofit、JAXRS-2.0和WebSocket。fake的第一个目标是降低将分母统一绑定到HTTP api的复杂性,而不管ReSTfulness如何。

@[toc]

在微服务架构中,服务之间的调用是非常常见的场景。Spring Cloud Alibaba提供了一套优秀的解决方案,其中之一就是OpenFeign。OpenFeign是一个基于Java编程语言的声明式HTTP客户端,它简化了服务之间的远程调用过程,并提供了一种优雅的编程方式。本文将介绍如何在Spring Cloud Alibaba项目中使用OpenFeign。

在使用上面openFeign和Feign几乎没有任何的区别

  • openFeign我们一般配合nacos使用
  • Feign我们配合Eureka使用

🌍第一步,添加jar包依赖

  <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 添加 nacos 框架依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>0.2.2.RELEASE</version>
        </dependency>
        <!-- 添加 openfeign 框架依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>

    </dependencies>

🌍第二步,启动类上添加@EnableFeignClients注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class OpenFeignApplication {
    public static void main(String[] args) {
        SpringApplication.run(OpenFeignApplication.class, args);
    }
}

🌍第三步,创建生产者

生产者类代码地址: https://gitee.com/WangFuGui-Ma/spring-cloud-alibaba/tree/master/nacos/discovery/provider

在这里插入图片描述

🌍第四步,创建openFeign的client

即openFeign调用生产者的接口,注意这里是一个接口

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient("service-provider")
public interface ConsumeClient {

    @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
    String echo(@PathVariable String str);
}

@FeignClient 填写生产者的服务名称 @RequestMapping填写生产者的rest路径

🌍第五步,添加openFeign的controller接口

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ConsumerController {

    @Autowired
    ConsumeClient client;

    @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
    public String echo(@PathVariable String str) {
        return client.echo(str);
    }
}

🌍第六步,编写启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class OpenFeignApplication {
    public static void main(String[] args) {
        SpringApplication.run(OpenFeignApplication.class, args);
    }
}

注意添加@EnableFeignClients注解开启feign

🌍第七步,编写配置文件

application.yml

spring:
  application:
    name: openfeign-consumer
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

server:
  port: 8099

🌍第八步,启动生产者和消费者(feign)

这个时候nacos已经注册进来了

在这里插入图片描述 调用我们的feign业务层获取生产者数据

流程图(引用)

在这里插入图片描述 类似 RestTemplate,本质上是 OpenFeign 的底层会用到 JDK 的HttpURLConnection 发出 HTTP 请求。

注册中心的作用只是告诉消费者服务端的地址。

通过本文的介绍,我们了解了Spring Cloud Alibaba中OpenFeign的使用。我们首先学习了如何配置和启用OpenFeign客户端,并使用注解实现接口的声明性远程调用。

OpenFeign作为Spring Cloud Alibaba的核心组件之一,为微服务架构中的服务间通信提供了便捷和灵活的解决方案。希望本文能够对您理解和使用OpenFeign提供一些帮助,并在实际项目中发挥其价值。如果您对Spring Cloud Alibaba和OpenFeign有更多兴趣,建议深入学习相关文档和示例,以便更好地应用于实际开发中。

代码地址 https://gitee.com/WangFuGui-Ma/spring-cloud-alibaba

在这里插入图片描述