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 AlibabaSpringCloud原生网关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 version1.0 encodingUTF-8? 4.0.0 org.example cloud-shop pom 1.0-SNAPSHOT cloud-shop-provider cloud-shop-consumer project.build.sourceEncodingUTF-8/project.build.sourceEncoding maven.compiler.source1.8/maven.compiler.source maven.compiler.target1.8/maven.compiler.target junit.version4.12/junit.version log4j.version1.2.17/log4j.version lombok.version1.18.22/lombok.version mysql.version8.0.24/mysql.version druid.version1.2.8/druid.version mybatis-plus.version3.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?characterEncodingutf8serverTimezoneAsia/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/xmlentityData AllArgsConstructor NoArgsConstructor public class Payment extends Model { private Long id; private String serial; }PaymentDaoMapper public interface PaymentDao extends BaseMapper { }PaymentServicepublic interface PaymentService extends IService { }PaymentServiceImplService public class PaymentServiceImpl extends ServiceImplPaymentDao, Payment implements PaymentService { }PaymentControllerRestController 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(); } }ConsumerControllerRestController 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去找到所需的服务这也就是服务中心服务消费者和服务提供者三者关系