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
相关推荐
WYS@不忘初心1 分钟前
在Eclipse IDE for Embedded C/C++ Developers软件中定义的宏,编译C源文件时编译器无法找到宏定义!
java·ide·eclipse
q***31895 分钟前
Spring Framework 中文官方文档
java·后端·spring
panco681209 分钟前
ants:强大的高性能与低成本 Go 协程池
后端
Cache技术分享10 分钟前
241. Java 集合 - 使用 Collections 工厂类处理集合
前端·后端
Heo12 分钟前
原型理解从入门到精通
前端·javascript·后端
Heo16 分钟前
通用会话控制方案
前端·javascript·后端
用户214118326360218 分钟前
Claude Skills 实战指南:一键生成公众号封面,3D 插画 + 描边标题 3 秒出图
后端
Heo20 分钟前
跨域问题解决方案汇总
前端·javascript·后端
武子康21 分钟前
大数据-154 Apache Druid 架构与组件职责全解析 版本架构:Coordinator/Overlord/Historical 实战
大数据·后端·apache
ZZHHWW22 分钟前
RocketMQ vs Kafka04 - 高性能设计与调优
后端