系统之间数据相互交互如何使用Feign构造参数请求

微服务已变成新的流行词,快速取代了微服务,微服务是由许多更小的架构组件,它们快速构建和交付,在单独和整体上变得越来越强大,体现了模块与模块之间的高内聚低耦合。本文开始以小白的视角学习,小编在这里对多系统之间数据交互进行了核心的整理,话不多少,开始正文。


--添加依赖

<dependency>  

<groupId>org.springframework.cloud</groupId>  

<artifactId>spring-cloud-starter-feign</artifactId>  

</dependency>


本文我们来探讨使用Feign多参数跨系统请求数据,主要以Get和Post请求为例,在这里以两个系统(A和B系统相互独立的系统)之间数据交互为例。数据请求之前平台已经搭建好consul集群,我们以consul为平台进行数据之间交互,由于该框架依赖于springboot,由于springboot的某些原因,在这里小编把接口打成jar包,web打成war包,而SpringCloud为Feign添加了SpringMvc的注解支持,小编在这里使用springmvc的写法写一遍,话不多说,来点实战吧,实战之前读者需要搞清楚注解@RequestBody和@RequestParam的区别以防http清楚出现404错误和乱码。



--A系统  在A系统中对外接口定义控制层,以及明白@RestController和@Controller的区别
@Api(value = "菜单", description = "A系统" )
@RestController
@RequestMapping("/A")
@CommonsLog
public class AController extends AbstractWebController {
   @RequestMapping(value = "/XX", method = RequestMethod.POST)
   @ApiOperation(value="数据匹配", notes="数据匹配/A系统")
   @ApiImplicitParams({
   @ApiImplicitParam(name = "Id", value = "id", required = true, paramType = "query"),
   @ApiImplicitParam(name = "name", value = "姓名", required = true, paramType = "query")})
    public Response<List<Entity>> bClass(@RequestBody Entity entity){
        //业务逻辑   。。。。。
        log.info("entity");
        return returnSuccess();
    }

   @RequestMapping(value = "/XX", method = RequestMethod.GET)
   @ApiOperation(value="数据匹配", notes="数据交互/A系统")
   @ApiImplicitParams({
      @ApiImplicitParam(name = "id",value = "id", required = true, paramType = "query")})
    public Response<List<Entity>> cClass(@RequestParam("id") String id){
        //业务逻辑   。。。。。
        log.info("id");
        return returnSuccess();
    }
}

   -- B系统 定义接口

@FeignClient(A)
public interface BClient {

    String A = "A";

  @RequestMapping(value = A + "/A/XX", consumes = "application/json", produces = "application/json", method = RequestMethod.POST)
   Response<List<Entity>> bClass(@RequestBody Entity entity);

  @RequestMapping(value = A + "/A/XX", consumes = "application/json", produces = "application/json", method = RequestMethod.GET)
    Response<List<Entity>> cClass(@RequestParam("id") String id);
}

      --- 系统之间数据交互成功,实现类读者由读者自行完成!


             ------本文属于小编原创内容,未经许可不得转载!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值