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
相关推荐
青石路3 分钟前
记一次多JDK版本问题的排查,一坑套一坑,差点没爬上来
java
杨充31 分钟前
1.面向对象设计思想
后端
IT_陈寒1 小时前
Java的Date类又坑了我一次,改用时间戳真香
前端·人工智能·后端
systemPro1 小时前
2.6亿条设备数据,历史查询从超时到50ms,我做了什么
后端
要阿尔卑斯吗2 小时前
提示词优化启示:为什么“按顺序输出“比“关键度评分“更有效
后端
她的男孩2 小时前
后台接口加密别只会 HTTPS,ForgeAdmin 的 RSA + SM4/AES 源码拆解
后端·面试·开源
极光技术熊2 小时前
Spring AI 从入门到精通:构建你的 AI 开发知识体系
后端·github
杉氧2 小时前
深入理解 Compose 重组机制:快照系统如何驱动 UI 精准刷新?
android·架构·android jetpack
程序员cxuan2 小时前
一句话,让你用上 GPT-5.6
人工智能·后端·程序员
远航_2 小时前
OpenSpec 完整详细介绍
前端·后端