当前位置: 首页 > news >正文

SpringCloud基础 入门级 学习SpringCloud 超详细(简单通俗易懂)

Spring Cloud 基础入门级学习 超详细(简单通俗易懂)

  • 一、SpringCloud核心组件
    • 第一代:SpringCloud Netflix组件
    • 第二代:SpringCloud Alibaba组件
    • SpringCloud原生组件
  • 二、SpringCloud体系架构图
  • 三、理解分布式与集群
    • 分布式
    • 集群
  • 四、最简单的样例实现微服务调用


在这里插入图片描述

一、SpringCloud核心组件

SpringCloud是在SpringBoot的基础上,增加了很多微服务相关的规范。目前,SpringCloud规范已经由Spring官方、SpringCloud Netflix、SpringCloud Alibaba等实现。通过组件化的方式,SpringCloud将这些实现整合起来,构成全家桶式的微服务技术栈。

第一代:SpringCloud Netflix组件

目前SpringCloud Netflix已经停止维护

组件名称作用
Eureka服务注册与发现组件
Ribbon负载均衡组件
Feign声明式服务调用
Hystrx熔断器组件
ZuulAPI网关组件

第二代:SpringCloud Alibaba组件

Spring Cloud Alibaba是阿里开源的一套Sping Cloud规范的实现,配置比 NetFlix 更简单易用。

组件名称作用
nacos服务注册与发现 配置中心 流量控制 (Eureka + config)
Sentinel容错保护组件
Dubbo服务治理 负载均衡与容错 远程调用 服务降级
RecketMQ消息中间件
Seata分布式事务

SpringCloud原生组件

组件名称作用
Consul服务注册
Zookeeper服务发现和配置管理
Config分布式配置中心
Sleuth/Zipkin分布式链路追踪
GatewayAPI服务网关(新一代API网关,提供路由,负载均衡,过滤等)
OpenFeign声明式服务调用
Stream消息驱动 简化消息的发送和接收
Bus消息总线
LoadBalancer客户端负载均衡

在这里插入图片描述

目前市场主流选择:

  • SpringCloud Alibaba+SpringCloud原生
  • 网关:Spring Cloud GateWay
  • 服务注册与发现、配置中心:SpringCloud Alibaba Nacos
  • 服务间调用:Spring Cloud OpenFeign
  • 负载均衡:Spring Cloud LoadBalance
  • 客户端容错保护:SpringCloud Alibaba Sentinel
  • 消息中间件:RabbitMQ

二、SpringCloud体系架构图

请添加图片描述

三、理解分布式与集群

分布式

分布式:把一个大业务拆分成多个子业务,每个子业务都是一套独立的系统,子业务之间相互协作最终完成整体的大业务

比如: 小明是一家烧烤店的烧烤师傅,每天不仅要,准备食材,准备配料,烧烤(可以看作一个单体架构)。 后来小明烤的太好吃了,客人也多了,又专门雇了两位师傅小红,李华,小红专门准备食材,李华专门准备配料,小明专门烧烤。小明,小红,李华这三者的关系就是分布式。

在这里插入图片描述

分工完成一件事,就是分布式

集群

集群:在几个服务器上部署相同的应用程序来分担客户端的请求。

比如: 小明不雇用小红和李华了,找到了自己的好朋友烧烤师傅 王刚来帮忙,王刚每天工作和小明一样要准备食材,准备配料,烧烤。此时王刚和小明的关系就是集群

在这里插入图片描述

各自做同一件事情,就是集群

四、最简单的样例实现微服务调用

  1. 创建shop父模块 和 shop-provider(服务提供者) 和 shop-consumer(服务消费者)三个maven模块
    请添加图片描述
  2. 父依赖pom.xml (锁定Springboot和SpringCloud版本 和 写一些通用的依赖)
<?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>cloud-shop</artifactId><packaging>pom</packaging><version>1.0-SNAPSHOT</version><modules><module>cloud-shop-provider</module><module>cloud-shop-consumer</module></modules><!-- 统一管理jar包版本 --><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><junit.version>4.12</junit.version><log4j.version>1.2.17</log4j.version><lombok.version>1.18.22</lombok.version><mysql.version>8.0.24</mysql.version><druid.version>1.2.8</druid.version><mybatis-plus.version>3.0.7.1</mybatis-plus.version></properties><dependencies><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- https://mvnrepository.com/artifact/com.alibaba/druid --><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.8</version></dependency><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>${mybatis-plus.version}</version><optional>true</optional><exclusions><exclusion><groupId>com.baomidou</groupId><artifactId>mybatis-plus-generator</artifactId></exclusion></exclusions></dependency></dependencies><!-- 子模块继承之后,提供作用:锁定版本 + 子 modlue 不用写 groupId和version  --><dependencyManagement><dependencies><!--spring boot 2.2.2--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.2.2.RELEASE</version><type>pom</type><scope>import</scope></dependency><!--spring cloud Hoxton.SR1--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR1</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><addResources>true</addResources></configuration></plugin></plugins></build></project>
  1. 准备一张数据表(任意数据都行)
    在这里插入图片描述
  2. 对服务提供者进行操作

application.yml文件

server:port: 8001  #服务端口号
spring:application:name: cloud-shop-provider    #服务名称datasource:type: com.alibaba.druid.pool.DruidDataSource      #当前数据源操作类型driver-class-name: com.mysql.cj.jdbc.Driver          #mysql驱动包url: jdbc:mysql://127.0.0.1:3306/csdn?characterEncoding=utf8&serverTimezone=Asia/Shanghaiusername: rootpassword: 1234
mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpltype-aliases-package: com.chq.csdn.entitymapper-locations: classpath:mapping/*xml

entity

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment extends Model<Payment> {private Long id;private String serial;
}

PaymentDao

@Mapper
public interface PaymentDao extends BaseMapper<Payment> {
}

PaymentService

public interface PaymentService extends IService<Payment> {}

PaymentServiceImpl

@Service
public class PaymentServiceImpl extends ServiceImpl<PaymentDao, Payment> implements PaymentService {}

PaymentController

@RestController
@RequestMapping("/payment")
public class PaymentController extends ApiController {/*** 通过主键查询单条数据* @param id 主键* @return 单条数据*/@GetMapping("/getPayment/{id}")public R selectOne(@PathVariable Serializable id) {return success(this.paymentService.getById(id));}/*** 新增数据* @param payment 实体对象* @return 新增结果*/@PostMapping("/insertPayment")public R insert(@RequestBody Payment payment) {return success(this.paymentService.save(payment));}

启动类

@SpringBootApplication
@MapperScan("com.chq.csdn.dao")
public class ShopProviderApplication {public static void main(String[] args) {SpringApplication.run(ShopProviderApplication.class,args);}
}
  1. 启动消费者项目

访问localhost:8001/payment/insertPayment
在这里插入图片描述
数据库数据
在这里插入图片描述

访问localhost:7001/payment/getPayment
在这里插入图片描述

  1. 在对服务消费者进行操作

再次编写一次entity,这里就有点重复操作了,实际开发时,我们可以将实体类通通放入一个模块进行调用,这里方便理解,就再写一遍

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment extends Model<Payment> {private Long id;private String serial;
}

进行服务调用这里我们使用RestTemplate,准备好RestTemplate配置类

@Configuration
public class ApplicationContextConfig {@Beanpublic RestTemplate restTemplate(){return new RestTemplate();}
}

ConsumerController

@RestController
@RequestMapping("/consumer")
public class ConsumerController extends ApiController {//服务端口号这里建议使用127.0.0.1,不建议使用localhostprivate static final String HOST = "http://127.0.0.1:8001";@Autowiredprivate RestTemplate restTemplate;//还是刚才的方法 获取 和 添加@GetMapping("/payment/get/{id}")public R get(@PathVariable("id") Long id){//拼接结果 http://127.0.0.1:8001/payment/getPayment/id//R.class是返回类型return restTemplate.getForObject(HOST + "/payment/getPayment/" + id,R.class);}@PostMapping("/payment/insert")public R insert(@RequestBody Payment payment){//payment是请求参数return restTemplate.postForObject(HOST + "/payment/insertPayment" ,payment,R.class);}
}

application.yml文件 注意这里的配置文件,端口号改变,并且没有配置数据源,无法对数据库直接操作

server:port: 8002  #服务端口号
spring:application:name: cloud-shop-consumer    #服务名称

启动类


//因为我们添加了数据库的依赖,却没有配置数据源 加上
//(exclude = {DataSourceAutoConfiguration.class, DruidDataSourceAutoConfigure.class})
//可以忽略数据源启动
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, DruidDataSourceAutoConfigure.class})
public class ShopConsumerApplication {public static void main(String[] args) {SpringApplication.run(ShopConsumerApplication.class,args);}
}
  1. 启动成功后

访问localhost:8002/consumer/payment/insert
在这里插入图片描述

查看数据表

在这里插入图片描述

访问localhost:8002/consumer/payment/get/id
在这里插入图片描述

此时服务调用就结束了,我们可以发现消费者consumer没有配置数据源,却通过RestTemplate访问消费者服务,从而间接访问数据库,这就是服务的调用。


服务注册中心扮演者一个仓储功能,服务提供者提供服务注册到注册中心,服务消费者通过http消息或组件到注册中心(而非RestTemplate)去找到所需的服务,这也就是服务中心,服务消费者和服务提供者三者关系

在这里插入图片描述

相关文章:

SpringCloud基础 入门级 学习SpringCloud 超详细(简单通俗易懂)

Spring Cloud 基础入门级学习 超详细&#xff08;简单通俗易懂&#xff09; 一、SpringCloud核心组件第一代&#xff1a;SpringCloud Netflix组件第二代&#xff1a;SpringCloud Alibaba组件SpringCloud原生组件 二、SpringCloud体系架构图三、理解分布式与集群分布式集群 四、…...

【Windows 常用工具系列 20 -- MobaXterm 登录 WSL】

文章目录 MobaXterm 登录 WSL MobaXterm 登录 WSL 在 WSL 启动之后&#xff0c;打开 MobaXterm&#xff1a; 在 Distribution 中选择自己本地安装的 ubuntu 版本&#xff0c;我这里使用的是ubuntu-20.4&#xff0c;然后在 runmethod 中选择 Localhost connection. 连接成功之…...

【vmware+ubuntu16.04】ROS学习_博物馆仿真克隆ROS-Academy-for-Beginners软件包处理依赖报错问题

首先安装git 进入终端&#xff0c;输入sudo apt-get install git 安装后&#xff0c;创建一个工作空间名为tutorial_ws&#xff0c; 输入 mkdir tutorial_ws#创建工作空间 cd tutorial_ws#进入 mkdir src cd src git clone https://github.com/DroidAITech/ROS-Academy-for-Be…...

UniApp的Vue3版本中H5配置代理的最佳方法

UniApp的Vue3版本中H5项目在本地开发时需要配置跨域请求调试 最开始在 manifest.json中配置 总是报404&#xff0c;无法通过代理请求远程的接口并返回404错误。 经过验证在项目根目录创建 vite.config.js文件 vite.config.js内容: // vite.config.js import {defineConfig }…...

深入了解Pod

Pod是Kubernetes中最小的单元,它由一组、一个或多个容器组成,每个Pod还包含了一个Pause容器,Pause容器是Pod的父容器,主要负责僵尸进程的回收管理,通过Pause容器可以使同一个Pod里面的多个容器共享存储、网络、PID、IPC等。 1、Pod 是由一组紧耦合的容器组成的容器组,当然…...

基于Spider异步爬虫框架+JS动态参数逆向+隧道代理+自定义中间件的猎聘招聘数据爬取

在本篇博客中&#xff0c;我们将介绍如何使用 Scrapy 框架结合 JS 逆向技术、代理服务器和自定义中间件&#xff0c;来爬取猎聘网站的招聘数据。猎聘是一个国内知名的招聘平台&#xff0c;提供了大量的企业招聘信息和职位信息。本项目的目标是抓取指定城市的招聘信息&#xff0…...

Spring 中的 BeanDefinitionParserDelegate 和 NamespaceHandler

一、BeanDefinitionParserDelegate Spring在解析xml文件的时候&#xff0c;在遇到<bean>标签的时候&#xff0c;我们会使用BeanDefinitionParserDelegate对象类解析<bean>标签的内容&#xff0c;包括<bean>标签的多个属性&#xff0c;例如 id name class in…...

BERT模型核心组件详解及其实现

摘要 BERT&#xff08;Bidirectional Encoder Representations from Transformers&#xff09;是一种基于Transformer架构的预训练模型&#xff0c;在自然语言处理领域取得了显著的成果。本文详细介绍了BERT模型中的几个关键组件及其实现&#xff0c;包括激活函数、变量初始化…...

图论-代码随想录刷题记录[JAVA]

文章目录 前言深度优先搜索理论基础所有可达路径岛屿数量岛屿最大面积孤岛的总面积沉默孤岛Floyd 算法dijkstra&#xff08;朴素版&#xff09;最小生成树之primkruskal算法 前言 新手小白记录第一次刷代码随想录 1.自用 抽取精简的解题思路 方便复盘 2.代码尽量多加注释 3.记录…...

c#加载shellcode

本地加载bin文件 SharpPELoader项目如下&#xff1a; using System; using System.IO; using System.Runtime.InteropServices;namespace TestShellCode {internal class Program{private const uint MEM_COMMIT 0x1000;private const uint PAGE_EXECUTE_READWRITE 0x40;pr…...

HarmonyOS 开发环境搭建

HarmonyOS&#xff08;鸿蒙操作系统&#xff09;作为一种面向全场景多设备的智能操作系统&#xff0c;正逐渐在市场上崭露头角。为了进入HarmonyOS生态&#xff0c;开发者需要搭建一个高效的开发环境。本文将详细介绍如何搭建HarmonyOS开发环境&#xff0c;特别是如何安装和配置…...

【网络云计算】2024第46周周考-磁盘管理的基础知识-RAID篇

文章目录 1、画出各个RAID的结构图&#xff0c;6句话说明优点和缺点&#xff0c;以及磁盘可用率和坏盘数量&#xff0c;磁盘总的数量2、写出TCP五层模型以及对应的常用协议 【网络云计算】2024第46周周考-磁盘管理的基础知识-RAID篇 1、画出各个RAID的结构图&#xff0c;6句话说…...

深入理解 SQL_MODE 之 ANSI_QUOTES

引言 在 MySQL 数据库中&#xff0c;sql_mode 是一个重要的配置参数&#xff0c;它定义了 MySQL 应该遵循的 SQL 语法标准以及数据验证规则。其中&#xff0c;ANSI_QUOTES 是 sql_mode 中的一个重要选项&#xff0c;它改变了 MySQL 对于字符串和标识符的识别方式&#xff0c;使…...

容器技术在持续集成与持续交付中的应用

&#x1f493; 博客主页&#xff1a;瑕疵的CSDN主页 &#x1f4dd; Gitee主页&#xff1a;瑕疵的gitee主页 ⏩ 文章专栏&#xff1a;《热点资讯》 容器技术在持续集成与持续交付中的应用 容器技术在持续集成与持续交付中的应用 容器技术在持续集成与持续交付中的应用 引言 容器…...

【嵌入式软件-STM32】OLED显示屏+调试方法

目录 一、调试方式 1&#xff09;串口调试 优势 弊端 2&#xff09;显示屏调试 优势 弊端 3&#xff09;Keil调试模式 4&#xff09;点灯调试法 5&#xff09;注释调试法 6&#xff09;对照法 二、OLED简介 OLED组件 OLED显示屏 0.96寸OLED模块 OLED外观和种类…...

kubernetes简单入门实战

本章将介绍如何在kubernetes集群中部署一个nginx服务&#xff0c;并且能够对其访问 Namespace Namespace是k8s系统中一个非常重要的资源&#xff0c;它的主要作用是用来实现多套环境的资源隔离或者多租户的资源隔离。 默认情况下&#xff0c;k8s集群中的所有的Pod都是可以相…...

Python连接Mysql、Postgre、ClickHouse、Redis常用库及封装方法

博主在这里分享一些常见的python连接数据库或中间件的库和封装方案&#xff0c;希望对大家有用。 Mysql封装 #!/usr/bin/python # -*- coding: utf-8 -*- import sys import pymysql from settings import MYSQL_DB, MYSQL_PORT, MYSQL_USER, MYSQL_PASSWORD, MYSQL_HOST, EN…...

如何修改npm包

前言 开发中遇到一个问题&#xff0c;配置 Element Plus 自定义主题时&#xff0c;添加了 ElementPlusResolver({ importStyle: "sass" }) 后&#xff0c;控制台出现报错&#xff0c;这是因为 Dart Sass 2.0 不再支持使用 !global 来声明新变量&#xff0c;虽然当前…...

Django 2024全栈开发指南(三):数据库模型与ORM操作(上篇)

目录 一、模型的定义二、数据迁移三、数据表关系四、数据表操作4.1 Shell工具4.2 数据新增4.3 数据修改4.4 数据删除4.5 数据查询 Django 对各种数据库提供了很好的支持&#xff0c;包括 PostgreSQL、MySQL、SQLite 和 Oracle&#xff0c;而且为这些数据库提供了统一的 API 方法…...

低代码可视化-uniapp开关选择组件-低码生成器

开关&#xff08;Switch&#xff09;选择组件是一种用户界面元素&#xff0c;允许用户在两种状态&#xff08;通常是开/关、是/否、启用/禁用等&#xff09;之间进行切换。这种组件在移动应用、桌面软件、网页以及物联网设备中广泛应用。以下是对开关Switch选择组件的详细介绍&…...

Linux应用开发之网络套接字编程(实例篇)

服务端与客户端单连接 服务端代码 #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <arpa/inet.h> #include <pthread.h> …...

前端导出带有合并单元格的列表

// 导出async function exportExcel(fileName "共识调整.xlsx") {// 所有数据const exportData await getAllMainData();// 表头内容let fitstTitleList [];const secondTitleList [];allColumns.value.forEach(column > {if (!column.children) {fitstTitleL…...

Leetcode 3577. Count the Number of Computer Unlocking Permutations

Leetcode 3577. Count the Number of Computer Unlocking Permutations 1. 解题思路2. 代码实现 题目链接&#xff1a;3577. Count the Number of Computer Unlocking Permutations 1. 解题思路 这一题其实就是一个脑筋急转弯&#xff0c;要想要能够将所有的电脑解锁&#x…...

《基于Apache Flink的流处理》笔记

思维导图 1-3 章 4-7章 8-11 章 参考资料 源码&#xff1a; https://github.com/streaming-with-flink 博客 https://flink.apache.org/bloghttps://www.ververica.com/blog 聚会及会议 https://flink-forward.orghttps://www.meetup.com/topics/apache-flink https://n…...

今日科技热点速览

&#x1f525; 今日科技热点速览 &#x1f3ae; 任天堂Switch 2 正式发售 任天堂新一代游戏主机 Switch 2 今日正式上线发售&#xff0c;主打更强图形性能与沉浸式体验&#xff0c;支持多模态交互&#xff0c;受到全球玩家热捧 。 &#x1f916; 人工智能持续突破 DeepSeek-R1&…...

在web-view 加载的本地及远程HTML中调用uniapp的API及网页和vue页面是如何通讯的?

uni-app 中 Web-view 与 Vue 页面的通讯机制详解 一、Web-view 简介 Web-view 是 uni-app 提供的一个重要组件&#xff0c;用于在原生应用中加载 HTML 页面&#xff1a; 支持加载本地 HTML 文件支持加载远程 HTML 页面实现 Web 与原生的双向通讯可用于嵌入第三方网页或 H5 应…...

招商蛇口 | 执笔CID,启幕低密生活新境

作为中国城市生长的力量&#xff0c;招商蛇口以“美好生活承载者”为使命&#xff0c;深耕全球111座城市&#xff0c;以央企担当匠造时代理想人居。从深圳湾的开拓基因到西安高新CID的战略落子&#xff0c;招商蛇口始终与城市发展同频共振&#xff0c;以建筑诠释对土地与生活的…...

【MATLAB代码】基于最大相关熵准则(MCC)的三维鲁棒卡尔曼滤波算法(MCC-KF),附源代码|订阅专栏后可直接查看

文章所述的代码实现了基于最大相关熵准则(MCC)的三维鲁棒卡尔曼滤波算法(MCC-KF),针对传感器观测数据中存在的脉冲型异常噪声问题,通过非线性加权机制提升滤波器的抗干扰能力。代码通过对比传统KF与MCC-KF在含异常值场景下的表现,验证了后者在状态估计鲁棒性方面的显著优…...

根目录0xa0属性对应的Ntfs!_SCB中的FileObject是什么时候被建立的----NTFS源代码分析--重要

根目录0xa0属性对应的Ntfs!_SCB中的FileObject是什么时候被建立的 第一部分&#xff1a; 0: kd> g Breakpoint 9 hit Ntfs!ReadIndexBuffer: f7173886 55 push ebp 0: kd> kc # 00 Ntfs!ReadIndexBuffer 01 Ntfs!FindFirstIndexEntry 02 Ntfs!NtfsUpda…...

Django RBAC项目后端实战 - 03 DRF权限控制实现

项目背景 在上一篇文章中&#xff0c;我们完成了JWT认证系统的集成。本篇文章将实现基于Redis的RBAC权限控制系统&#xff0c;为系统提供细粒度的权限控制。 开发目标 实现基于Redis的权限缓存机制开发DRF权限控制类实现权限管理API配置权限白名单 前置配置 在开始开发权限…...