当前位置: 首页 > news >正文

Spring Cloud Feign的两种使用姿势

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

Profile


概述

最近结合一些别人的开源项目来学习Spring Cloud,其中关于服务消费这方面的一个很便利的工具 Feign让我记忆颇深。虽然网上的Demo和例子不胜枚举,但大多比较分散,本文就来集中记录一下声明式客户端 Feign的一些使用姿势。

注: 本文首发于 博客 CodeSheep · 程序羊,欢迎光临 小站

下文就结合例子来记录这一过程,代码在文尾处。


创建基于 Eureka的服务注册中心

三个步骤即可搞定:

  • 建工程

创建一个名为 eureka_server的 SpringBoot工程,并在pom.xml中添加好对应依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
  • 改主类

修改应用主类,添加 @EnableEurekaServer注解

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
  • 加配置

配置 application.properties 文件如下所示:

spring.application.name=eureka-server
server.port=1111

eureka.instance.hostname=localhost

#默认设置下,服务注册中心自己也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
  • 启动服务注册中心

浏览器访问之:

浏览器访问服务注册中心

此时还没有任何服务注册上来。


创建服务提供者

  • 建工程

创建一个名为 service_provider的 SpringBoot工程,并在pom.xml中添加好对应依赖:

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
  • 改主类

添加 @EnableDiscoveryClient注解

@EnableDiscoveryClient
@SpringBootApplication
public class ServiceProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceProviderApplication.class, args);
    }
}
  • 添加控制器 DateServiceController

提供一个Restful接口而已,该接口的作用是获取服务器上的时间并返回

@RestController
public class DateServiceController {

    @RequestMapping( value = "/test", method = RequestMethod.GET )
    public String test( @RequestParam String param ){
        return "hello " + param;
    }
}
  • 配置 application.properties文件
spring.application.name=service_provider
server.port=1112
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
  • 启动工程

浏览器访问服务注册中心,我们发现服务提供者 service_provider已经注册到 eureka_server上:

服务提供者已注册上来了

同时浏览器访问:http://localhost:1112/test?param=www.codesheep.cn,可以测试服务提供 service_provider提供的接口工作正常

测试发现服务提供者的接口工作正常

接下来我们创建服务消费者,是 Feign该登场的时候了!


创建基于 Feign的服务消费者

  • 创建一个名为 service_consumer的 SpringBoot工程,并在pom.xml中添加好对应依赖
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
  • 修改应用主类

主要是添加有关 Feign客户端的一些注解而已

@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class ServiceConsumerApplication {

	public static void main(String[] args) {
		SpringApplication.run(ServiceConsumerApplication.class, args);
	}
}
  • 创建一个 Feign客户端的接口:DateServiceFeignClientInterface

很明显其内部用 @FeignClient( value = "service-provider" ) 声明的方式指向了 服务提供者,而接口方法则实现了对 服务提供者接口的实际调用

@FeignClient( value = "service-provider" )
public interface DateServiceFeignClientInterface {

    @GetMapping("/test")
    String consumer( @RequestParam("param") String param );
}
  • 创建控制器:DateServiceFeignController

注意,这是服务消费者提供的 Rest接口

@RestController
@RequestMapping("/consumer")
public class DateServiceFeignController {

    @Resource
    DateServiceFeignClientInterface dateServiceFeignClientInterface;

    @GetMapping("/date")
    public String getDate( @RequestParam String param ) {
        return dateServiceFeignClientInterface.consumer( param );
    }
}
  • 配置 application.properties
spring.application.name=service-consumer
server.port=1113

eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
  • 启动服务消费者

我们先去服务注册中心上看看,发现 服务消费者也注册上来了:

服务消费者已注册上来

然后我们浏览器访问 服务消费者提供的Rest接口: http://localhost:1113/consumer/date?param=www.codesheep.cn

数据成功取回

这样我们就通过 服务消费者的 Feign客户端 取到了服务提供者 给予的接口数据。

上面这就是声明式客户端 Feign的第一种使用姿势,也是常用的手法,常见于很多Demo

下面我们来实践一下关于 Feign的继承与实现机制,发现其使用更加灵活( Feign支持接口继承方式快速生成客户端,颇有点RPC的意思(关于RPC的实践可以参考我的文章:《RPC框架实践之:Google gRPC》、《RPC框架实践之:Apache Thrift》) )


抽象出一个公共的 API服务

  • 创建一个普通 Maven项目:service_provider_api

  • 创建一个公共接口:DateService

public interface DateService {
    @GetMapping("/api/test")
    String consumer( @RequestParam("param") String param );
}

改造之前的 服务提供者 / 消费者项目

  • 在服务消费者 service_consumer项目中添加一个新的Feign的客户端接口
@FeignClient( value = "service-provider" )
public interface DateServiceFeignClientInterface2 extends DateService {
}
  • 并且在 service_consumer项目中添加一个新的控制器 DateServiceFeignController2
@RestController
@RequestMapping("/consumer2")
public class DateServiceFeignController2 {

    @Resource
    DateServiceFeignClientInterface2 dateServiceFeignClientInterface2;

    @GetMapping("/date")
    public String getDate( @RequestParam String param ) {
        return dateServiceFeignClientInterface2.consumer( param );
    }
}
  • 在服务提供者 service_provider项目中来实现我们在公共api项目 service_provider_api中的 DateService接口,赋予实际逻辑
@RestController
public class DateServiceController2 implements DateService {

    @Override
    public String consumer( @RequestParam String param) {
        Date now = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("今天是"+"yyyy年MM月dd日 E kk点mm分" );
        String nowTime = simpleDateFormat.format( now );
        return "hello again " + param + ", " + nowTime;
    }
}
  • 依次将 eureka_serverservice_providerservice_consumer 三个项目分别启动

浏览器访问:http://localhost:1113/consumer2/date?param=www.codesheep.cn

成功实现服务调用

使用 feign的继承特性时,可以将服务接口的定义从服务消费者中剥离出去,形成独立的api项目从而可以很方便的实现接口定义和依赖的共享,不用再复制粘贴接口进行绑定,当然这种做法存在的问题就是可能会导致服务提供者和服务消费者间的耦合度增高,此时如果服务提供者修改了一个接口定义,服务消费者可能也得跟着变,进而带来一些坑。



后 记

由于能力有限,若有错误或者不当之处,还请大家批评指正,一起学习交流!

  • My Personal Blog:CodeSheep 程序羊
  • 我的半年技术博客之路

本文实验代码在此



转载于:https://my.oschina.net/hansonwang99/blog/2221254

相关文章:

  • Tornado实现多进程/多线程的HTTP服务
  • 数据库之左连接,右连接和内连接
  • C++ 引用
  • ES6 ...操作符
  • 2.Median of Two Sorted Arrays (两个排序数组的中位数)
  • 轻量级kotlin + Mvp + Rxjava + Retrofit框架
  • HDU 2722 Here We Go(relians) Again
  • yii2-queue一个好用的yii2队列操作扩展
  • ppwjs之bootstrap表格:响应式
  • [大牛翻译系列]Hadoop(22)附录D.2 复制连接框架
  • Java大小写转换
  • Transact-SQL语法速查手册
  • 开源地图数据可视化库——mapnik
  • IOS开发常用的linux命令
  • grep/字符/次数匹配/锚定符/小大括号/wc/tr/cut/sort/uniq
  • IE9 : DOM Exception: INVALID_CHARACTER_ERR (5)
  • 【Leetcode】104. 二叉树的最大深度
  • 【跃迁之路】【699天】程序员高效学习方法论探索系列(实验阶段456-2019.1.19)...
  • 2017-09-12 前端日报
  • 4月23日世界读书日 网络营销论坛推荐《正在爆发的营销革命》
  • co模块的前端实现
  • css系列之关于字体的事
  • gf框架之分页模块(五) - 自定义分页
  • JS基础之数据类型、对象、原型、原型链、继承
  • Js基础知识(一) - 变量
  • opencv python Meanshift 和 Camshift
  • python大佬养成计划----difflib模块
  • React-生命周期杂记
  • 阿里云应用高可用服务公测发布
  • 关于extract.autodesk.io的一些说明
  • 简析gRPC client 连接管理
  • 将回调地狱按在地上摩擦的Promise
  • 开源中国专访:Chameleon原理首发,其它跨多端统一框架都是假的?
  • 聊聊flink的TableFactory
  • 日剧·日综资源集合(建议收藏)
  • 使用Envoy 作Sidecar Proxy的微服务模式-4.Prometheus的指标收集
  • 收藏好这篇,别再只说“数据劫持”了
  • 数据仓库的几种建模方法
  • 数据可视化之 Sankey 桑基图的实现
  • 阿里云服务器购买完整流程
  • 完善智慧办公建设,小熊U租获京东数千万元A+轮融资 ...
  • #我与Java虚拟机的故事#连载11: JVM学习之路
  • (175)FPGA门控时钟技术
  • (附源码)spring boot建达集团公司平台 毕业设计 141538
  • (附源码)计算机毕业设计ssm电影分享网站
  • (算法)Game
  • (五) 一起学 Unix 环境高级编程 (APUE) 之 进程环境
  • (原創) 如何使用ISO C++讀寫BMP圖檔? (C/C++) (Image Processing)
  • .aanva
  • .net 4.0 A potentially dangerous Request.Form value was detected from the client 的解决方案
  • .Net 8.0 新的变化
  • .NET Core 通过 Ef Core 操作 Mysql
  • .net MVC中使用angularJs刷新页面数据列表
  • .net 验证控件和javaScript的冲突问题
  • .net开发引用程序集提示没有强名称的解决办法