1. 回顾

  前文的示例中是使用RestTemplate实现REST API调用的,代码大致如下:

@GetMapping("/user/{id}")
public User findById(@PathVariable Long id) {
return this.restTemplate.getForObject("http://microservice-provider-user/" + id, User.class);
}

  由代码克制,我们是使用拼接字符串的方式构造URL的,该URL只有一个参数。

  然而在现实中,URL往往有多个参数。如果这时还使用这种方式构造URL,那么就会变得很低效,并且难以维护。

2. Feign简介

  Feign是Netflix开发的声明式、模板化的HTTP客户端,其灵感来自Retrofit、JAXRS-2.0以及WebSocket。Feign可帮助我们更加便捷、优雅地调用HTTP API。

  在Spring Cloud中,使用Feign非常简单——创建一个接口,并在接口上添加一些注解,代码就完成了。Feign支持多种注解,例如Feign自带的注解或者

  JAX-RS注解等。

  Spring Cloud对Feign进行了增强,使Feign支持了Spring MVC注解,并整合了Ribbon和Eureka,从而让Feign的使用更加方便。

3. 为服务消费者整合Feign

  > 复制项目 microservice-consumer-movie,将ArtifactId修改为 microservice-consumer-movie-feign

  > 添加Feign的依赖

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

  > 创建一个Feign接口,并添加@FeignClient注解

package com.itmuch.cloud.microserviceconsumermoviefeign.feign;

import com.itmuch.cloud.microserviceconsumermoviefeign.pojo.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; @FeignClient(name = "microservice-provider-user")
public interface UserFeignClient { @GetMapping(value = "/{id}")
User findById(@PathVariable("id") Long id); }

  @FeignClient注解中的microservice-provider-user是一个任意的客户端名称,用于创建Ribbon负载均衡器。在本例中,由于使用了Eureka,

  所以Ribbon会把microservice-provider-user解析成Eureka Server服务注册表中的服务。

  如果不想使用Eureka,也可使用service.ribbon.listOfServers属性配置服务器列表

  也可使用url属性指定请求的URL(URL可以是完整的URL或主机名),例如

  @FeignClient(name = "microservice-provider-user", url = "http://localhost:8000/")

  > 修改Controller代码,让其调用Feign接口

package com.itmuch.cloud.microserviceconsumermoviefeign.controller;

import com.itmuch.cloud.microserviceconsumermoviefeign.feign.UserFeignClient;
import com.itmuch.cloud.microserviceconsumermoviefeign.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; @RestController
public class MovieController { @Autowired
private UserFeignClient userFeignClient; @GetMapping("/user/{id}")
public User findById(@PathVariable Long id) {
return this.userFeignClient.findById(id);
} }

  > 修改启动类,添加@EnableFeignClients注解

package com.itmuch.cloud.microserviceconsumermoviefeign;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class MicroserviceConsumerMovieFeignApplication { public static void main(String[] args) {
SpringApplication.run(MicroserviceConsumerMovieFeignApplication.class, args);
} @Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}

  > 启动 microservice-discovery-eureka

  > 启动两个以上的 microservice-provider-user 实例

  > 启动 microservice-consumer-movie-feign

  > 多次访问 http://localhost:8010/user/1,在各个实例的控制台下都可看见类似日志

Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?
2018-03-27 17:48:19.468 TRACE 14160 --- [nio-8001-exec-2] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [BIGINT] - [1]
2018-03-27 17:48:19.480 TRACE 14160 --- [nio-8001-exec-2] o.h.type.descriptor.sql.BasicExtractor : extracted value ([age2_0_0_] : [INTEGER]) - [20]
2018-03-27 17:48:19.481 TRACE 14160 --- [nio-8001-exec-2] o.h.type.descriptor.sql.BasicExtractor : extracted value ([balance3_0_0_] : [NUMERIC]) - [100.00]
2018-03-27 17:48:19.481 TRACE 14160 --- [nio-8001-exec-2] o.h.type.descriptor.sql.BasicExtractor : extracted value ([name4_0_0_] : [VARCHAR]) - [张三]
2018-03-27 17:48:19.481 TRACE 14160 --- [nio-8001-exec-2] o.h.type.descriptor.sql.BasicExtractor : extracted value ([username5_0_0_] : [VARCHAR]) - [account1]

4. 总结

  以上结果说明,本文实现了声明式的REST API调用,同时还实现了客户端侧的负载均衡。

  下文将讲解自定义Feign配置,敬请期待~~~

5. 参考

  周立 --- 《Spring Cloud与Docker微服务架构与实战》

SpringCloud系列十:使用Feign实现声明式REST调用的更多相关文章

  1. SpringCloud系列-利用Feign实现声明式服务调用

    上一篇文章<手把手带你利用Ribbon实现客户端的负载均衡>介绍了消费者通过Ribbon调用服务实现负载均衡的过程,里面所需要的参数需要在请求的URL中进行拼接,但是参数太多会导致拼接字符 ...

  2. 6.使用Feign实现声明式REST调用

                        使用Feign实现声明式REST调用 6.1. Feign简介 Feign是一个声明式的REST客户端,它的目的就是让REST调用更加简单. Feign提供了H ...

  3. SpringCloud学习笔记(3):使用Feign实现声明式服务调用

    简介 Feign是一个声明式的Web Service客户端,它简化了Web服务客户端的编写操作,相对于Ribbon+RestTemplate的方式,开发者只需通过简单的接口和注解来调用HTTP API ...

  4. spring cloud 入门系列五:使用Feign 实现声明式服务调用

    一.Spring Cloud Feign概念引入通过前面的随笔,我们了解如何通过Spring Cloud ribbon进行负责均衡,如何通过Spring Cloud Hystrix进行服务断路保护,两 ...

  5. SpringCloud(四):使用Feign实现声明式服务调用

    一.Feign介绍Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单.使用Feign,只需要创建一个接口并注解.它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解 ...

  6. springCould:使用Feign 实现声明式服务调用

    一.Spring Cloud Feign概念引入通过前面的随笔,我们了解如何通过Spring Cloud ribbon进行负责均衡,如何通过Spring Cloud Hystrix进行服务断路保护,两 ...

  7. Spring Cloud Feign声明式服务调用(转载)+遇到的问题

    转载:原文 总结: 1.pom添加依赖 2.application中填写正确的eureka配置 3.启动项中增加注解 @EnableFeignClients 4.填写正确的调用接口 通过原文使用Fei ...

  8. springcloud-feign组件实现声明式的调用

    11.使用feign实现声明式的调用 使用RestTemplate+ribbon已经可以完成对服务端负载均衡的调用,为什么还要使用feign? @RequestMapping("/hi&qu ...

  9. SpringCloud 源码系列(6)—— 声明式服务调用 Feign

    SpringCloud 源码系列(1)-- 注册中心 Eureka(上) SpringCloud 源码系列(2)-- 注册中心 Eureka(中) SpringCloud 源码系列(3)-- 注册中心 ...

随机推荐

  1. Codeforces Round 536 (Div. 2) (E)

    layout: post title: Codeforces Round 536 (Div. 2) author: "luowentaoaa" catalog: true tags ...

  2. Spoj Query on a tree III

    题目描述 给出N个点的一棵树(N-1条边),节点有白有黑,初始全为白 有两种操作: 0 i : 改变某点的颜色(原来是黑的变白,原来是白的变黑) 1 v : 询问1到v的路径上的第一个黑点,若无,输出 ...

  3. 【点分治】bzoj2152 聪聪可可

    模板题. #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> ...

  4. AngularJS的加载执行过程

    1. HTML页面的加载,这会触发加载页面包含的所有JS (包括 AngularJS) 2. AngularJS启动,搜寻所有的指令(directive) 3. 找到ng-app,搜寻其指定的模块(M ...

  5. jvm-监控指令-jdump

    格式: jmap [option] vmid 作用: 生成堆转储快照. 使用:(注意:需要使用工具打开,分析. 比如: EclipseMemoryAnalyzer)

  6. 使用ASP.Net WebAPI构建REST服务(七)——调试工具

    由于WebAPI本身是基于HTTP协议的,在开发过程中,我们可以使用浏览器或Fiddler等HTTP工具辅助开发.与此同时,微软也提供了一些工具方便我们调试,使得开发更加简单快捷,本文就简单的介绍一下 ...

  7. 【重装系统】老毛桃U盘工具V2013超级装机版-安装原版Win7/Win8

    老毛桃U盘工具V2013超级装机版-程序下载和运行 老毛桃U盘工具V2013超级装机版-安装原版XP的方法 老毛桃U盘工具V2013超级装机版-安装原版Win7/Win8

  8. 更合理的 setState()

    原文发表在我的博客:http://www.erichain.me/2017/04/17/2017-04-17-more-reasonable-setstate/ React 是我做前端以来接触到的第三 ...

  9. oracle 11g jdbc jar包在哪个文件目录

    一. 如果装了oracle数据库的话, 大致是这样的目录:    D:\oracle\product\11.2.0\client_1\oui\jlib\classes12.jar 或者    D:\o ...

  10. XSS跨站脚本测试用例

    '><script>alert(document.cookie)</script>='><script>alert(document.cookie)&l ...