天天看点

关于FeignClient注解的使用,部署访问

近来关于FeignClient的使用,按理说应该现在已经是很成熟了,但目前仍然会有人有一些疑惑,就想从自己的角度去把这个如何使用做个介绍:

从四个角度,了解FeginClient是什么,主要用于做什么的,如何使用这个,如何部署访问到

首先关于FeginClient注解,主要是用于服务间进行调用,作用在接口上,把接口暴露出来,其他服务可以访问到,目前我们使用的注册中心用的eurkea,举例:

A,B两套微服务

B服务上正常写自己的controller调用服务(假定获取数量)

A服务需要获取到B的数量进行A的数量的操作,这时候就需要在A服务中写一个:(分别用Get和Post方式)

这是post请求:

@FeignClient(name=“B”)

@RequestMapping("/product")

public interface ProductService {

@RequestMapping(value = "/getCounts", method = RequestMethod.POST,consumes = "application/json")
@ApiOperation("查询数量值")
public ContractRootResp<String> getCounts(@RequestBody ContractRootReq<SeqCtrlBo> req);
           

}

这是get请求:

@FeignClient(name=“B”)

@RequestMapping("/product")

public interface ProductService{

@RequestMapping(value = "/getCounts", method = RequestMethod.GET)
@ApiOperation("查询数量值")
public ContractRootResp<String> getCounts(@RequestParam("orderId") String orderId);
           

}

@Autowired
    private BFormService bFormService;
在A中写上面的这两种方式去声明,然后引入注入这个bFormService,然后直接调用bFormService.getCounts即可。
最后需要注意的一点:
	
**关于这个部署,只需要将A、B微服务打包部署上去,在同一个注册中心上就可以访问到**