一、目前Spring Cloud Alibaba的版本与Spring Boot、Spring Cloud版本的兼容关系:
Spring Boot | Spring Cloud | Spring Cloud Alibaba |
---|---|---|
2.1.x | Greenwich | 0.9.x |
2.0.x | Finchley | 0.2.x |
1.5.x | Edgware | 0.1.x |
1.5.x | Dalston | 0.1.x |
二、引入SpringCloud版本依赖
在项目主pom 中引入SpringCloud依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
在所有需要用到Feign的模块中引入openfeign依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
三、用Feign的@FeignClient(“服务名称”)映射服务调用
@FeignClient(name = "product-service")
public interface ProductFeign {
@GetMapping("/product/{productCode}")
ResultData<Product> getByCode(@PathVariable(value = "productCode") String productCode);
@PostMapping("/product/update")
ResultData<Boolean> update(@RequestBody Product product);
@PostMapping("/product/insert")
ResultData<Boolean> insert(@RequestBody Product product);
@PostMapping("/product/delete")
ResultData<Boolean> delete(@RequestParam(value = "productCode") String productCode);
}
上面是最简单的feign client的使用,声明完为feign client后,其他spring管理的类,如service就可以直接注入使用了,例如: