feign其實是針對原來的ribbon做了一層封裝,使得遠程調用面向接口,直接提供參數和request映射即可:
同樣的,在上幾篇博客的基礎上,copy一份order-service模塊出來,其他不變,就是刪除掉RibbonServiceImpl
然後直接提供接口和相關的參數:修改RibbonService
@FeignClient(value = "USER-SERVICE") ///指命相關的微服務
public interface RibbonUserService {
// 只提供相應的接口和映射即可
@RequestMapping(value = "/v1/user",method = RequestMethod.GET)
UserInfoResp getUserInfoFeign();
}
記得在pom.xml文件中加入feign的依賴:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>
在啓動類上加入註解:
@EnableFeignClients
修改後爲:
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class,args);
}
}
然後啓動項目:調用接口 GET http://localhost:8803/v1/order/feign 成功返回