SpringCloud Feign远程调用 【SpringCloud系列3】

ゝ一世哀愁。 2023-10-08 21:02 77阅读 0赞

SpringCloud 大型系列课程正在制作中,欢迎大家关注与提意见。
程序员每天的CV 与 板砖,也要知其所以然,本系列课程可以帮助初学者学习 SpringBooot 项目开发 与 SpringCloud 微服务系列项目开发

Nacos 官网 https://nacos.io/zh-cn/
使用 nacos
Feign是一个声明式的http客户端,官方地址:https://github.com/OpenFeign/feign

本文章是系列文章中的一篇

  • 1、SpringCloud 项目基础工程搭建 【SpringCloud系列1】
  • 2、SpringCloud 集成Nacos注册中心 【SpringCloud系列2】
1 项目中添加 Feign 依赖如下
  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-openfeign</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.cloud</groupId>
  7. <artifactId>spring-cloud-starter-loadbalancer</artifactId>
  8. </dependency>

需要注意 排除 discovery 中默认的 ribbon

  1. <dependency>
  2. <groupId>com.alibaba.cloud</groupId>
  3. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  4. <exclusions>
  5. <exclusion>
  6. <groupId>org.springframework.cloud</groupId>
  7. <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
  8. </exclusion>
  9. </exclusions>
  10. </dependency>

然后在 启动类中添加 @EnableFeignClients 注解,如我这里的 order-service

  1. import org.mybatis.spring.annotation.MapperScan;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.openfeign.EnableFeignClients;
  5. @EnableFeignClients
  6. @SpringBootApplication
  7. @MapperScan(basePackages = "com.biglead.orderservice.mapper")
  8. public class OrderServiceApplication {
  9. public static void main(String[] args) {
  10. SpringApplication.run(OrderServiceApplication.class, args);
  11. }
  12. }
2 Feign 客户端的使用

在order-service中新建一个 FeignUserClient 接口,内容如下:

  1. import com.biglead.common.pojo.UserInfo;
  2. import org.springframework.cloud.openfeign.FeignClient;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.PathVariable;
  5. @FeignClient("user-service")
  6. public interface FeignUserClient {
  7. @GetMapping("/user/{id}")
  8. UserInfo queryById(@PathVariable("id") Long id);
  9. }

user-service 为服务名称,在nacos注册中心对应的服务名称
在这里插入图片描述
然后在 订单 Service中查询 订单关联的用户信息如下:

  1. @Service
  2. @Slf4j
  3. public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderInfo> implements OrderService {
  4. @Resource
  5. private FeignUserClient feignUserClient;
  6. /**
  7. * 查询订单详情
  8. *
  9. * @param id
  10. */
  11. @Override
  12. public OrderInfo queryById(Long id) {
  13. OrderInfo orderInfo = this.getById(id);
  14. if (orderInfo==null) {
  15. throw new RuntimeException("未查询到订单");
  16. }
  17. Long userId = orderInfo.getUserId();
  18. //根据用户ID来查询用户信息
  19. UserInfo userInfo = feignUserClient.queryById(userId);
  20. orderInfo.setUserInfo(userInfo);
  21. return orderInfo;
  22. }
  23. }

然后接口查询订单的详情
在这里插入图片描述

3 公共类 common-api 抽取

在父工程类类目下创建一个空的maven 项目,然后将用户类 UserInfo 放到公共类中
在这里插入图片描述
然后在 order-service 与 user-service 中加入 common-api 的依赖

  1. <dependency>
  2. <groupId>com.biglead</groupId>
  3. <artifactId>common-api</artifactId>
  4. <version>1.0-SNAPSHOT</version>
  5. </dependency>
4 Feign整合HttpClient依赖

首先是 pom.xml 中添加依赖

  1. <!--httpClient连接池的依赖 -->
  2. <dependency>
  3. <groupId>io.github.openfeign</groupId>
  4. <artifactId>feign-httpclient</artifactId>
  5. </dependency>

然后在

  1. spring:
  2. feign:
  3. client:
  4. config:
  5. default: # 这里用default就是全局配置,如果是写服务名称,则是针对某个微服务的配置
  6. loggerLevel: FULL # 日志级别四种NONE、BASIC、HEADERS、FULL
  7. httpclient:
  8. enabled: true # 开启feign对HttpClient的支持
  9. max-connections: 200 # 最大的连接数
  10. max-connections-per-route: 50 # 每个路径的最大连接数

本文章是系列文章,代码过多,可以查看源码如下:
https://gitee.com/android.long/spring-cloud-biglead/tree/master/biglead-api-03-feign
有兴趣可以关注一下 公众号 biglead

发表评论

表情:
评论列表 (有 0 条评论,77人围观)

还没有评论,来说两句吧...

相关阅读