springCloud入門學習(九):使用feign實現API調用

1、簡介java

Feign是一款聲明式、模板化的HTTP客戶端,能夠幫助咱們優雅的調用HTTP API。spring

2、爲服務消費者整合Feignapp

一、添加feign依賴負載均衡

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-feign</artifactId>
</dependency>

二、建立Feign接口ide

@FeignClient(name = "user")
public interface UserFeignClient {

    @RequestMapping(value = "/user/getUserInfo", method = RequestMethod.GET)
    Map findById(@RequestParam("userId") Integer userId);

}

註解@FeignClient中指定的就是註冊中心內的serviceId,用於建立Ribbon負載均衡。
測試

三、修改controllerblog

@RequestMapping(value = "/movie/findById/feign", method = RequestMethod.GET)
public Map findByIdFeign(Integer userId){

    return userFeignClient.findById(userId);
}

四、修改啓動類,加入以下註解接口

@EnableFeignClients

3、測試get

訪問   http://localhost:8020/movie/findById/feign?userId=1it

image.png

相關文章
相關標籤/搜索