• OpenFeign:声明式服务调用


    Spring Cloud OpenFeign:声明式服务调用

    一、OpenFeign简介

    1. 什么是OpenFeign

    ​ OpenFeign目前是Spring Cloud 二级子项目。平时说的Feign指的是Netflix下的Feign,现在我们学习的是OpenFeign,是Spring提供的。

    ​ OpenFeign是一种声明式、模板化的HTTP客户端(仅在Application Client中使用)(称OpenFeign作用:声明式服务调用)。声明式调用是指,就像调用本地方法一样调用远程方法,无需感知操作远程http请求。学习完OpenFeign后可以不使用RestTemplate进行调用。

    ​ 使用OpenFeign时就好像在写控制器方法,OpenFeign都是写在接口中,在声明的方法上添加SpringMVC注解或声明的参数上添加SpringMVC注解就可以完成调用远程的控制器方法。

    2. OpenFeign的执行流程

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0Z9QOT65-1662386288714)(images/openfeign流程.png)]

    文字说明

    整体流程说明:

    1. Application Service向Eureka Server 注册服务。
    2. Application Client从Eureka Server中发现服务。
    3. 在Application Client中调用OpenFeign接口中方法。
    4. Application Client中OpenFeign通过应用程序名访问Application Service。
    5. OpenFeign访问远程服务时,基于Ribbon实现负载均衡。

    二、第一个OpenFeign项目

    1. Application Service应用准备

    ​ 使用Ribbon课程中的Application Service项目作为已有应用直接启动即可。

    2. Application Client应用开发

    2.1 创建Application Client项目

    ​ 创建项目application_client_openfeign

    2.2POM依赖
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.3.12.RELEASEversion>
    parent>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>Hoxton.SR12version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-openfeignartifactId>
        dependency>
    dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    2.3 编写配置文件

    ​ 编写配置文件application.yml

    server:
      port: 9999
    spring:
      application:
        name: eureka-client-open-feign
    
    • 1
    • 2
    • 3
    • 4
    • 5
    2.4 编写OpenFeign本地接口

    ​ 编写接口 com.wck.feign.AppServiceOpenfeignClient

    /**
     * 定义接口,基于注解,实现声明式远程服务调用。
     * 技术是OpenFeign。
     * 需要确定的事情:
     *  1. 
    • 1
    • 2
    • 3
    • 4
  • 相关阅读:
    数据湖iceberg-day01-概念,特点,存储格式以及各种表中的演化,数据类型
    华为OD机试 - 判断字符串子序列(Java 2023 B卷 100分)
    【Lua面试】 迭代器和泛型For
    【commons-beanutils专题】004- BeanUtils 专题
    【iOS】OC关键字总结及底层原理(上)
    元宇宙简介
    JavaScript之运算符相关知识
    后疫情时代,河北吉力宝打开传统制造业数字化转型新视角
    Android查看签名信息系列 · 使用逆向分析工具JadxGUI获取签名
    Matlab坐标轴标签中文设置宋体
  • 原文地址:https://blog.csdn.net/woruosuifenglang/article/details/126714457