轻松搭建SpringCloudAlibaba分布式微服务
SpringCloudAlibaba随笔目录
一、
二、
三、
四、
五、
六、
七、
八、
九、
十、
SpringCloudAlibaba项目父工程搭建
1、微服务
什么是微服务? 维基上对其定义为:一种软件开发技术- 面向服务的体系结构(SOA)架构样式的一种变体,它提倡将单一应用程序划分成一组小的服务,服务之间互相协调、互相配合,为用户提供最终价值。每个服务运行在其独立的进程中,服务与服务间采用轻量级的通信机制互相沟通(通常是基于HTTP的RESTful API)。每个服务都围绕着具体业务进行构建,并且能够独立地部署到生产环境、类生产环境等。另外,应尽量避免统一的、集中式的服务管理机制,对具体的一个服务而言,应根据上下文,选择合适的语言、工具对其进行构建。 微服务是一种用于构建应用的架构方案。微服务架构有别于更为传统的单体式方案,可将应用拆分成多个核心功能。每个功能都被称为一项服务,可以单独构建和部署,这意味着各项服务在工作(和出现故障)时不会相互影响。2、SpringCloud介绍
Spring Cloud是一系列框架的有序集合。它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用Spring Boot的开发风格做到一键启动和部署。Spring Cloud并没有重复制造轮子,它只是将各家公司开发的比较成熟、经得起实际考验的服务框架组合起来,通过Spring Boot风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂、易部署和易维护的分布式系统开发工具包。
3、SpringCloudAlibaba
很多人可能会问,有了spring cloud这个微服务的框架,为什么又要使用spring cloud alibaba这个框架了?最重要的原因在于spring cloud中的几乎所有的组件都使用Netflix公司的产品,然后在其基础上做了一层封装。然而Netflix的服务发现组件Eureka已经停止更新,我们公司在使用的时候就发现过其一个细小的Bug;而其他的众多组件预计会在明年(即2020年)停止维护。所以急需其他的一些替代产品,也就是spring cloud alibaba,目前正处于蓬勃发展的态式。
Spring Cloud Alibaba是Spring Cloud下的一个子项目,Spring Cloud Alibaba为分布式应用程序开发提供了一站式解决方案,它包含开发分布式应用程序所需的所有组件,使您可以轻松地使用Spring Cloud开发应用程序,使用Spring Cloud Alibaba,您只需要添加一些注解和少量配置即可将Spring Cloud应用程序连接到Alibaba的分布式解决方案,并使用Alibaba中间件构建分布式应用程序系统。Spring Cloud Alibaba 是阿里巴巴开源中间件跟 Spring Cloud 体系的融合。
官方链接
官方自述:https://github.com/alibaba/spring-cloud-alibaba/blob/master/README-zh.md
官方github:https://github.com/alibaba/spring-cloud-alibaba
官方Samples:https://github.com/alibaba/spring-cloud-alibaba/tree/master/spring-cloud-alibaba-examples
版本兼容
我们需要关注的紧紧是版本兼容问题
SpringCloud和SpringBoot的版本兼容:
https://spring.io/projects/spring-cloud
SpringCloudAlibaba和SpringBoot的版本兼容:
https://github.com/alibaba/spring-cloud-alibaba/blob/master/README-zh.md
SpringCloudAlibaba官方整理的版本说明:
https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E
| Spring Cloud Version | Spring Cloud Alibaba Version | Spring Boot Version |
|---|---|---|
|
Spring Cloud 2020.0.1 |
2021.1 |
2.4.2 |
|
Spring Cloud Hoxton.SR12 |
2.2.7.RELEASE |
2.3.12.RELEASE |
|
Spring Cloud Hoxton.SR9 |
2.2.6.RELEASE |
2.3.2.RELEASE |
|
Spring Cloud Greenwich.SR6 |
2.1.4.RELEASE |
2.1.13.RELEASE |
|
Spring Cloud Hoxton.SR3 |
2.2.1.RELEASE |
2.2.5.RELEASE |
|
Spring Cloud Hoxton.RELEASE |
2.2.0.RELEASE |
2.2.X.RELEASE |
|
Spring Cloud Greenwich |
2.1.2.RELEASE |
2.1.X.RELEASE |
|
Spring Cloud Finchley |
2.0.4.RELEASE(停止维护,建议升级) |
2.0.X.RELEASE |
|
Spring Cloud Edgware |
1.5.1.RELEASE(停止维护,建议升级) |
1.5.X.RELEASE |
组件版本关系:https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E
| Spring Cloud Alibaba Version | Sentinel Version | Nacos Version | RocketMQ Version | Dubbo Version | Seata Version |
|---|---|---|---|---|---|
|
2.2.7.RELEASE* |
1.8.1 |
2.0.3 |
4.6.1 |
2.7.13 |
1.3.0 |
|
2.2.6.RELEASE |
1.8.1 |
1.4.2 |
4.4.0 |
2.7.8 |
1.3.0 |
|
2021.1 or 2.2.5.RELEASE or 2.1.4.RELEASE or 2.0.4.RELEASE |
1.8.0 |
1.4.1 |
4.4.0 |
2.7.8 |
1.3.0 |
|
2.2.3.RELEASE or 2.1.3.RELEASE or 2.0.3.RELEASE |
1.8.0 |
1.3.3 |
4.4.0 |
2.7.8 |
1.3.0 |
|
2.2.1.RELEASE or 2.1.2.RELEASE or 2.0.2.RELEASE |
1.7.1 |
1.2.1 |
4.4.0 |
2.7.6 |
1.2.0 |
|
2.2.0.RELEASE |
1.7.1 |
1.1.4 |
4.4.0 |
2.7.4.1 |
1.0.0 |
|
2.1.1.RELEASE or 2.0.1.RELEASE or 1.5.1.RELEASE |
1.7.0 |
1.1.4 |
4.4.0 |
2.7.3 |
0.9.0 |
|
2.1.0.RELEASE or 2.0.0.RELEASE or 1.5.0.RELEASE |
1.6.3 |
1.1.1 |
4.4.0 |
2.7.3 |
0.7.1 |
这里我们使用的版本:
Spring Cloud Version:Hoxton.SR12
Spring Cloud Alibaba Version:2.2.7.RELEASE
Spring Boot Version:2.3.12.RELEASE
4、快速搭建
准备
开发工具:IntelliJ IDEA 2018.3.2 x64
java:jdk1.8
Maven:apache-maven-3.6.1
要求
熟悉springboot
构建父工程,整个项目是由maven进行管理,这里使用默认的初始化服务地址,不然后面选择Maven POM创建项目时会出问题。
父工程目录:
修改pom文件
<?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.0modelVersion> <groupId>com.qtgroupId> <artifactId>spring-cloud-alibabaartifactId> <version>1.0-SNAPSHOTversion> <properties> <java.version>1.8java.version> <spring.boot.version>2.3.12.RELEASEspring.boot.version> <spring.cloud.alibaba>2.2.7.RELEASEspring.cloud.alibaba> <spring.cloud>Hoxton.SR12spring.cloud> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding> properties> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starterartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope> dependency> dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>${spring.boot.version}version> <type>pomtype> <scope>importscope> dependency> <dependency> <groupId>com.alibaba.cloudgroupId> <artifactId>spring-cloud-alibaba-dependenciesartifactId> <version>${spring.cloud.alibaba}version> <type>pomtype> <scope>importscope> dependency> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-dependenciesartifactId> <version>${spring.cloud}version> <type>pomtype> <scope>importscope> dependency> dependencies> dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> plugin> plugins> build> project>
此时父工程就搭建完成了,就是这么简单。
整个项目是由maven进行管理的, 依赖包就是我们开发时需要用到的第三方jar包, 也就是框架, 这里为什么什么也不选, 因为我们要做的是微服务, 所以框架结构为一个基座+多个子模块, 我们上面建立的父工程就是基座, 你可以把它当成一个工作空间用途是管理子模块。
最近在项目结束之余,自学了一下微服务,自主搭建springcloud项目,特此留作笔记,,,