md_files/自学/微服务.md

50 lines
2.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 微服务
## 认识微服务
微服务架构,首先是服务化,就是将单体架构中的功能模块从单体应用中拆分出来,独立部署为多个服务。
<img src="https://pic.bitday.top/i/2025/05/20/iyyuzk-0.png" alt="image-20250520114708790" style="zoom:67%;" />
**SpringCloud**
![image-20250520123727017](https://pic.bitday.top/i/2025/05/20/kgm91d-0.png)
使用Spring Cloud 2021.0.x以及Spring Boot 2.7.x版本需要对应
<img src="https://pic.bitday.top/i/2025/05/20/knttz4-0.png" alt="image-20250520124938379" style="zoom:80%;" />
<img src="https://pic.bitday.top/i/2025/05/20/knvz3s-0.png" alt="image-20250520124948604" style="zoom:80%;" />
在父pom中的`<dependencyManagement>`锁定版本,使得后续你在子模块里引用 Spring Cloud 或 Spring Cloud Alibaba 的各个组件时,不需要再写 `<version>`Maven 会统一采用你在父 POM 中指定的版本。
## 微服务拆分
微服务拆分时:
- **高内聚**:每个微服务的职责要尽量单一,包含的业务相互关联度高、完整度高。
- **低耦合**:每个微服务的功能要相对独立,尽量减少对其它微服务的依赖,或者依赖接口的稳定性要强。
<img src="https://pic.bitday.top/i/2025/05/20/m06nzx-0.png" alt="image-20250520133100419" style="zoom:67%;" />
**一般微服务项目有两种不同的工程结构:**
- [ ] 完全解耦:每一个微服务都创建为一个**独立的工程**,甚至可以使用不同的开发语言来开发,项目完全解耦。
- 优点:服务之间耦合度低
- 缺点:每个项目都有自己的独立仓库,管理起来比较麻烦
- [x] **Maven聚合**整个项目为一个Project然后每个微服务是其中的**一个Module**
- 优点:项目代码集中,管理和运维方便
- 缺点:服务之间耦合,编译时间较长
IDEA配置小技巧
1.自动导包
![image-20250520182745862](https://pic.bitday.top/i/2025/05/20/u81lcq-0.png)
2.配置service窗口以显示多个微服务启动类