需要先部署Nacos注册中心
代码如下:
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-loadbalancerartifactId>
dependency>
启动类上添加注解,启动OpenFeign功能
代码如下:
@EnableFeignClients
@SpringBootApplication
public class CartApplication {
public static void main(String[] args) {
SpringApplication.run(CartApplication.class, args);
}
}
代码如下:
@FeignClient("item-service")
public interface ItemClient {
@GetMapping("/items")
List<ItemDTO> queryItemByIds(@RequestParam("ids") Collection<Long> ids);
}
代码如下:
private final ItemClient itemClient;
private void handleCartItems(List<CartVO> vos) {
// 获取商品id
Set<Long> itemIds = vos.stream().map(CartVO::getItemId).collect(Collectors.toSet());
// 查询商品
List<ItemDTO> items = itemClient.queryItemByIds(itemIds);
}
代码如下:导入依赖
<dependency>
<groupId>io.github.openfeigngroupId>
<artifactId>feign-okhttpartifactId>
dependency>
开启连接池
feign:
okhttp:
enabled: true # 开启OKHttp功能