0%

SpringCloud入门概述

SpringCloud 是什么?

Spring 官网:https://spring.io/

SpringCloud 和 SpringBoot 的关系

  • SpringBoot 专注于开苏方便的开发单个个体微服务;
  • SpringCloud 是关注全局的微服务协调整理治理框架,它将 SpringBoot 开发的一个个单体微服务,整合并管理起来,为各个微服务之间提供:配置管理、服务发现、断路器、路由、为代理、事件总栈、全局锁、决策竞选、分布式会话等等集成服务;
  • SpringBoot 可以离开 SpringCloud 独立使用,开发项目,但 SpringCloud 离不开 SpringBoot,属于依赖关系;
  • SpringBoot 专注于快速、方便的开发单个个体微服务,SpringCloud 关注全局的服务治理框架;

Dubbo 和 SpringCloud 技术选型

1. 分布式+服务治理 Dubbo

目前成熟的互联网架构,应用服务化拆分 + 消息中间件

2. Dubbo 和 SpringCloud 对比

可以看一下社区活跃度:
https://github.com/dubbo
https://github.com/spring-cloud
对比结果:

Dubbo SpringCloud
服务注册中心 Zookeeper Spring Cloud Netfilx Eureka
服务调用方式 RPC REST API
服务监控 Dubbo-monitor Spring Boot Admin
断路器 不完善 Spring Cloud Netfilx Hystrix
服务网关 Spring Cloud Netfilx Zuul
分布式配置 Spring Cloud Config
服务跟踪 Spring Cloud Sleuth
消息总栈 Spring Cloud Bus
数据流 Spring Cloud Stream
批量任务 Spring Cloud Task

最大区别:Spring Cloud 抛弃了 Dubbo 的 RPC 通信,采用的是基于 HTTP 的 REST 方式
严格来说,这两种方式各有优劣。虽然从一定程度上来说,后者牺牲了服务调用的性能,但也避免了上面提到的原生 RPC 带来的问题。而且 REST 相比 RPC 更为灵活,服务提供方和调用方的依赖只依靠一纸契约,不存在代码级别的强依赖,这个优点在当下强调快速演化的微服务环境下,显得更加合适。
品牌机和组装机的区别
社区支持与更新力度的区别
总结:二者解决的问题域不一样:Dubbo 的定位是一款 RPC 框架,而 SpringCloud 的目标是微服务架构下的一站式解决方案。

3、SpringCloud 能干嘛?

  • Distributed/versioned configuration 分布式/版本控制配置
  • Service registration and discovery 服务注册与发现
  • Routing 路由
  • Service-to-service calls 服务到服务的调用
  • Load balancing 负载均衡配置
  • Circuit Breakers 断路器
  • Distributed messaging 分布式消息管理

4、SpringCloud 下载

官网:http://projects.spring.io/spring-cloud/
版本号有点特别:
image.png
SpringCloud 没有采用数字编号的方式命名版本号,而是采用了伦敦地铁站的名称,同时根据字母表的顺序来对应版本时间顺序,比如最早的 Realse 版本:Angel,第二个 Realse 版本:Brixton,然后是 Camden、Dalston、Edgware,目前最新的是 Hoxton SR4 CURRENT GA 通用稳定版。
自学参考书:

SpringCloud Rest 学习环境搭建:服务提供者

介绍

  • 我们会使用一个 Dept 部门模块做一个微服务通用案例Consumer消费者(Client)通过 REST 调用Provider提供者(Server)提供的服务。
  • 回顾 Spring,SpringMVC,Mybatis 等以往学习的知识。
  • Maven 的分包分模块架构复习。

一个简单的 Maven 模块结构是这样的:– app-parent: 一个父项目(app-parent)聚合了很多子项目(app-util\app-dao\app-web…) |– pom.xml | |– app-core ||—- pom.xml | |– app-web ||—- pom.xml ……
一个父工程带着多个 Moudule 子模块
MicroServiceCloud 父工程(Project)下初次带着 3 个子模块(Module)

  • microservicecloud-api 【封装的整体 entity/接口/公共配置等】
  • microservicecloud-consumer-dept-80 【服务提供者】
  • microservicecloud-provider-dept-8001 【服务消费者】

SpringCloud 版本选择

大版本说明

SpringBoot SpringCloud 关系
1.2.x Angel 版本(天使) 兼容 SpringBoot1.2x
1.3.x Brixton 版本(布里克斯顿) 兼容 SpringBoot1.3x,也兼容 SpringBoot1.4x
1.4.x Camden 版本(卡姆登) 兼容 SpringBoot1.4x,也兼容 SpringBoot1.5x
1.5.x Dalston 版本(多尔斯顿) 兼容 SpringBoot1.5x,不兼容 SpringBoot2.0x
1.5.x Edgware 版本(埃奇韦尔) 兼容 SpringBoot1.5x,不兼容 SpringBoot2.0x
2.0.x Finchley 版本(芬奇利) 兼容 SpringBoot2.0x,不兼容 SpringBoot1.5x
2.1.x Greenwich 版本(格林威治)

实际开发版本关系

spring-boot-starter-parent spring-cloud-dependencles
版本号 发布日期 版本号 发布日期
1.5.2.RELEASE 2017-03 Dalston.RC1 2017-x
1.5.9.RELEASE 2017-11 Edgware.RELEASE 2017-11
1.5.16.RELEASE 2018-04 Edgware.SR5 2018-10
1.5.20.RELEASE 2018-09 Edgware.SR5 2018-10
2.0.2.RELEASE 2018-05 Fomchiey.BULD-SNAPSHOT 2018-x
2.0.6.RELEASE 2018-10 Fomchiey-SR2 2018-10
2.1.4.RELEASE 2019-04 Greenwich.SR1 2019-03

使用后两个

创建父工程

  • 新建父工程项目 springcloud,切记Packageing 是 pom 模式
  • 主要是定义 POM 文件,将后续各个子模块公用的 jar 包等统一提取出来,类似一个抽象父类
    image.png

pom.xml

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?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>com.haust</groupId>
<artifactId>springcloud</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>springcloud-api</module>
<module>springcloud-provider-dept-8001</module>
<module>springcloud-consumer-dept-80</module>
<module>springcloud-eureka-7001</module>
<module>springcloud-eureka-7002</module>
<module>springcloud-eureka-7003</module>
<module>springcloud-provider-dept-8002</module>
<module>springcloud-provider-dept-8003</module>
<module>springcloud-consumer-dept-feign</module>
<module>springcloud-provider-dept-hystrix-8001</module>
<module>springcloud-consumer-hystrix-dashboard</module>
<module>springcloud-zuul-9527</module>
<module>springcloud-config-server-3344</module>
<module>springcloud-config-client-3355</module>
<module>springcloud-config-eureka-7001</module>
<module>springcloud-config-dept-8001</module>
</modules>
<!--打包方式 pom-->
<packaging>pom</packaging>
<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.16.18</lombok.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--springCloud的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--SpringBoot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.4.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!--SpringBoot 启动器-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--日志测试~-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>