SpringCloud微服务--使用使用openfeign实现服务间调用传参

首先在中台引入pox:

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

创建一个Service接口:

package com.zhisen.cslcps.service;

import java.util.Map;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient("cslcp")	
public interface FeignService {
	@GetMapping("/test")
	public Map<String,Object> test1();
	
	@DeleteMapping("/d1")
	public void restTemplatedelete1();
	@DeleteMapping("/d2")
	public void restTemplatedelete2(@RequestParam Map<String, Object> result);
	@DeleteMapping("/d3")
	public void restTemplatedelete3(@RequestBody Map<String, Object> result);
//	传递参数为Param
	@PostMapping("/test3")
	public Map<String, Object> restTemplatepostparam(@RequestParam Map<String, Object> result);
//	传递参数为body
	@PostMapping("/test2")
	public Map<String, Object> restTemplatepost(@RequestBody Map<String, Object> result);
//	传递参数为url或者formdata
	@GetMapping("/test1")
	public Map<String, Object> restTemplate(@RequestParam Map<String, Object> result);
}

@FeignClient("cslcp") 中的内容是服务的名称,所有的方法的返回值和参数都与普通工程中的接口一一对应。
注意:如果参数类型为一个实体,而你的方法为GetMapping会报错,原因是因为实体会再body中传输,这时,你可以将普通工程中的方法改为post,或者添加拦截器等等。

再在中台的主类中加入;

@EnableFeignClients

这样就可以直接在controller中直接调用service接口中的方法,就可以实现调用其他服务的接口了。

制作整理不易,以上内容均为原创(参考了部分官方文档和老师整理的案例)。如要引用请附上本文链接,如有疑问可以在评论区畅所欲言,作者看到会第一时间回复,欢迎交流!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

么贺贵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值