Feign的使用

阅读更多

前提:本服务和要调用的服务都注册到consul

 

1.引入依赖:


    org.springframework.cloud
    spring-cloud-starter-openfeign

2. 启动类上面加上

 @EnableFeignClients

3.本地stub

@FeignClient("bank-card-identifier")
public interface CardService {
    @RequestMapping(value = "/card/info?cardNo={cardNo}", method = RequestMethod.GET)
    BankCardInfo cardInfo(@Param(value = "cardNo") String cardNo);}

 

bank-card-identifier是要调用的接口在consul里的服务名称
@RequestMapping 指定要调用的接口

 

4.调用

@RestController
@AllArgsConstructor
@Slf4j
public class FeignTest {
    private final CardService cardService;
@RequestMapping(value = "/get",method = RequestMethod.GET)
    BankCardInfo get(String cardNo){
        final BankCardInfo bankCardInfo = cardService.cardInfo(cardNo);
log.info(bankCardInfo.getBankCode());
        return bankCardInfo;
}
}

你可能感兴趣的:(feign)