服务拆分原则
单一职责原则
单一职责原则原本是面向对象设计的一个基本原则,是指一个类应该专注于单一的功能,不要存在多于一个导致类变更的原因
在微服务架构中,是指一个微服务只负责一个功能或者业务领域,每个服务应该由清晰的定义和边界,只关注自己的特定业务领域。
服务自治
服务自治是指每个微服务都应该具备高度自治的能力,即每个服务要做到独立开发,独立测试,独立构建,独立部署,独立运行
单向依赖
微服务之间需要做到单向依赖,严禁循环依赖,双向依赖
父子工程
微服务的搭建,我们这里使用父子工程。
我们先创建一个空项目:
这个项目就是我们的父项目,之后在这个父项目下创建我们的子项目即可
父工程的 pom 文件的完善:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>spring-cloud-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>order-service</module>
<module>product-service</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<java.version>17</java.version>
<mybatis.version>3.0.3</mybatis.version>
<mysql.version>8.0.33</mysql.version>
<spring-cloud.version>2022.0.3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>${mybatis.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
DependencyManagement 和 Dependencies 的标签的介绍
DependencyManagement :只是一个声明的标签,并没有将里面的 jar 包导入进去。
如果子项目需要用到相关的依赖,需要显式声明,也就说使用 < dependencies> 标签 来进行导入
如果子项目没有指定版本的话,就会从父项目读取 version 版本,如果子项目中指定了版本号,那就会导入指定的 jar 包
父工程的打包方式应该是 pom 而不是 jar,所以这里需要指定 pom 打包:
< packaging>pom< /packaging>
Dependencies 这个标签就是将所依赖的 jar 直接加入到项目中,父项目的 Dependencies 引入的 jar 包会被子项目继承。
RestTemplate
当我们要进行服务与服务之间的调用的时候,可能就会使用到 RestTemplate
RestTemplate 是 Spring 3.0 开始支持的一个 HTTP 请求工具,它是一个同步的 REST API 客户端工具,提供了 常见的 REST 请求方案的模板。
REST 的介绍
REST 是表现层资源状态转移
资源:网络上所有的事务可以抽象为资源,每个资源都有唯一的资源标识符 【URL】
表现层:资源的表现形式:txt、xml、json、html 等等
状态转移:访问 URL ,也就说客户端和服务器的交互过程,我们通过网络访问资源,对资源进行增删改等操作的时候,都会引起资源状态的变化。
REST 描述的是网络中 Client 和 Server 的一种交互方式,REST 本身不实用,实用的是如何设计 RESTful API
演示:
我们先创建 RestTemplate 对象并交给 Spring 管理
@Configuration
public class BeanConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
这里有两个类,其中 ProductDetailInfo 是在另一个服务中获取的,因此我们要使用 RestTemplate 来进行调用。
@Data
public class OrderInfo {
private Integer id;
private Long userId;
private Long productId;
private Integer num;
private Long price;
private int deleteFlag;
private Date createTime;
private Date updateTime;
private ProductDetailInfo productDetailInfo;
}
@Data
public class ProductDetailInfo {
private Integer id;
private String productName;
private Long productPrice;
private Integer state;
private Date createTime;
private Date updateTime;
}
@Service
public class OrderService {
@Autowired
private OrderMapper orderMapper;
@Autowired
private RestTemplate restTemplate;
public OrderInfo getOrderById(Integer orderId) {
OrderInfo orderInfo = orderMapper.selectById(orderId);
String url = "http://127.0.0.1:9090/product/" + orderInfo.getProductId();
ProductDetailInfo productDetailInfo = restTemplate.getForObject(url, ProductDetailInfo.class);
orderInfo.setProductDetailInfo(productDetailInfo);
return orderInfo;
}
}
问题
在远程调用的时候,我们通过 http://127.0.0.1:9090/product/ ,这个 url 是写死的,如果我们需要更换 ip 的话,就需要频繁修改代码。
在实际开发中,如果业务的需求和 RESTful API 不太匹配或者很麻烦的话,我们可以不用 RESTful API,除此之外,还有很多问题(负载均衡等),后面会介绍如何使用 spring cloud 来解决上述的问题。