微服务已变成新的流行词,快速取代了云微服务,微服务是由许多更小的架构组件,它们快速构建和交付,在单独和整体上变得越来越强大,体现了模块与模块之间的高内聚低耦合。本文开始以小白的视角学习,小编在这里对多系统之间数据交互进行了核心的整理,话不多少,开始正文。
--添加依赖
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-feignartifactId>
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> 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
> 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> bClass(@RequestBody Entity entity); @RequestMapping(value = A + "/A/XX", consumes = "application/json", produces = "application/json", method = RequestMethod.GET) Response
> cClass(@RequestParam("id") String id); }
--- 系统之间数据交互成功,实现类读者由读者自行完成!
------本文属于小编原创内容,未经许可不得转载!