# 简介
简化外部调用 官方文档:https://docs.spring.io/spring-cloud-openfeign/docs/current/reference/html/
# 使用
依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.10.RELEASE</version>
</dependency>
1
2
3
4
5
2
3
4
5
启动类加注解
@SpringBootApplication
@EnableFeignClients
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
业务代码
@FeignClient(name = "${feign.name}", url = "${feign.url}", configuration = FooConfiguration.class)
public interface StoreClient {
@RequestMapping(method = RequestMethod.GET, value = "/stores")
List<Store> getStores();
@RequestMapping(method = RequestMethod.GET, value = "/stores")
Page<Store> getStores(Pageable pageable);
@RequestMapping(method = RequestMethod.POST, value = "/stores/{storeId}", consumes = "application/json")
Store update(@PathVariable("storeId") Long storeId, Store store);
@RequestMapping(method = RequestMethod.DELETE, value = "/stores/{storeId:\\d+}")
void delete(@PathVariable Long storeId);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
可以配置自动获取token等
# 注意
- 配置类上不需要加@Configuration注解
- 配置url后也需要配置name
# 常见问题
# 找不到bean
原因:有些老项目包路径不规范,maven分模块项目报名不一致 方法如下:
- 指定扫码路径 @EnableFeignClients(basePackages = "com.example.clients")
- 指定FeignClient类 @EnableFeignClients(clients = InventoryServiceFeignClient.class)
# 收不到值
原因:java的框架属性都是首字母小写的,有些外部系统字段首字母大写就不认了 解决方法: 实体类属性上加注解@JsonProperty("Response")
我的博客即将同步至腾讯云开发者社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=38dpnhkh4o8wo