SpringBoot第三方服务开发实践:版本兼容性和依赖管理

本文最后更新于:1 年前

就像春天的花朵,无论经历了多少寒冬的洗礼,都会在春风的吹拂下绽放出最灿烂的笑容。

前言

  • 在使用 Spring Boot 整合第三方服务的开发过程中,我们常常面临着各种挑战和问题。不管是版本不兼容、依赖包导入错误还是其他种种原因,每一次踩坑都是一次宝贵的经验。本篇博客旨在记录我在使用Spring Boot 整合第三方服务时所遇到的踩坑记录,并与大家分享解决方案和经验

  • 作为一个 Spring Boot 开发者,我深知在实际开发过程中,整合第三方服务经常成为一项必要的任务。然而,第三方服务的集成往往并不是一件简单的事情。我曾在整合过程中遇到了版本不适配问题maven 依赖包导入错误等一系列问题。这些问题不仅费时费力,还会延缓项目的进度和给开发团队带来不必要的压力。

  • 通过撰写此博客,我希望能把我在整合第三方服务时踩过的坑以及所得到的经验分享给那些即将或正在面临类似问题的开发者们。

  • 我希望通过分享解决方案和经验,能够帮助大家避免一些常见的问题,节省时间和精力。

  • 🥇 推荐阅读(2023/10/23 晚)
💢 SpringBoot 整合 Mybatis

正文

版本不适配问题

踩坑经历

  • 在开发 API 接口开放平台时,使用到 Dubbo 框架+Nacos 注册中心实现远程服务调用(2023/08/24 午)
  • 有关这方面的知识,可以在官网了解到:

  • 也可以在《API 开放平台-开发文档》《从零开始构建分布式服务架构:用 Dubbo 和注册中心实现远程调用、服务注册与发现、配置管理》中了解学习

  • 以下是项目中最初引入的依赖包:

1
2
3
4
5
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>3.0.9</version>
</dependency>
1
2
3
4
5
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>2.1.0</version>
</dependency>
  • 但是启动项目后,一直报错:
1
register failed
  • 即服务注册失败
  • 我想到了也许是因为依赖包的版本不适配,所以提高了两个依赖包的版本:
1
2
3
4
5
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>3.1.8</version>
</dependency>
1
2
3
4
5
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>2.2.1</version>
</dependency>
  • 但直到此时,项目依旧报错,服务注册失败
  • 那既然修改服务提供者的依赖版本无效,会不会是注册中心的问题呢?

  • Nacos 快速开始
  • 我下载了最新版本的 Nacos 注册中心,重新启动 Nacos:

image-20230824131844391

  • 再次启动项目,服务注册成功:(2023/08/24 午)

image-20230824132539814

整合第三方服务

SpringBoot 整合 Mybatis

  • SpringBoot 整合 Mybatis 后,如何实现手写 SQL 语句,执行数据库操作?(2023/09/01 早)
  • 在项目中导入依赖:
1
2
3
4
5
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
  • application.yaml 中编写配置:
1
2
3
4
5
6
7
8
spring:
# 数据库配置
# todo 需替换配置
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/memory_api
username: *******
password: *******
  • 第一种方法:在 mapper 包下添加注解,自主编写 SQL(注解法):
1
2
@Select(value = "select * from user where userRole = 'admin'")
List<User> userList();
  • 第二种方法:在 mapper.xml 下编写 SQL,通过 id 映射至 mapper 包下的方法(配置法):
1
2
3
<select id="userList2" resultType="com.example.memorycommen.model.entity.User">
select * from user where userRole = #{role};
</select>
1
List<User> userList2(String role);
  • 测试:在 controller 下编写两个接口,开启接口文档发送请求:
1
2
3
4
5
6

@GetMapping("/list")
public BaseResponse<List<User>> getUser() {
List<User> users = userMapper.userList();
return ResultUtils.success(users);
}
1
2
3
4
5
6

@GetMapping("/list2")
public BaseResponse<List<User>> getUser2() {
List<User> users = userMapper.userList2("admin");
return ResultUtils.success(users);
}
  • 测试结果完美,都查询出了 userRole 为管理员的用户:(2023/09/01 早)

image-20230901124032605

image-20230901124037675

总结

-


SpringBoot第三方服务开发实践:版本兼容性和依赖管理
https://test.atomgit.net/blog/2023/08/23/SpringBoot第三方服务开发实践:版本兼容性和依赖管理/
作者
Memory
发布于
2023年8月23日
更新于
2023年10月23日
许可协议