Spring Cloud Feign 的两种使用姿势

2018-09-29 07:21:52 +08:00
 hansonwang99


概述

最近结合一些别人的开源项目来学习 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);
    }
}

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

@RestController
public class DateServiceController {

    @RequestMapping( value = "/test", method = RequestMethod.GET )
    public String test( @RequestParam String param ){
        return "hello " + param;
    }
}
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 的服务消费者

    <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);
	}
}

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

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

    @GetMapping("/test")
    String consumer( @RequestParam("param") String param );
}

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

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

    @Resource
    DateServiceFeignClientInterface dateServiceFeignClientInterface;

    @GetMapping("/date")
    public String getDate( @RequestParam String param ) {
        return dateServiceFeignClientInterface.consumer( param );
    }
}
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 》]( http://www.codesheep.cn/2018/05/21/RPC 框架实践之:Google-gRPC/)、[《 RPC 框架实践之:Apache Thrift 》]( http://www.codesheep.cn/2018/05/14/RPC 框架实践之:Apache-Thrift/)) )


抽象出一个公共的 API 服务

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

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

@FeignClient( value = "service-provider" )
public interface DateServiceFeignClientInterface2 extends DateService {
}
@RestController
@RequestMapping("/consumer2")
public class DateServiceFeignController2 {

    @Resource
    DateServiceFeignClientInterface2 dateServiceFeignClientInterface2;

    @GetMapping("/date")
    public String getDate( @RequestParam String param ) {
        return dateServiceFeignClientInterface2.consumer( param );
    }
}
@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;
    }
}

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

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



后 记

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

本文实验代码在此



2326 次点击
所在节点    程序员
5 条回复
LucasLee92
2018-09-29 09:16:04 +08:00
感谢楼主,回帖收藏,有空看
twocold0451
2018-09-29 09:16:39 +08:00
V 站竟然有技术贴,收藏一下
Resource
2018-09-29 10:16:40 +08:00
再次被 @
ksupertu
2018-09-29 10:33:54 +08:00
@twocold0451 笑出了猪叫声
hansonwang99
2018-09-29 12:16:05 +08:00
V 站技术文章不挺多的吗?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/493704

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX