【从入门到精通Spring Cloud】声明式服务调用组件OpenFeign
文章目录
- 📕1. 快速上手
- ✏️1.1 引入依赖(客户端)
- ✏️1.2 添加注解(客户端启动类)
- ✏️1.3 编写OpenFeign客户端
- ✏️1.4 远程调用
- 📕2. OpenFeign参数传递
- ✏️2.1 传递单个参数
- ✏️2.2 传递多个参数
- ✏️2.3 传递对象
- ✏️2.4 传递JSON
- 📕3. Feign的最佳实践 -> 继承
- ✏️3.1 创建一个Module
- ✏️3.2 引入依赖
- ✏️3.3 编写ProductFeignApi(在feign- service服务中)
- ✏️3.4 将feign- service服务打jar包
- ✏️3.5 编写ProductController(服务提供方,在product- service服务中)
- ✏️3.6 编写服务消费者的Feign客户端
特此注明 :
Designed By :长安城没有风
Version:1.0
Time:2025.09.10
Location:辽宁 · 大连
OpenFeign是一个显示声明式的WebService客户端。使用OpenFeign能让编写Web Service客户端更加简单。使用时只需定义服务接口,然后在上面添加注解。在Spring Cloud中使用OpenFeign,可以做到使用HTTP请求访问远程服务,就像调用本地方法一样的,开发者完全感知不到这是在调用远程方法,更感知不到在访问HTTP请求,非常的方便。
📕1. 快速上手
✏️1.1 引入依赖(客户端)
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
✏️1.2 添加注解(客户端启动类)
@EnableFeignClients
@SpringBootApplication
public class OrderServiceApplication {public static void main(String[] args) {SpringApplication.run(OrderServiceApplication.class, args);}
}
✏️1.3 编写OpenFeign客户端
@FeignClient(value = "product-service",path = "product")
public interface ProductApi {@RequestMapping("/{id}")ProductInfo getproductById(@PathVariable("id") Integer productId);//此处只是借口,真正的代码逻辑实现还在product-service的controller层里面
}
@FeignClient 注解作⽤在接⼝上, 参数说明 :
name/value :指定FeignClient的名称,也就是微服务的名称,⽤于服务发现,Feign底层会使⽤Spring Cloud LoadBalance进⾏负载均衡,也可以使⽤ url 属性指定⼀个具体的url/
path : 定义当前FeignClient的统⼀前缀。
✏️1.4 远程调用
@Slf4j
@Service
public class OrderService {@Autowiredprivate ProductApi productApi;public OrderInfo getOrderById(Integer orderId) {OrderInfo orderInfo = orderMapper.selectOrderById(orderId);ProductInfo forObject = productApi.getproductById(orderInfo.getProductId());orderInfo.setProductInfo(forObject);return orderInfo;}
}
📕2. OpenFeign参数传递
使⽤@RequestParam 进⾏参数绑定即可
✏️2.1 传递单个参数
服务提供者
@RestController
@RequestMapping("test")
public class FeignTest {@RequestMapping("/t1")public String t1(Integer id){return "t1:"+id;}
}
Feign客户端
@FeignClient(value = "product-service",path = "test")
public interface FeignTest {@RequestMapping("/t1")public String t1(Integer id);
}
//注意: @RequestParam 做参数绑定, 不能省略
服务消费者
@RestController
@RequestMapping("/feign")
public class FeignTestController {@Autowiredprivate FeignTest feignTest;@RequestMapping("/t1")public String t1(Integer id) {return feignTest.t1(id);}
}
✏️2.2 传递多个参数
使⽤多个@RequestParam 进⾏参数绑定即可
服务提供者
@RestController
@RequestMapping("/product")
public class ProductController {@RequestMapping("t2")public String t2(String name,Integer id) {return "name:"+name+" id:"+id;}
}
Feign客户端
@FeignClient(value = "product-service",path = "product")
public interface ProductApi {@RequestMapping("t2")String t2(@RequestParam("name") String name,@RequestParam("id") Integer id);
}
服务消费者
@RestController
@RequestMapping("/feign")
public class FeignTestController {@Autowiredprivate ProductApi feignTest;@RequestMapping("/t2")public String t2(String name,Integer id) {return feignTest.t2(name,id);}
}
✏️2.3 传递对象
服务提供者
@RequestMapping("/p3")
public String p3(ProductInfo productInfo){return "接收到对象, productInfo:"+productInfo;
}
Feign客户端
@RequestMapping("/p3")
String p3(@SpringQueryMap ProductInfo productInfo);
服务消费者
@RequestMapping("/o3")
public String o3(ProductInfo productInfo){return productApi.p3(productInfo);
}
✏️2.4 传递JSON
服务提供者
@RequestMapping("/p4")
public String p4(@RequestBody ProductInfo productInfo){return "接收到对象, productInfo:"+productInfo;
}
Feign客⼾端
@RequestMapping("/p4")
String p4(@RequestBody ProductInfo productInfo);
服务消费者
📕3. Feign的最佳实践 -> 继承
Feign ⽀持继承的⽅式,我们可以把⼀些常⻅的操作封装到接⼝⾥。我们可以定义好⼀个接⼝,服务提供⽅实现这个接⼝,服务消费⽅编写Feign 接⼝的时候,直接继承这个接⼝。
✏️3.1 创建一个Module
✏️3.2 引入依赖
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency></dependencies>
✏️3.3 编写ProductFeignApi(在feign- service服务中)
public interface ProductFeignApi {@RequestMapping("/{id}")ProductInfo getProductById(@PathVariable("id") Integer id);@RequestMapping("t1")String t1(@RequestParam("id") Integer id);@RequestMapping("t2")String t2(@RequestParam("name") String name,@RequestParam("id") Integer id);@RequestMapping("t3")String t3(@SpringQueryMap ProductInfo productInfo);}
✏️3.4 将feign- service服务打jar包
✏️3.5 编写ProductController(服务提供方,在product- service服务中)
@RestController
@RequestMapping("/product")
public class ProductController implements ProductFeignApi {@Autowiredprivate ProductService productService;@RequestMapping("/{id}")public ProductInfo getProductById(@PathVariable("id") Integer id) {return productService.getProductById(id);}@Overridepublic String t1(Integer id) {return "";}@Overridepublic String t2(String name, Integer id) {return "";}@Overridepublic String t3(feign.model.ProductInfo productInfo) {return "";}
}
✏️3.6 编写服务消费者的Feign客户端
@FeignClient(value = "product-service",path = "product")
public interface ProductApi extends ProductFeignApi {
}