news 2026/9/8 9:13:03

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

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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"?> 4.0.0 org.example cloud-shop pom 1.0-SNAPSHOT cloud-shop-provider cloud-shop-consumer <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> org.springframework.boot spring-boot-starter-web com.alibaba druid-spring-boot-starter 1.2.8 mysql mysql-connector-java org.springframework.boot spring-boot-starter-jdbc org.projectlombok lombok com.baomidou mybatis-plus-boot-starter ${mybatis-plus.version} true com.baomidou mybatis-plus-generator org.springframework.boot spring-boot-dependencies 2.2.2.RELEASE pom import org.springframework.cloud spring-cloud-dependencies Hoxton.SR1 pom import org.springframework.boot spring-boot-maven-plugin true 3.准备一张数据表(任意数据都行)4. **对服务提供者进行操作**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/Shanghai username: root password: 1234 mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl type-aliases-package: com.chq.csdn.entity mapper-locations: classpath:mapping/xmlentity@Data @AllArgsConstructor @NoArgsConstructor public class Payment extends Model { private Long id; private String serial; }PaymentDao@Mapper public interface PaymentDao extends BaseMapper { }PaymentServicepublic interface PaymentService extends IService { }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); } } 5. **启动消费者项目访问localhost:8001/payment/insertPayment**数据库数据访问localhost:7001/payment/getPayment6. **在对服务消费者进行操作再次编写一次entity,这里就有点重复操作了,实际开发时,我们可以将实体类通通放入一个模块进行调用,这里方便理解,就再写一遍** @Data @AllArgsConstructor @NoArgsConstructor public class Payment extends Model { private Long id; private String serial; }进行服务调用这里我们使用RestTemplate,准备好RestTemplate配置类@Configuration public class ApplicationContextConfig { @Bean public RestTemplate restTemplate(){ return new RestTemplate(); } }ConsumerController@RestController @RequestMapping(“/consumer”) public class ConsumerController extends ApiController { //服务端口号这里建议使用127.0.0.1,不建议使用localhost private static final String HOST = “http://127.0.0.1:8001”; @Autowired private 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); } } 7. **启动成功后**访问localhost:8002/consumer/payment/insert**查看数据表访问localhost:8002/consumer/payment/get/id此时服务调用就结束了,我们可以发现消费者consumer没有配置数据源,却通过RestTemplate访问消费者服务,从而间接访问数据库,这就是服务的调用。* *服务注册中心扮演者一个仓储功能,服务提供者提供服务注册到注册中心,服务消费者通过http消息或组件到注册中心(而非RestTemplate)去找到所需的服务,这也就是服务中心,服务消费者和服务提供者三者关系
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/1 4:10:27

ComfyUI 工作流迁移指南:三步完成备份、恢复与分享

ComfyUI 工作流迁移指南&#xff1a;三步完成备份、恢复与分享 【免费下载链接】ComfyUI The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. 项目地址: https://gitcode.com/GitHub_Trending/co/ComfyUI ComfyUI 是一…

作者头像 李华
网站建设 2026/8/30 23:55:35

MATLAB实现层次分析法:从理论到实战的量化决策指南

1. 项目概述&#xff1a;从决策困境到量化工具做项目、评职称、选方案&#xff0c;甚至挑手机、定旅游目的地&#xff0c;我们每天都在做各种决策。当面对的因素多起来&#xff0c;比如要选一款软件&#xff0c;既要考虑功能是否强大&#xff0c;又要看价格是否合适&#xff0c…

作者头像 李华