1 前言

  • 前面我们使用的RestTemplate实现REST API的调用,代码如下:
  1. package com.sunxiaping.order.controller;
  2. import com.sunxiaping.order.domain.Product;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.PathVariable;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import org.springframework.web.client.RestTemplate;
  9. @RestController
  10. @RequestMapping(value = "/order")
  11. public class OrderController {
  12. @Autowired
  13. private RestTemplate restTemplate;
  14. @GetMapping(value = "/buy/{id}")
  15. public Product buy(@PathVariable(value = "id") Long id) {
  16. Product product = restTemplate.getForObject("http://localhost:9003/product/findById/" + id, Product.class);
  17. return product;
  18. }
  19. }
  • 由上面的代码可知,我们是使用字符串拼接的方式构造URL的,该URL只有一个参数。但是,在现实中,URL中往往含有多个参数,这个时候,我们如果还需要字符串拼接的方式构造URL,将会非常痛苦。

2 Feign简介

  • Feign是Netflix公司开发的声明式、模板化的HTTP客户端,其灵感来自Retrofit、JAXRS-2.0以及WebSocket。
  • 服务调用Feign入门 - 图1Feign可以帮助我们更加便捷、优雅的调用HTTP的API。
  • 服务调用Feign入门 - 图2在SpringCloud中,使用Feign非常简单–创建一个接口,并在接口上添加一些注解,代码就完成了。
  • 服务调用Feign入门 - 图3Feign支持多种注解,例如Feign自带的注解或者JAX-RS注解等。
  • 服务调用Feign入门 - 图4SpringCloud对Feign进行了增强,使得Feign支持了SpringMVC注解,并整合了Ribbon和Eureka,从而让Feign的使用更加简单。

3 基于Feign的服务调用

3.1 导入Feign的相关jar包的Maven坐标

  • 修改部分:
  1. <!-- 导入openFeign的依赖 -->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-openfeign</artifactId>
  5. </dependency>
  • 完整部分:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>spring_cloud_demo</artifactId>
  7. <groupId>org.sunxiaping</groupId>
  8. <version>1.0</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>order-service-feign9005</artifactId>
  12. <dependencies>
  13. <!-- 导入openFeign的依赖 -->
  14. <dependency>
  15. <groupId>org.springframework.cloud</groupId>
  16. <artifactId>spring-cloud-starter-openfeign</artifactId>
  17. </dependency>
  18. </dependencies>
  19. </project>

3.2 在启动类上添加Feign的支持

  • 启动类:
  1. package com.sunxiaping.order;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.openfeign.EnableFeignClients;
  5. @SpringBootApplication
  6. @EnableFeignClients //添加对Feign的支持,@EnableFeignClients注解开启SpringCloudFeign的支持功能
  7. public class Order9005Application {
  8. public static void main(String[] args) {
  9. SpringApplication.run(Order9005Application.class, args);
  10. }
  11. }

3.3 配置调用接口

  • ProductClientFeign.java
  1. package com.sunxiaping.order.feign;
  2. import com.sunxiaping.order.domain.Product;
  3. import org.springframework.cloud.openfeign.FeignClient;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.PathVariable;
  6. /**
  7. * 声明需要调用的微服务名称
  8. *
  9. * @FeignClient name: 服务提供者的名称
  10. */
  11. @FeignClient(name = "service-product")
  12. public interface ProductFeignClient {
  13. /**
  14. * 配置需要调用的微服务的接口
  15. *
  16. * @param id
  17. * @return
  18. */
  19. @GetMapping(value = "/product/findById/{id}")
  20. Product findById(@PathVariable(value = "id") Long id);
  21. }

3.4 通过配置调用的接口调用远程微服务

  • OrderController.java
  1. package com.sunxiaping.order.controller;
  2. import com.sunxiaping.order.domain.Product;
  3. import com.sunxiaping.order.feign.ProductFeignClient;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.PathVariable;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RestController;
  9. @RestController
  10. @RequestMapping(value = "/order")
  11. public class OrderController {
  12. @Autowired
  13. private ProductFeignClient productFeignClient;
  14. @GetMapping(value = "/buy/{id}")
  15. public Product buy(@PathVariable(value = "id") Long id) {
  16. Product product = productFeignClient.findById(id);
  17. return product;
  18. }
  19. }

3.5 测试

基于Feign的服务调用测试.png

4 Feign和Ribbon的联系

  • Ribbon是一个基于HTTP和TCP客户端的负载均衡工具。它可以在客户端配置RibbonServerList(服务端列表),使用HttpClient或者RestTemplate模拟HTTP请求,步骤相当繁琐。
  • Feign是在Ribbon的基础上进行了一次改进,是一个使用起来更加方便的HTTP客户端。采用接口的方式,只需要创建一个接口,然后在上面添加注解,将需要调用的其他服务的方法定义成抽象方法即可,不需要自己构建HTTP请求。然后就像调用自身工程的方法调用个,而感觉不到调用远程方法,使得编写客户端变得非常容易。

5 负载均衡

  • Feign本身已经集成了Ribbon的依赖和自动配置,因此我们不需要额外的引入依赖,也不需要再注册RestTemplate对象。我们可以通过ribbon.xx来进行全局配置,或者通过服务名.ribbon.xx来对指定的服务配置。
  • 启动两个商品微服务,并将其注册到Eureka中,重新测试可以返现使用Ribbon的轮询策略进行负载均衡。

Feign的负载均衡.gif