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
相关推荐
Lei活在当下1 天前
【Perfetto从入门到精通】2. 使用 Perfetto 追踪/分析 APP 的 Native/Java 内存
android·性能优化·架构
HerayChen1 天前
HbuilderX 内存溢出报错
java·大数据·linux
程序员小白条1 天前
0经验如何找实习?
java·开发语言·数据结构·数据库·链表
Xの哲學1 天前
Linux Miscdevice深度剖析:从原理到实战的完整指南
linux·服务器·算法·架构·边缘计算
小马爱打代码1 天前
Spring AI:搭建自定义 MCP Server:获取 QQ 信息
java·人工智能·spring
郭涤生1 天前
QT 架构笔记
java·数据库·系统架构
韩立学长1 天前
基于Springboot流浪动物领养网站0kh2iyb4(程序、源码、数据库、调试部署方案及开发环境)系统界面展示及获取方式置于文档末尾,可供参考。
数据库·spring boot·后端
daidaidaiyu1 天前
基于LangGraph开发复杂智能体学习一则
java·ai
Moment1 天前
从美团全栈化看 AI 冲击:前端转全栈,是自救还是必然 🤔🤔🤔
前端·后端·面试
小小8程序员1 天前
STL 库(C++ Standard Template Library)全面介绍
java·开发语言·c++