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
相关推荐
_extraordinary_4 小时前
Java Spring事务,事务的传播机制
java·开发语言·spring
摸鱼的老谭4 小时前
Java学习之旅第三季-17:Lambda表达式
java·lambda表达式·1024程序员节
canonical_entropy4 小时前
领域驱动设计(DDD)中聚合根的最主要职责真的是维护一致性吗?
后端·架构·领域驱动设计
摸鱼的老谭4 小时前
Java学习之旅第三季-18:方法引用
java·方法引用
AntBlack4 小时前
不当韭菜 : 好像真有点效果 ,想藏起来自己用了
前端·后端·python
lang201509285 小时前
Spring依赖注入与配置全解析
java·spring
橙子家5 小时前
Serilog 日志库的简介
后端
百锦再5 小时前
破茧成蝶:全方位解析Java学习难点与征服之路
java·python·学习·struts·kafka·maven·intellij-idea