文章目录

  • 概述
  • 实例
    • 新建工程
    • 增加maven依赖
    • 创建一个Feign接口,并添加@FeignClient注解
    • 修改Controller层,将RestTemplate改为调用Feign接口
    • 启动类增加@EnableFeiginClients注解
    • 测试
  • 源码

概述

回想下我们在使用Eureka 和 Ribbon的时候是怎么调用注册在Eureka Server上的微服务的地址呢?

可以看到其实是通过拼接的方式,当然了我们上面的这个例子只有一个参数 id,看起来没有这麻烦。

设想下如果有多个参数呢?
假设URL如下
http://localhost:8080/search?name=小工匠&age=20&username=artisan

那我们用RestTemplate如何调用对方的微服务呢? 可以采用如下方式

  @GetMapping("/searchUser")public User searchUser(String name ,String age ,String username) {Map<String, Object> paraMap = new HashMap<String ,Object>() {{put("name",name);put("age",age);put("username",username);}  };return  this.restTemplate.getForObject("http://microservice-provider-user/search?name={name}&age={age}&username={username}", User.class, paraMap);}

是不是已经很麻烦了?

Spring Cloud为我们整合了Fegin解决上述苦恼。


Feign官方文档: https://cloud.spring.io/spring-cloud-static/Finchley.SR2/single/spring-cloud.html#_spring_cloud_openfeign

Feign是Netflix开发的声明模板化的HTTP客户端。 在Spring Cloud中使用Feign,只需要创建一个接口,并在接口上添加一些注解即可。 Spring Cloud对Feign进行了增强,使Feign支持了SpringMVC的总结,并整合了Ribbon和Eureka。


实例

新建工程

在父工程上右键,新建Maven Module ,如下


下面根据官方文档操作即可

增加maven依赖

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

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

package com.artisan.micorservice.feignclient;import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;import com.artisan.micorservice.model.User;@FeignClient("microservice-provider-user")
public interface UserFeignClient {@RequestMapping(method = RequestMethod.GET, value = "/user/{id}")public User findById(@PathVariable Long id);}

FeignClient中的microservice-provider-user是要调用的微服务的名称,用于创建Ribbon负载均衡器。

因为我们这里使用了Eureka,所以Ribbon会把microservice-provider-user解析成Eureka Server中注册的服务。

另外,也可以通过url属性指定请求的URL ,比如 @FeignClient("microservice-provider-user", url="http://localhost:8900/")


修改Controller层,将RestTemplate改为调用Feign接口

package com.artisan.micorservice.controller;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;import com.artisan.micorservice.feignclient.UserFeignClient;
import com.artisan.micorservice.model.User;@RestController
public class MovieController {@Autowiredprivate UserFeignClient userClient;@GetMapping("/movie/{id}")public User findById(@PathVariable Long id) {return userClient.findById(id);}
}

启动类增加@EnableFeiginClients注解

package com.artisan.micorservice;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
public class MicorserviceConsumerFeginApplication {public static void main(String[] args) {SpringApplication.run(MicorserviceConsumerFeginApplication.class, args);}
}

测试

  1. 启动eureka server微服务
  2. 启动2个 provider-user微服务
  3. 启动该微服务

2次请求http://localhost:7901/movie/1 ,观察 provider-user微服务的日志打印情况。

8900端口

8901端口

通过日志可以看到不仅实现了声明式的REST API调用,同时也实现了客户端的负载均衡。


源码

https://github.com/yangshangwei/SpringCloudMaster

Spring Cloud【Finchley】-06服务消费者整合Feign相关推荐

  1. Spring Cloud入门-Ribbon服务消费者(Hoxton版本)

    文章目录 Spring Cloud入门系列汇总 摘要 Ribbon简介 RestTemplate的使用 GET请求方法 getForObject方法 getForEntity方法 POST请求方法 p ...

  2. Spring Cloud入门 -- Ribbon服务消费者(Hoxton.SR5版)

    Ribbon简介 Ribbon是Netflix公司开源的客户端负载均衡器,可以控制Http和Tcp客户端的负载均衡. Ribbon默认提供了很多负载均衡算法,如:轮询.随机等,也可以实现自定义的负载均 ...

  3. 5.Spring Cloud Alibaba教程:Nacos整合Feign

    概述 Feign是一个声明式的http客户端.使用Feign只需要创建接口并加上对应的注解,就可以实现类似RestTemplate方式的调用,只是它将底层的http请求代码隐藏起来.另外,Feign默 ...

  4. [Spring-cloud-eureka]使用 gradle, spring boot,Spring cloud Eureka 搭建服务消费者

    2019独角兽企业重金招聘Python工程师标准>>> 本次内容为搭建一个服务消费者,用于消费上一篇博客注册在注册中心里的服务. 1)用 eclipse 新建一个 gradle 项目 ...

  5. Spring Cloud服务提供者与服务消费者怎么运作的?

    在微服务中最基本最基本的两个角色是服务提供者与服务消费者. 之前所有代码都在同一个框架的时候,比如Controller调用Service的,直接注入Service bean即可进行调用了.现在做成微服 ...

  6. spring cloud 建一个服务消费者client-ribbon

    2019独角兽企业重金招聘Python工程师标准>>> 在它的pom.xml文件分别引入起步依赖spring-cloud-starter-eureka.spring-cloud-st ...

  7. Spring Cloud入门-Admin服务监控中心(Hoxton版本)

    文章目录 Spring Cloud入门系列汇总 摘要 Spring Boot Admin 简介 创建admin-server模块 创建admin-client模块 监控信息演示 结合注册中心使用 修改 ...

  8. Spring Cloud分布式微服务整体架构

    去饭店吃饭就是一个完整的业务,饭店的厨师.配菜师.传菜员.服务员就是分布式:厨师.配菜师.传菜员和服务员都不止一个人,这就是集群:分布式就是微服务的一种表现形式,分布式是部署层面,微服务是设计层面. ...

  9. Spring Cloud入门-Gateway服务网关(Hoxton版本)

    文章目录 Spring Cloud入门系列汇总 摘要 Gateway 简介 相关概念 创建 api-gateway模块 在pom.xml中添加相关依赖 两种不同的配置路由方式 使用yml配置 使用Ja ...

最新文章

  1. 每一次宕机都是新的开始
  2. netty java开发文档_《Netty官方文档》设置开发环境
  3. nuget 包版本冲突解决 packages.config
  4. 【转】JMeter学习(二十七)Jmeter常见问题
  5. 关于错误:不能将licenses.licx文件转换成二进制,error lc0003 !
  6. win10 打开注册表
  7. anylogic中编写java代码_anylogic 使用
  8. Day4--MATLAB简介
  9. cei()、linspace()、arrange()、full()、eye()、empty()、random()
  10. IP地址中的网络地址和主机地址分别是什么意思?
  11. 高翔视觉slam十四讲习题(1)
  12. hadoopsnappy解压_Hadoop安装配置snappy压缩
  13. html单元格浮雕效果,怎样用PS做出这种凹面浮雕效果?
  14. Superset系列6-制作折线图
  15. 搭建go语言开发环境
  16. 从键盘输入50个学生数据,打印出每门课的平均成绩
  17. 关于xxxxxxRepository.search()方法一个分页的小陷阱
  18. 交换机 POE 学习
  19. ubuntu 8.04 配置okl4编译环境
  20. python 打印直角三角形

热门文章

  1. matlab九节点网络仿真问题,三机九节点电力系统仿真matlab.docx
  2. centos 安装mysql5.7_Zabbix 4.2.5 安装部署实践详解
  3. 机器学习应用方向(二)~概念漂移(concept drift)
  4. keras 实战系列之Self-Attention详细解析
  5. Matlab实现线性回归和逻辑回归: Linear Regression Logistic Regression
  6. python logging 毫秒级别的时间打印
  7. 【算法与数据结构】一道检测inversion count的初级算法
  8. Quartz框架多个trigger任务执行出现漏执行的问题分析--转
  9. 多种负载均算法及其 Java 代码实现 --转
  10. spring与memcache的整合