目录
在开发中,我们常用httpClient去远程调用其他系统的接口,需要我们指定调用的url,Feign 是一个声明式的 Web Service 客户端,它实现了一套远程调用的方法,调用的方式也更为优雅。
前提:1.被调用的模块注册到eureka中,能正常运行
2.feign接口文件夹要和启动类在同一个目录下, 否则需指定包名,@EnableFeignClients(basePackages="com.**.***")
不同版本依赖可能略有区别
2.1 添加feigh客户端依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
2.2 启动类上添加feign注解:@EnableFeignClients
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class SIAPIApplication { public static void main(String[] args) { SpringApplication.run(SIAPIApplication.class, args); } }
2.3 application.yml配置文件添加配置信息
feign: client: config: default: connectTimeout: 10000 readTimeout: 10000
2.4 创建一个feign客户端,用于引入远程模块的接口:@FeignClient
# 注册到eureka中的si-api模块名称 @FeignClient(value = "si-api") public interface ApiOrderFlowService { // 这里定义了类似于SpringMVC用法的方法,就可以进行RESTful的调用了 /** * 更新流程状态 */ @RequestMapping("flow/progressOrderFlow") Result progressOrderFlow(@RequestParam("orderId") Long orderId); /** * 创建流程实例, 并关联指定订单 * @param orderId 指定订单ID * @param serviceOfferId 指定订单操作类型 * @return 流程数量 */ @RequestMapping("flow/createOrderFlowInst") Result createOrderFlowInst(@RequestParam("orderId") Long orderId, @RequestParam("serviceOfferId") Integer serviceOfferId); }
2.5 在需要进行远程调用的方法里注入该接口,并调用对应的api接口方法
@Autowired private ApiOrderFlowService apiOrderFlowService;