原文地址:Spring Cloud 入门 之 Feign 篇(三)

博客地址:http://www.extlight.com

一、前言

在上一篇文章《Spring Cloud 入门 之 Ribbon 篇(二)》 中介绍了 Ribbon 使用负载均衡调用微服务,但存在一个问题:消费端每个请求方法中都需要拼接请求服务的 URL 地址,存在硬编码问题且不符合面向对象编程思想。如果服务名称发生变化,消费端也需要跟着修改。

本篇文章将介绍 Feign 来解决上边的问题。

二、简单介绍

Feign 是一个声明式的 Web Service 客户端。使用 Feign 能让编写 Web Service 客户端更加简单,同时支持与Eureka、Ribbon 组合使用以支持负载均衡。

Spring Cloud 对 Feign 进行了封装,使其支持了 Spring MVC 标准注解和 HttpMessageConverters。

Feign 的使用方法是定义一个接口,然后在其上边添加 @FeignClient 注解

三、实战演练

本次测试案例基于之前发表的文章中介绍的案例进行演示,不清楚的读者请先转移至 《Spring Cloud 入门 之 Ribbon 篇(二)》 进行浏览。

3.1 添加依赖

在 common-api 和 order-server 项目中添加依赖:

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

3.2 定义新接口

在 common-api 中项目中新建一个接口:

@FeignClient(value="GOODS")
public interface GoodsServiceClient { @RequestMapping("/goods/goodsInfo/{goodsId}")
public Result goodsInfo(@PathVariable("goodsId") String goodsId);
}

使用 @FeignClient 注解指定调用的微服务名称,封装了调用 USER-API 的过程,作为消费方调用模板。

注意:Feign 接口的定义最好与对外开发的 controller 中的方法定义一致,此处的定义与 goods-server 项目中 controller 类定义的方法一致。

3.3 修改调用

在 order-server 项目中,使用 GoodsServiceClient 获取商品信息:

@Service
public class OrderServiceImpl implements OrderService{ // @Autowired
// private RestTemplate restTemplate; @Autowired
private GoodsServiceClient goodsServiceClient; @Override
public void placeOrder(Order order) throws Exception{ //Result result = this.restTemplate.getForObject("http://GOODS/goods/goodsInfo/" + order.getGoodsId(), Result.class); Result result = this.goodsServiceClient.goodsInfo(order.getGoodsId()); if (result != null && result.getCode() == 200) {
System.out.println("=====下订单====");
System.out.println(result.getData());
}
} }

直接使用 Feign 封装模板调用服务方,免去麻烦的 URL 拼接问题,从而实现面向对象编程。

3.4 启动 Feign 功能

在启动类上添加 @EnableEeignClients 注解:

@EnableFeignClients(basePackages = {"com.extlight.springcloud"})
@EnableEurekaClient
@SpringBootApplication
public class OrderServerApplication { public static void main(String[] args) {
SpringApplication.run(OrderServerApplication.class, args);
}
}

由于 order-server 项目中引用了 common-api 中的 GoodsServiceClient,不同属一个项目,为了实例化对象,因此需要在 @EnableFeignClients 注解中添加需要扫描的包路径。

使用 Postman 请求订单系统,请求结果如下图:

请求成功,由于 Feign 封装了 Ribbon,也就实现了负载均衡的功能。

四、案例源码

Feign demo 源码

Spring Cloud 入门 之 Feign 篇(三)的更多相关文章

  1. Spring Cloud 入门 之 Hystrix 篇(四)

    原文地址:Spring Cloud 入门 之 Hystrix 篇(四) 博客地址:http://www.extlight.com 一.前言 在微服务应用中,服务存在一定的依赖关系,如果某个目标服务调用 ...

  2. Spring Cloud 入门 之 Config 篇(六)

    原文地址:Spring Cloud 入门 之 Config 篇(六) 博客地址:http://www.extlight.com 一.前言 随着业务的扩展,为了方便开发和维护项目,我们通常会将大项目拆分 ...

  3. Spring Cloud 入门 之 Zuul 篇(五)

    原文地址:Spring Cloud 入门 之 Zuul 篇(五) 博客地址:http://www.extlight.com 一.前言 随着业务的扩展,微服务会不对增加,相应的其对外开放的 API 接口 ...

  4. Spring Cloud 入门 之 Ribbon 篇(二)

    原文地址:Spring Cloud 入门 之 Ribbon 篇(二) 博客地址:http://www.extlight.com 一.前言 上一篇<Spring Cloud 入门 之 Eureka ...

  5. Spring Cloud 入门 之 Eureka 篇(一)

    原文地址:Spring Cloud 入门 之 Eureka 篇(一) 博客地址:http://www.extlight.com 一.前言 Spring Cloud 是一系列框架的有序集合.它利用 Sp ...

  6. <Spring Cloud>入门四 Feign

    1.Feign 之前使用的是Ribbon+RestTemplate调用,通过的是微服务的名字进行调用,实现负载均衡 但是为了满足接口编程,提供了Feign 2.实现 2.1引入坐标 在 ms-comm ...

  7. Spring Cloud 入门教程(六): 用声明式REST客户端Feign调用远端HTTP服务

    首先简单解释一下什么是声明式实现? 要做一件事, 需要知道三个要素,where, what, how.即在哪里( where)用什么办法(how)做什么(what).什么时候做(when)我们纳入ho ...

  8. Spring Cloud 入门教程(三): 配置自动刷新

    之前讲的配置管理, 只有在应用启动时会读取到GIT的内容, 之后只要应用不重启,GIT中文件的修改,应用无法感知, 即使重启Config Server也不行. 比如上一单元(Spring Cloud ...

  9. Spring Cloud 入门教程(八): 断路器指标数据监控Hystrix Dashboard 和 Turbine

    1. Hystrix Dashboard (断路器:hystrix 仪表盘)  Hystrix一个很重要的功能是,可以通过HystrixCommand收集相关数据指标. Hystrix Dashboa ...

随机推荐

  1. C#Winform窗体实现服务端和客户端通信例子(TCP/IP)

    Winform窗体实现服务端和客户端通信的例子,是参考这个地址 http://www.cnblogs.com/longwu/archive/2011/08/25/2153636.html 进行了一些异 ...

  2. PHP与MYSQL数据库链接方法

    <?php //Mysqli链接数据库的方法 $host='localhost';//主机地址 $dbname='mydata2017';//数据库名 $username='root';//用户 ...

  3. linux pipes

    In Linux, a pipe is implemented using two filedata structures which both point at the same temporary ...

  4. sgu108. Self-numbers 2 滚动数组 打表 难度:1

    108. Self-numbers 2 time limit per test: 0.5 sec. memory limit per test: 4096 KB In 1949 the Indian ...

  5. css 让div 置于最顶层而不被其他东西挡住

    今天遇到自己写的div被其他东西给挡住了,需要设置一个属性就成功了 设置:z-index:值:比如 z-index:999. 若值设置为为-1,代表为最底层. div的图层由div的style中的z- ...

  6. js根据选中的复选框,隐藏那一行

    如图,选择复选框,点击“隐藏”按钮,隐藏选中行 1.JavaScript代码: function getCheckedIds() { var checkedSubject = $('#showSbgl ...

  7. Linux内核分析-分析Linux内核创建一个新进程的过程

    作者:江军 ID:fuchen1994 实验题目:分析Linux内核创建一个新进程的过程 阅读理解task_struct数据结构http://codelab.shiyanlou.com/xref/li ...

  8. Double H5.0

    Alpha阶段 - 博客链接合集 项目Github地址 Github 敏捷冲刺日志 Alpha冲刺! Day1 - 磨刀 Alpha冲刺! Day2 - 砍柴 Alpha冲刺! Day3 - 砍柴 A ...

  9. 对Repository模式误用的反思和纠正

    一直以来想自己做一套开发框架,在其基础上进行快速开发,自从接触微软的MVC框架和Entityframework以来,阅读了大量园子里的相关的技术文章,也进行了不少摸索和尝试,中间经历了多次大刀阔斧的重 ...

  10. [翻译]HTTP: Response Code

    原文地址:HTTP: Response Code 在上一篇文章中,我们总结说HTTP管理客户端和服务端之间的交互,并且解释HTTP头部的概念.在随后的系列文章中我们将讨论更多关于以下方面的信息:对交互 ...