Feign(简介和使用)

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;

	@Autowired
	public 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: 8003

eureka:
  client:
    # 默认拉取服务列表,这里不做配置了
    register-with-eureka: false
    service-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

  • 18
    点赞
  • 78
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
RabbitMQ和Feign异步的使用场景包括限流削峰、异步解耦和数据收集。在限流削峰场景中,可以使用RabbitMQ和Feign来控制请求的并发量,避免系统过载。\[1\]在异步解耦方面,使用Feign进行微服务间的调用时,可以通过异步方式发送请求,提高系统的响应速度和吞吐量。\[1\]同时,使用异步消息队列(如RabbitMQ)可以实现解耦,将请求发送到消息队列中,然后由消费者异步处理,提高系统的可伸缩性和可靠性。\[2\]在数据收集方面,可以使用RabbitMQ和Feign来收集和处理大量的数据,通过异步方式将数据发送到消息队列中,然后由消费者进行处理和存储,提高数据处理的效率和可靠性。\[1\]\[3\] #### 引用[.reference_title] - *1* [分布式消息中间件RabbitMQ学习笔记(一)——使用场景(限流削峰、异步解耦、数据收集)](https://blog.csdn.net/weixin_51542566/article/details/127471912)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [服务异步通讯——RabbitMQ](https://blog.csdn.net/m0_56188609/article/details/127576258)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值