2024.5.6 关于 SpringCloud 的基本认知

目录

引言

微服务框架所包含的技术栈

微服务架构演变

单体架构

分布式架构

微服务架构

微服务技术对比

[认识 SpringCloud](#认识 SpringCloud)

[SpringBoot 版本兼容关系](#SpringBoot 版本兼容关系)

服务拆分和远程调用

服务拆分注意事项

远程调用


引言

  • 微服务是一种框架风格,按照业务板块来划分应用代码,使单个应用的职责更清晰,相互之间可以做到独立升级迭代

微服务框架所包含的技术栈

微服务架构演变

单体架构

  • 将业务的所有功能集中在一个项目中开发,打成一个包部署
  • 适合小型项目

总结:

  • 优 ------> 架构简单 、部署成本低
  • 缺 ------> 耦合度高

分布式架构

  • 根据业务功能对系统进行拆分,每个业务模块作为独立项目开发,称为一个服务
  • 适合大型互联网项目

总结:

  • 优 ------> 降低服务耦合度、利于服务升级拓展

分布式框架需要考虑的问题:

  • 服务拆分的粒度如何?
  • 服务集群地址如何维护?
  • 服务之间如何实现远程调用?
  • 服务健康状态如何感知?

微服务架构

  • 微服务是一种经过良好架构设计的分布式架构方案

特征:

  • 面向服务 ------> 微服务对外暴露业务接口
  • 隔离性强 ------> 服务调用做好隔离、容错、降级,避免出现级联问题
  • 单一职责 ------> 微服务拆分粒度更小,每一个服务均对应唯一的业务能力,避免重复业务开发
  • 自治 ------> 团队独立、技术独立、数据独立、部署独立

总结:

  • 优点 ------> 拆分粒度更小、服务更独立、耦合度更低
  • 缺点 ------> 框架非常复杂、运维、监控、部署难度提高

微服务技术对比

  • 国内最知名微服务技术框架的就是 SpringCloud 和 阿里巴巴的 Dubbo

|-------------|---------------------|-----------------------------|-----------------------------|
| | Dubbo | SpringCloud | SpringCloudAlibaba |
| 注册中心 | zookeeper、Redis | Eureka、Consul | Nacos、Eureka |
| 服务器远程调用 | Dubbo协议 | Feign(http协议) | Dubbo、Feign |
| 配置中心 | | SpringCloudConfig | SpringCloudConfig、Nacos |
| 服务网关 | | SpringCloudGateway、Zuul | SpringCloudGateway、Zuul |
| 服务监管和保护 | dubbo-admin,功能弱 | Hystrix | Sentinel |

认识 SpringCloud

  • SpringCloud 集成了各种微服务功能组件
  • SpringCloud 给予 SpringBoot 实现了各种组件的自动装配,达到了开箱即用体验的效果

SpringBoot版本兼容关系

服务拆分和远程调用

服务拆分注意事项

  1. 不同微服务,不要重复开发相同业务
  2. 微服务数据独立,不要访问其他微服务的数据库
  3. 微服务可以将自己的业务暴露为接口,供其他微服务调用

实例理解

  • 此处我们将该服务拆分成了 订单模块 和 用户模块
  • 每个微服务均有属于自己的数据库
  • 由于在不同的数据库中,因此只能通过订单 id 查询订单数据,或者通过用户 id 查询用户数据,无法交叉访问

问题:

  • 如何在根据订单 id 查询订单的时,将该订单所属用户的用户信息一并返回呢?
  • 即如何在 订单模块 中远程调用 用户模块 中的接口?

远程调用

  • 注册 RestTemplate
  • 在 order-service 的 OrderApplication 中(SpringBoot 启动类)注册 RestTemplate
java 复制代码
package cn.itcast.order;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;


//@MapperScan 注解的作用相当于在指定包下的所有 mapper 接口上都加上了 @mapper 注解
@MapperScan("cn.itcast.order.mapper")
@SpringBootApplication
public class OrderApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }

    /**
     * 将 RestTemplate 对象注入到 spring 容器中
     * @return
     */
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
  • 远程调用 RestTemplate
  • 在 order-service 中,通过 RestTemplate 的 getForObject 方法构造 get 请求并发送
  • user-service 收到请求并返回对应数据,最后 getForObject 方法收到数据并反序列化为指定类型
java 复制代码
package cn.itcast.order.service;

import cn.itcast.order.mapper.OrderMapper;
import cn.itcast.order.pojo.Order;
import cn.itcast.order.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RestTemplate restTemplate;

    public Order queryOrderById(Long orderId) {
//        1、查询订单
        Order order = orderMapper.findById(orderId);
//        2、利用 RestTemplate 发起 http 请求,查询用户
//          a) url 路径
        String url = "http://localhost:8081/user/" + order.getUserId();
//          b) 发起 http 请求,实现远程调用
//      get请求:getForObject
//      post请求:postForObject
//      第一个参数是 url, 第二个参数是请求后响应的参数类型(自动的反序列化)
        User user = restTemplate.getForObject(url, User.class);
//        3、封装 User 到 Order
        order.setUser(user);
//        4、返回
        return order;
    }
}

运行结果:

相关推荐
用户2986985301416 分钟前
.NET 文档自动化:Spire.Doc 设置奇偶页页眉/页脚的最佳实践
后端·c#·.net
序安InToo1 小时前
第6课|注释与代码风格
后端·操作系统·嵌入式
xyy1231 小时前
C#: Newtonsoft.Json 到 System.Text.Json 迁移避坑指南
后端
洋洋技术笔记1 小时前
Spring Boot Web MVC配置详解
spring boot·后端
JxWang051 小时前
VS Code 配置 Markdown 环境
后端
navms1 小时前
搞懂线程池,先把 Worker 机制啃明白
后端
JxWang051 小时前
离线数仓的优化及重构
后端
Nyarlathotep01131 小时前
gin01:初探gin的启动
后端·go
JxWang051 小时前
安卓手机配置通用多屏协同及自动化脚本
后端
JxWang051 小时前
Windows Terminal 配置 oh-my-posh
后端