目录
基于Feign的远程服务调用
-
Feign是什么
Feign 是一种声明式Web服务客户端,底层封装了对Rest技术的应用,通过Feign可以简化服务消费方对远程服务提供方法的调用实现。如图所示:
Feign 最早是由 Netflix 公司进行维护的,后来 Netflix 不再对其进行维护,最终 Feign 由社区进行维护,更名为 OpenFeign。
-
Feign应用实践
- 在服务消费方,添加项目依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
- 在启动类上添加注解 @EnableFeignClients
@EnableFeignClients @SpringBootApplication public class ConsumerApplication {…}
- 定义Http请求的API,基于此API借助OpenFeign访问远端服务
package com.jt.feign; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; /** * @FeignClient 注解用于描述远程