一、Spring与SpringBoot
前言:
环境要求
Java8及以上
Maven 3.3及以上
https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started.html#getting-started-system-requirements
需要注意:Java8版本及以下建议用SpringBoot1,在 SPringBoot2.x 版本底层用的是 Spring5.x,要求 JDK 的版本也必须是 1.8 及以上,所以 SpringBoot 里面有一些新特,如支持 Lambda 表达式,接口的方法有默认实现等。
1、Spring能做什么
1.1、Spring的能力
当然Spring的功能不仅仅局限于此
1.2、Spring的生态圈
https://spring.io/projects/spring-boot
覆盖了:
web开发
数据访问
安全控制
分布式
消息服务
移动开发
批处理
…
1.3、Spring5重大升级
1.3.1、响应式编程
通过异步数据流,用少量的线程占用少量的资源,处理大量的并发
1.3.2、内部源码设计
基于Java8的一些新特性,如:接口默认实现(区别于以前的适配器,不用适配器也可以实现重写)。重新设计源码架构。
2、为什么用SpringBoot
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.
能快速创建出生产级别的Spring应用
2.1、SpringBoot优点
-
Create stand-alone Spring applications
-
-创建独立Spring应用
-
Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
-
内嵌web服务器,每一个工程都可以打包成一个 jar 包,其中内置了 tomcat 或其它 servlet 容器,可以独立运行(无需部署 war 文件),这是和微服务理念最为契合的一点。
-
Provide opinionated ‘starter’ dependencies to simplify your build configuration
-
自动starter依赖,简化构建配置,只需导入一个场景,
-
Automatically configure Spring and 3rd party libraries whenever possible
-
自动配置Spring以及第三方功能,每一个特定场景下的需求都封装成了一个 starter,只要导入这个 starter(场景启动器中含有项目启动的所有依赖) 就有了这个场景所需要的一切,其中 包含针对这个场景的自动化配置、依赖信息。
-
Provide production-ready features such as metrics, health checks, and externalized configuration
-
提供生产级别的监控、健康检查及外部化配置(只需要修改配置文件)
-
Absolutely no code generation and no requirement for XML configuration
-
无代码生成、无需编写XML,SpringBoot 是对 Spring 的进一步封装,基于注解开发,舍弃了笨重的 XML,确实需要配置的使用 yml 或 properties 进行简要配置即可,即可完成 Spring 的自动配置。
SpringBoot是整合Spring技术栈的一站式框架
SpringBoot是简化Spring技术栈的快速开发脚手架(搭建好所有的东西,只需要开发自己的业务。)
2.2、SpringBoot缺点
- 人称版本帝,迭代快,需要时刻关注变化
- 封装太深,内部原理复杂,不容易精通
3、时代背景
3.1、微服务
James Lewis and Martin Fowler (2014) 提出微服务完整概念。https://martinfowler.com/microservices/
In short, the microservice architectural style is an approach to developing a single application as a suite of small services, each running in its own process and communicating with lightweight mechanisms, often an HTTP resource API. These services are built around business capabilities and independently deployable by fully automated deployment machinery. There is a bare minimum of centralized management of these services, which may be written in different programming languages and use different data storage technologies.-- James Lewis and Martin Fowler (2014)
- 微服务是一种架构风格
- 一个应用拆分为一组小型服务
- 每个服务运行在自己的进程内,也就是可独立部署和升级
- 服务之间使用轻量级HTTP交互
- 服务围绕业务功能拆分
- 可以由全自动部署机制独立部署
- 去中心化,服务自治。服务可以使用不同的语言、不同的存储技术
3.2、分布式
A是订单微服务
B是购物车微服务
D是用户微服务
分布式的困难
- 远程调用:A想要调用B通过http实现远程调用
- 服务发现:A想要调B,B在四台机器都有,是服务发现
- 负载均衡:A调用B的四台机器都可以,涉及到负载均衡
- 服务容错:B出现网络错误不能用怎么办,都不能用A返回一个兜底数据
- 配置管理:想修改A的配置,不可能把十几台机器的配置都修改了,应该把所有的配置放到一个配置中心里面,每一个机器自动同步配置
- 服务监控:这么多服务上到云平台,进行监控
- 链路追踪:A->B->C->D 一旦有一个出现问题,搜索整个链路是哪一个导致的
- 日志管理:大型应用的日志怎么进行管理
- 任务调度:定时任务是一个机器触发还是十几台机器一起触发,并行还是串行
- …
分布式的解决
- SpringBoot + SpringCloud
3.3、云原生
原生应用如何上云。 Cloud Native
上云的困难
- 服务自愈
- 弹性伸缩
- 服务隔离
- 自动化部署
- 灰度发布
- 流量治理
- …
上云的解决
4、如何学习SpringBoot
完全可以自己看官方文档进行学习
4.1、官网文档架构
查看版本新特性;
https://github.com/spring-projects/spring-boot/wiki#release-notes