• Spring Cloud(Finchley版本)系列教程(三) 服务消费者(Feign)


    Spring Cloud(Finchley版本)系列教程(三) 服务消费者(Feign)

    一、Feign和OpenFeign的对比

    FeignNetflix公司写的,是SpringCloud组件中的一个轻量级RESTfulHTTP服务客户端,是SpringCloud中的第一代负载均衡客户端。OpenFeignSpringCloud自己研发的,在Feign的基础上支持了Spring MVC的注解,如@RequesMapping等等。是SpringCloud中的第二代负载均衡客户端。Feign是在2019就已经不再更新了,随之取代的是OpenFeign,从名字上就可以知道,它是Feign的升级版。而目前很多公司项目都是Feign的天下,OpenFeign的普及还是需要一丢丢时间的。我们今天玩一下Feign,需要OpenFeign使用的小伙伴可以参考一下Spring Cloud(Kilburn 2022.0.2版本)系列教程(三) 服务消费者(OpenFeign)

    SpringCloud F 及F版本以上 SpringBoot 2.0 以上基本上使用OpenFeign,OpenFeign如果从框架结构上看就是2019年Feign停更后出现版本,也可以说大多数新项目都用OpenFeign,2018年以前的项目在使用Feign。笔者强烈建议使用OpenFeign,紧跟新技术时代潮流。

    二、创建hepServiceFeign项目

    复制项目eurekaClient,并重命名为hepServiceFeign

    修改pom文件,下面三项改为hepServiceFeign,并增加Feign依赖。

    <artifactId>hepServiceFeignartifactId>
    <name>hepServiceFeignname>
    <description>hepServiceFeigndescription>
    
    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-feignartifactId>
        <version>1.4.7.RELEASEversion>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    修改pplication.yml文件,端口改为8007,spring.application.namehepServiceFeignhostname也为hepServiceFeign

    server:
      port: 8007 # 端口号
    
    spring:
      application:
        name: hepServiceFeign # Eureka名称
    
    eureka:
      instance:
        prefer-ip-address: true
        hostname: hepServiceFeign
      client:
        healthcheck:
          enabled: true
        fetch-registry: true
        register-with-eureka: true
        service-url:
          defaultZone: http://eureka:eurekapwd@www.huerpu.cc:1678/eureka/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    修改启动类名称为HepServiceFeignApplication,并加上@EnableFeignClients注解开启Feign的功能。

    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableFeignClients
    public class HepServiceFeignApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(HepServiceFeignApplication.class, args);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    三、定义Feign接口

    定义一个Feign接口,通过@FeignClient("服务名"),来指定调用哪个服务。比如在代码中调用了eurekaClient服务的"/getUserById"接口,代码如下

    package cc.huerpu.eurekaserver.feign;
    
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @FeignClient(value = "eurekaClient")
    public interface UserService {
        @RequestMapping(value = "/getUserById")
        String getUserById();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    创建一个UserController,暴露接口"/getUserByIdFeign",通过上面定义的Feign客户端UserService来消费服务。

    package cc.huerpu.eurekaserver.controller;
    
    import cc.huerpu.eurekaserver.feign.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class UserController {
        @Autowired
        private UserService userService;
        @RequestMapping("/getUserByIdFeign")
        public String getUserById(){
            return "hepServiceFeign UserController: " + userService.getUserById();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    四、Feign调用验证

    启动程序,多次访问接口http://localhost:8007/getUserByIdFeign,浏览器交替显示

    hepServiceFeign UserController: {id:1,name:jason,age:23} from server:eurekaServer:8008
    hepServiceFeign UserController: {id:1,name:jason,age:23} from server:eurekaServer:8009
    
    • 1
    • 2

    image-20230908162000205

    参考文档: https://www.fangzhipeng.com/springcloud/2018/08/03/sc-f3-feign.html

  • 相关阅读:
    推荐一款免费的内网穿透工具ngrok
    Java基础——初识Java(3) 简单认识方法
    python中使用xlrd、xlwt操作excel表格详解
    XSS简介及xsslabs第一关
    基于kolla的openstack在线变更网卡(bond)
    基于PHP+MySQL的图书分享平台
    SG-Former:具有进化Token重新分配的自引导Transformer
    [RK3568 Android11] Binder通信整体框架
    JUC基础
    2023最新SSM计算机毕业设计选题大全(附源码+LW)之java小区宠物信息管理系统0v9l2
  • 原文地址:https://blog.csdn.net/JingLisen/article/details/132763453