Spring Cloud - Spring Cloud 声明式接口调用(Fiegn 声明式接口调用概述、Fiegn 使用)

一、Fiegn 声明式接口调用

1、Fiegn 概述
  • Feign 是由 Netflix 提供的一套组件,是一个提供模版的声明式 Web Service 客户端

  • 使用 Feign 可以简化 Web Service 客户端的编写,开发者可以通过简单的接口和注解来调用 HTTP API

  • Spring Cloud 也提供了对 Feign 的集成组件 Spring Cloud Feign,有可插拔(低耦合)、基于注解、负载均衡、服务熔断等功能

2、Fiegn 使用概述
  • 只需要创建接口,同时在接口上添加相关注解即可
3、Ribbon 和 Feign 的对比
  • Ribbon 是一个通用的 HTTP 客户端工具

  • Feign 基于 Ribbon ,更加灵活

4、Fiegn 的特点
  • Feign 是一个声明式 Web Service 客户端

  • 支持 Feign 注解和 Spring MVC 注解

  • Feign 基于 Ribbon 实现

  • Feign 集成了 Hystrix,具备服务熔断功能


二、Fiegn 使用

1、具体实现
(1)创建工程
  • 创建子工程(Module),在 pom.xml 文件中配置相关依赖
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>SpringCloudTest</artifactId>
        <groupId>com.my</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>Feign</artifactId>

    <dependencies>

        <!-- Eureka Client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!-- Feign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

</project>
(2)配置文件
  • 在 recourses 目录下创建并配置 application.yaml 文件
yaml 复制代码
server:
  port: 8050
spring:
  application:
    name: Feign
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
(3)Entity
  • 创建 Student 实体类
java 复制代码
package com.my.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    private Integer id;
    private String name;
}
(4)Feign 接口
  • 创建 FeignServerProviderClient 接口
java 复制代码
package com.my.feign;

import com.my.entity.Student;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

// 指向服务提供者(服务在注册中心对应的服务名)
@FeignClient(value = "ServerProvider")
public interface FeignServerProviderClient {

    @GetMapping("/student/findAll")
    public Collection<Student> findAll();

    @GetMapping("/student/findById/{id}")
    public Student findById(@PathVariable("id") Integer id);

    @PostMapping("/student/save")
    public void save(@RequestBody Student student);

    @PutMapping("/student/update")
    public void update(@RequestBody Student student);
    
    @DeleteMapping("/student/deleteById/{id}")
    public void deleteById(@PathVariable("id") Integer id);
}
(5)Controller
  • 创建 FeignHandler 类
java 复制代码
package com.my.controller;

import com.my.entity.Student;
import com.my.feign.FeignServerProviderClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

@RestController
@RequestMapping("/feign")
public class FeignHandler {

    @Autowired
    private FeignServerProviderClient feignServerProviderClient;

    @GetMapping("/findAll")
    public Collection<Student> findAll(){
        return feignServerProviderClient.findAll();
    }

    @GetMapping("/findById/{id}")
    public Student findById(@PathVariable("id") Integer id){
        return feignServerProviderClient.findById(id);
    }

    @PostMapping("/save")
    public void save(@RequestBody Student student){
        feignServerProviderClient.save(student);
    }

    @PutMapping("/update")
    public void update(@RequestBody Student student){
        feignServerProviderClient.update(student);
    }
    
    @DeleteMapping("/deleteById/{id}")
    public void deleteById(@PathVariable("id") Integer id){
        feignServerProviderClient.deleteById(id);
    }
}
(6)启动类
  • 创建启动类
java 复制代码
package com.my;

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

@SpringBootApplication

// 使 Feign 生效
@EnableFeignClients
public class FeignApplication {
    public static void main(String[] args) {
        SpringApplication.run(FeignApplication.class, args);
    }
}
2、测试
  • 依次启动注册中心、服务提供者、Feign,使用 Postman 进行测试
(1)findAll
参数项 说明
请求类型 GET 请求
请求地址 访问地址:http://localhost:8040/feign/findAll
(2)findById
参数项 说明
请求类型 GET 请求
请求地址 http://localhost:8040/feign/findById/1
(3)save
参数项 说明
请求类型 POST 请求
访问地址 http://localhost:8040/feign/save
  • JSON 数据
json 复制代码
{
    "id": 4,
    "name": "nono"
}
(4)update
参数项 说明
请求类型 PUT 请求
请求地址 http://localhost:8040/feign/update
  • JSON 数据
json 复制代码
{
    "id": 3,
    "name": "jack"
}
(5)deleteById
参数项 说明
请求类型 DELETE 请求
请求地址 http://localhost:8040/feign/deleteById/4
相关推荐
+VX:Fegn0895几秒前
计算机毕业设计|基于java+ vue共享单车信息系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
苏渡苇5 分钟前
Spring Insight 里如何把 Span 画成瀑布时间线
后端·spring·spring cloud·springboot·监控·apm
泡茶喝茶写代码8 分钟前
量化数据进阶:多维实战篇(第 11 篇):历史涨跌停价:涨跌停序列与止损线测算
java·python·股票数据api·股票数据·股票数据api接口·股票api数据接口·股票量化数据api
叶总没有会11 分钟前
Nacos—注册中心
spring cloud·微服务
景熙552320 分钟前
单体项目编写思路和落地形式
java·开发语言
Ivanqhz21 分钟前
Slope One 算法详解:与矩阵分解、共现矩阵的异同
java·服务器·网络·人工智能·深度学习
Charlie_Byte36 分钟前
Spring AI 2 手把手:把 filesystem MCP Server 跑通(SSE / stdio + 真调工具)
后端
程序猿DD44 分钟前
OctaFuse Gateway 2.11.0:流式请求优化、用户折扣策略与错误契约升级
前端·后端
Bs_MoneyMagnet1 小时前
基于springboot+vue的非遗物质文化遗产系统的设计与实现 源码+文档
java·vue.js·spring boot·后端·spring·毕业设计·计算机毕业设计
万物智能1 小时前
波形发生AD9833模块配置—【万物智能之开源鸿蒙OpenHarmony系统实战开发系列教程】
java