• 微服务基础---认识微服务


    1.1认识微服务

    1.1.1微服务架构演变

    单体架构

    将业务的所有功能都集中在一个项目中进行开发,打成一个包部署.

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

    分布式架构

    根据业务功能对系统进行拆分,每个业务模块作为独立项目开发,称为一个服务

    • 优点:降低服务耦合,有利于服务升级拓展

    服务治理

    分布式架构要考虑的问题:

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

    微服务

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

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

    优点:拆分粒度更小、服务更独立、耦合度更低

    缺点:架构非常复杂,运维、监控、部署难度提高

    微服务结构

    微服务需要技术框架来实现,比较出名的是SpringCloud和alibaba的Dubbo

    微服务技术对比

    image-20220907142421544

    企业需求:

    image-20220907143041960

    1.1.2SpringCloud

    SpringCloud集成了各种微服务功能组件,并基于SpringBoot实现了这些组件的自动装配,从而提供了了良好的开箱即用:

    image-20220907193229298

    1.2分布式服务架构案例

    服务拆分注意事项:

    • 不同微服务,不用重复开发相同业务
    • 微服务数据独立,不要访问其他微服务的数据库
    • 微服务可以将自己的业务暴露接口,提供给其他微服务使用

    项目cloud-demo1

    git地址 https://gitee.com/xn2001/cloudcode/tree/master/01-cloud-demo

    image-20220907203127583

    根据订单id查询订单功能

    需求:根据订单id查询订单的同时,把订单所属的用户信息一起返回

    远程调用方式分析:

    image-20220907203403669

    1. 注册RestTemplate

    在order-service的OrderApplication中添加RestTemplate配置类

    	/**
         * 创建RestTemplate并注入spring容器
         * @return
         */
        @Bean
        public RestTemplate restTemplate(){
            return new RestTemplate();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    1. 更改orderService业务代码
    package com.xn2001.order.service;
    
    import com.xn2001.order.mapper.OrderMapper;
    import com.xn2001.order.pojo.Order;
    import com.xn2001.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请求,查询用户
            //2.1url路径
            String url = "http://localhost:8081/user/"+order.getUserId();
            //2.2发送http请求,实现远程调用,getForObject get请求
            User user = restTemplate.getForObject(url, User.class);
            //3.封装user到Order
            order.setUser(user);
            // 4.返回
            return order;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    打开浏览器测试:localhost:8080/order/101

    测试结果:

    image-20220907204428309

    总结:

    想要远程调用,实际上就是一个基于http的请求

  • 相关阅读:
    Qt实现单例模式:Q_GLOBAL_STATIC和Q_GLOBAL_STATIC_WITH_ARGS
    【@EnableAspectJAutoProxy为符合切点的目标 bean 自动创建代理】
    联发科最先完成WiFi 7演示,速度是WiFi 6的2.4倍
    代码随想录算法训练营day35 | 0-1背包理论基础、416. 分割等和子集
    Redis --- 安装教程
    大数据如何进行测试
    Cortex-M系列处理器偶发死机定位方法
    Linux搭建GitLab私有仓库,并内网穿透实现公网访问
    【C语言】用纯C来创建顺序表
    MongoDB聚合运算符:$sinh
  • 原文地址:https://blog.csdn.net/YSecret_Y/article/details/126917986