1. Feign介绍

通过RestTemplate调用其它服务的API时,所需要的参数须在请求的URL中进行拼接,如果参数少的话或许我们还可以忍受,一旦有多个参数的话,这时拼接请求字符串就会效率低下

Feign是一个声明式的Web Service客户端,它的目的就是让Web Service调用更加简单。Feign提供了HTTP请求的模板,通过编写简单的接口和插入注解,就可以定义好HTTP请求的参数、格式、地址等信息。

而Feign则会完全代理HTTP请求,我们只需要像调用方法一样调用它就可以完成服务请求及相关处理。Feign整合了Ribbon和Hystrix,可以让我们不再需要显式地使用这两个组件。

Feign具有如下特性:

  • 支持可插拔的HTTP编码器和解码器;
  • 支持Hystrix和它的Fallback;
  • 支持Ribbon的负载均衡;
  • 支持HTTP请求和响应的压缩。

有点像我们springmvc模式的Controller层的RequestMapping映射。这Feign是用@FeignClient来映射服务的。

2. Feign的使用

接前面的文章,再新建一个项目FeignTest,这里把OpenFeign勾选上

FeignTest项目核心代码

部分代码从之前的项目中直接拷贝

package com.springcloud.consumer.service;import com.springcloud.consumer.pojo.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;/**** @date 2020/7/28 12:27* @author wei.heng*/
@FeignClient("PRODUCT-PROVIDER")
public interface ProductService {/*** 获取产品对象* @param id 主键ID* @return com.springcloud.consumer.pojo.Product* @date 2020/7/28 12:27* @author wei.heng*/@GetMapping("/product/{id}")Product getProduct(@PathVariable Long id);/**** 新增产品对象* @param product 产品对象* @return org.springframework.http.ResponseEntity* @date 2020/7/28 12:27* @author wei.heng*/@PostMapping("/product")ResponseEntity addProduct(@RequestBody Product product);
}
package com.springcloud.consumer;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;/**** @date 2020/7/28 10:45* @author wei.heng*/
@SpringBootApplication
@EnableFeignClients
public class ConsumerApplication {/**** 初始化 RestTemplate -  @LoadBalanced做多节点负载均衡* @return org.springframework.web.client.RestTemplate* @date 2020/7/27 16:57* @author wei.heng*/@LoadBalanced@Bean(name = "restTemplate")public RestTemplate initRestTemplate(){return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(ConsumerApplication.class, args);}}
package com.springcloud.consumer.controller;import com.springcloud.consumer.pojo.Product;
import com.springcloud.consumer.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;/**** @date 2020/7/27 14:58* @author wei.heng*/
@RestController
public class ProductController {private ProductService productService;@Autowiredpublic ProductController(ProductService productService) {this.productService = productService;}@GetMapping("/product/{id}")public ResponseEntity<Product> product(@PathVariable Long id){Product product = productService.getProduct(id);return ResponseEntity.ok(product);}@PostMapping("/product")public ResponseEntity products(@RequestBody Product product){return productService.addProduct(product);}}

application.yml

server:port: 8003eureka:client:# 默认拉取服务列表,这里不做配置了register-with-eureka: falseservice-url:# 服务中心地址defaultZone: http://localhost:7000/eureka/,http://localhost:8000/eureka/

在product-provider服务项目中新增方法

 @PostMapping("/product")public ResponseEntity product(@RequestBody Product product){int i = productService.inserProduct(product);ResponseEntity responseEntity;if(i > 0){responseEntity = new ResponseEntity(HttpStatus.CREATED);} else {responseEntity = new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);}return responseEntity;}

postman测试

feign post 传递单个字符串示例

feign

 @PostMapping(value = "/update2ApplySuccess", consumes = MediaType.TEXT_PLAIN_VALUE)Boolean update2ApplySuccess(@RequestBody String transactionNo);

controller

 @PostMapping("/update2ApplySuccess")public Boolean update2ApplySuccess(@RequestBody String transactionNo)

亲测OK

Feign(简介和使用)相关推荐

  1. Feign 简介和使用

    声明式服务消费Feign 一.简介 二.使用Feign实现服务消费者 三.实现普通的服务提供者 四.Feign服务调用测试 五.Feign消费者测试 负载均衡 服务熔断 一.简介 Feign是Netf ...

  2. 微服务SpringCloud之Feign简介及使用

    Feign的目标 feign是声明式的web service客户端,它让微服务之间的调用变得更简单了,类似controller调用service.Spring Cloud集成了Ribbon和Eurek ...

  3. Spring Cloud Feign 1(声明式服务调用Feign 简介)

    Spring Cloud Feign基于Netflix Feign 同时整合了Spring Cloud Ribbon和Spring Cloud Hytrix,除了提供两者的强大功能外,它还提供了一种声 ...

  4. Feign简介与简单应用

    一 点睛 Feign是Netflix开发的声明式.模板化的HTTP客户端, Feign可以帮助我们更快捷.优雅地调用HTTP API. 在Spring Cloud中,使用Feign非常简单--创建一个 ...

  5. Feign的简介及使用

    一.Feign简介 Feign是一个声明式的http客户端,官方地址:https://github.com/OpenFeign/feign 其作用就是帮助我们优雅的实现http请求的发送,解决代码可读 ...

  6. 企业分布式微服务云SpringCloud SpringBoot mybatis - 服务消费者(Feign)

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

  7. Spring Cloud(四)服务提供者 Eureka + 服务消费者 Feign

    上一篇文章,讲述了如何通过RestTemplate + Ribbon去消费服务,这篇文章主要讲述如何通过Feign去消费服务. Feign简介 Feign是一个声明式的伪Http客户端,它使得写Htt ...

  8. Spring Cloud第三篇:服务消费者Feign

    上一篇文章,讲述了如何通过RestTemplate+Ribbon去消费服务,这篇文章主要讲述如何通过Feign去消费服务. 一.Feign简介 Feign是一个声明式的伪Http客户端,它使得写Htt ...

  9. java B2B2C Springboot仿淘宝电子商城系统-负载均衡之ribbon+feign

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

  10. 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务

    在spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端.我们可以使用JDK原生的URLConnection.Ap ...

最新文章

  1. 对抗样本无法被重建!CMU提出通用的无监督对抗攻击检测方法
  2. Scala类的继承,抽象类定义,接口定义
  3. Netbeans8下 Weblogic EJB案例
  4. 【VxWorks系列】任务间同步与通信之共享内存
  5. 调整Excel的打印线
  6. 从inceptionv1走向xception小结
  7. 机器学习- 吴恩达Andrew Ng Week8 知识总结 Clustering
  8. linux 命令是什么的缩写,Linux一部分命令解释(命令缩写代表什么意思)
  9. js正则只能输入大于0的正整数
  10. 数据结构python吕云翔_数据结构(PYTHON版)/吕云翔
  11. Linux: 李纳斯·托沃兹(Linus Torvalds): “使用KDE”(转)
  12. python数据爬取并可视化代码,python爬取摩拜单车API数据并做可视化分析(源码)...
  13. 洛谷P1151 子数整数(问题转化,透过现象看本质)
  14. 群晖 winscp php,WinSCP自动执行脚本
  15. gym103627L XXII Open Cup, GP of Daejeon Curly Racetrack 题解
  16. java中的空指针异常处理
  17. 南京沁恒推出的国产 M3 架构芯片与意法半导体 M3 芯片对比
  18. ora-01128,ora-00275
  19. ccc-sklearn-13-朴素贝叶斯(1)
  20. 品牌推广方法大全(收藏日后必有用处)

热门文章

  1. elasticsearch 一款高扩展性的分布式全文检索引擎
  2. 让Tomcat支持中文路径名和中文文件名
  3. NetCore中带图片的word转html (NPOI.Word2Html)
  4. Linux常用命令— 硬链接和软链接
  5. 【SCADA】关于KingSCADA仿真驱动的应用
  6. How To Generate PDFs in Rails With Prawn
  7. 怎样使用PS将彩色图片转为黑白图片
  8. stm32f103c8t6使用PB3和PB4做普通GPIO使用时发现异常
  9. R语言在读取数据的时候自动加上X. 检查列名的有效性
  10. android 自定义心电图,手把手教你打造一个心电图效果View Android自定义View(示例代码)...