为什么说 Java 程序员到了必须掌握 Spring Boot 的时候?
本文内容纲要:
-Spring历史
-SpringBoot的诞生
-什么是SpringBoot
-使用SpringBoot的优势
-SpringBoot让开发变得更简单
-SpringBoot使测试变得更简单
-SpringBoot让配置变得更简单
-SpringBoot让部署变得更简单
-SpringBoot让监控变得更简单
-Spring、SpringBoot和SpringCloud的关系
-总结
SpringBoot2.0的推出又激起了一阵学习SpringBoot热,就单从我个人的博客的访问量大幅增加就可以感受到大家对学习SpringBoot的热情,那么在这么多人热衷于学习SpringBoot之时,我自己也在思考:SpringBoot诞生的背景是什么?Spring企业又是基于什么样的考虑创建SpringBoot?传统企业使用SpringBoot会给我们带来什么样变革?
带着这些问题,我们一起来了解下SpringBoot到底是什么?
Spring历史
说起SpringBoot我们不得不先了解一下Spring这个企业,不仅因为SpringBoot来源于Spirng大家族,而且SpringBoot的诞生和Sping框架的发展息息相关。
时间回到2002年,当时正是JavaEE和EJB大行其道的时候,很多知名公司都是采用此技术方案进行项目开发。这时候有一个美国的小伙子认为EJB太过臃肿,并不是所有的项目都需要使用EJB这种大型框架,应该会有一种更好的方案来解决这个问题。
为了证明他的想法是正确的,于2002年10月甚至写了一本书《ExpertOne-on-OneJ2EE》,介绍了当时Java企业应用程序开发的情况,并指出了JavaEE和EJB组件框架中存在的一些主要缺陷。在这本书中,他提出了一个基于普通Java类和依赖注入的更简单的解决方案。
在书中,他展示了如何在不使用EJB的情况下构建高质量,可扩展的在线座位预留系统。为了构建应用程序,他编写了超过30,000行的基础结构代码,项目中的根包命名为com.interface21,所以人们最初称这套开源框架为interface21,也就是Spring的前身。
他是谁呢,他就是大名鼎鼎的RodJohnson(下图),RodJohnson在悉尼大学不仅获得了计算机学位,同时还获得了音乐学位,更令人吃惊的是在回到软件开发领域之前,他还获得了音乐学的博士学位。现在RodJohnson已经离开了Spring,成为了一个天使投资人,同时也是多个公司的董事,早已走上人生巅峰。
在这本书发布后,一对一的J2EE设计和开发一炮而红。这本书免费提供的大部分基础架构代码都是高度可重用的。2003年RodJohnson和同伴在此框架的基础上开发了一个全新的框架命名为Spring,据RodJohnson介绍Spring是传统J2EE新的开始。随后Spring发展进入快车道。
- 2004年03月,1.0版发布。
- 2006年10月,2.0版发布。
- 2007年11月更名为SpringSource,同时发布了Spring2.5。
- 2009年12月,Spring3.0发布。
- 2013年12月,Pivotal宣布发布Spring框架4.0。
- 2017年09月,Spring5.0发布。
SpringBoot的诞生
随着使用Spring进行开发的个人和企业越来越多,Spring也慢慢从一个单一简洁的小框架变成一个大而全的开源软件,Spring的边界不断的进行扩充,到了后来Spring几乎可以做任何事情了,市面上主流的开源软件、中间件都有Spring对应组件支持,人们在享用Spring的这种便利之后,也遇到了一些问题。
Spring每集成一个开源软件,就需要增加一些基础配置,慢慢的随着人们开发的项目越来越庞大,往往需要集成很多开源软件,因此后期使用Spirng开发大型项目需要引入很多配置文件,太多的配置非常难以理解,并容易配置出错,到了后来人们甚至称Spring为配置地狱。
Spring似乎也意识到了这些问题,急需有这么一套软件可以解决这些问题,这个时候微服务的概念也慢慢兴起,快速开发微小独立的应用变得更为急迫,Spring刚好处在这么一个交叉点上,于2013年初开始的SpringBoot项目的研发,2014年4月,SpringBoot1.0.0发布。
SpringBoot诞生之初,就受到开源社区的持续关注,陆续有一些个人和企业尝试着使用了SpringBoot,并迅速喜欢上了这款开源软件。直到2016年,在国内SpringBoot才被正真使用了起来,期间很多研究SpringBoot的开发者在网上写了大量关于SpringBoot的文章,同时有一些公司在企业内部进行了小规模的使用,并将使用经验分享了出来。从2016年到2018年,使用SpringBoot的企业和个人开发者越来越多,我们从SpringBoot关键字的百度指数就可以看出。
上图为2014年到2018年SpringBoot的百度指数,可以看出SpringBoot2.0的推出引发了搜索高峰。
当然SpringBoot不是为了取代Spring,SpringBoot基于Spring开发,是为了让人们更容易的使用Spring。看到SpringBoot的市场反应,Spring官方也非常重视SpringBoot的后续发展,已经将SpringBoot作为公司最顶级的项目来推广,放到了官网上第一的位置,因此后续SpringBoot的持续发展也被看好。
什么是SpringBoot
SpringBoot介绍
SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。用我的话来理解,就是SpringBoot其实不是什么新的框架,它默认配置了很多框架的使用方式,就像maven整合了所有的jar包,SpringBoot整合了所有的框架(不知道这样比喻是否合适)。
SpringBoot简化了基于Spring的应用开发,通过少量的代码就能创建一个独立的、产品级别的Spring应用。SpringBoot为Spring平台及第三方库提供开箱即用的设置,这样你就可以有条不紊地开始。SpringBoot的核心思想就是约定大于配置,多数SpringBoot应用只需要很少的Spring配置。采用SpringBoot可以大大的简化你的开发模式,所有你想集成的常用框架,它都有对应的组件支持。
SpringBoot特性
- 使用Spring项目引导页面可以在几秒构建一个项目
- 方便对外输出各种形式的服务,如RESTAPI、WebSocket、Web、Streaming、Tasks
- 非常简洁的安全策略集成
- 支持关系数据库和非关系数据库
- 支持运行期内嵌容器,如Tomcat、Jetty
- 强大的开发包,支持热启动
- 自动管理依赖
- 自带应用监控
- 支持各种IED,如IntelliJIDEA、NetBeans
SpringBoot这些特性会给我们研发带来非常大的优势,下面我们可以分开来介绍:
使用SpringBoot的优势
使用SpringBoot开发项目,会给我们带来非常美妙的开发体验,可以从以下几个方面展开来说明
SpringBoot让开发变得更简单
SpringBoot对开发效率的提升是全方位的,我们可以简单做一下对比:
在没有使用SpringBoot之前我们开发一个web项目需要做哪些工作:
- 1)配置web.xml,加载Spring和Springmvc
- 2)配置数据库连接、配置Spring事务
- 3)配置加载配置文件的读取,开启注解
- 4)配置日志文件
- …
- n)配置完成之后部署tomcat调试
可能你还需要考虑各个版本的兼容性,jar包冲突的各种可行性。
那么使用SpringBoot之后我们需要开发一个web项目需要哪些操作呢?
- 1)登录网址http://start.spring.io/选择对应的组件直接下载
- 2)导入项目,直接开发
上面的N步和下面的2步形成巨大的反差,这仅仅只是在开发环境搭建的这个方面。
SpringBoot使测试变得更简单
SpringBoot对测试的支持不可谓不强大,SpringBoot内置了7种强大的测试框架:
- JUnit:一个Java语言的单元测试框架
- SpringTest&SpringBootTest:为SpringBoot应用提供集成测试和工具支持
- AssertJ:支持流式断言的Java测试框架
- Hamcrest:一个匹配器库
- Mockito:一个javamock框架
- JSONassert:一个针对JSON的断言库
- JsonPath:JSONXPath库
我们只需要在项目中引入spring-boot-start-test
依赖包,就可以对数据库、Mock、Web等各种情况进行测试。
SpringBootTest中包含了我们需要使用的各种测试场景,满足我们日常项目的测试需求。
SpringBoot让配置变得更简单
SpringBoot让配置变简单,说到这里我们就需要了解一下SpringBoot的核心思想:约定优于配置。那么什么是约定优于配置呢?
约定优于配置(conventionoverconfiguration),也称作按约定编程,是一种软件设计范式,旨在减少软件开发人员需做决定的数量,获得简单的好处,而又不失灵活性。
本质是说,开发人员仅需规定应用中不符约定的部分。例如,如果模型中有个名为User的类,那么数据库中对应的表就会默认命名为user。只有在偏离这一约定时,例如将该表命名为”user_info”,才需写有关这个名字的配置。
SpringBoot让部署变得更简单
说起SpringBoot让部署变简单,就不得不说SpringBoot内嵌容器。内嵌容器不只让部署变得简单,其实在开发调试阶段也会带来非常大的便利性,对比以往开发Web项目时配置Tomcat的繁琐,会让大家使用SpringBoot内嵌容器开发时有更深的感触。使用SpringBoot开发Web项目,让我们不需要关心容器的环境问题,专心写业务代码即可。
Jenkins是目前持续构建领域使用最广泛的工具之一,Jenkins是一个独立的开源自动化服务器,可用于自动化各种任务,如构建,测试和部署软件。Jenkins可以通过本机系统包Docker安装,甚至可以通过安装JavaRuntimeEnvironment的任何机器独立运行。
说直白一点Jenkins就是专门来负责如何将代码变成可执行的程序包,将它部署到目标服务器中,并对其运营状态(日志)进行监控的软件。自动化、性能、打包、部署、发布、发布结果自动化验证、接口测试、单元测试等等关于我们打包测试部署的方方面面Jenkins都可以很友好的支持。
使用Jenkins部署SpringBoot项目非常简单,大家想继续了解可以参考我的文章:使用Jenkins部署SpringBoot,只需要前期做一些简单的配置,当我们需要发布项目时只需要点击项目对应的发布按钮,就可以将项目从版本库中拉取、打包、发布到目标服务器中,大大简化了运维后期的部署工作。
虚拟化技术的发展给我们带来了更多的可能性,我们可以利用容器化技术,将SpringBoot项目做成镜像,根据容器集群的策略来实现弹性扩容、动态部署等。所以SpringBoot+Docker+Jenkins会将SpringBoot项目的部署做得更简单化、智能化。
SpringBoot让监控变得更简单
可以说SpringBoot就是一款自带监控的开源软件,在设计之初就考虑到应用的监控问题,专门提供了一款监控组件来完成这个工作,这个组件就是
SpringBootActuator。
SpringBootActuator是SpringBoot提供的对应用系统监控的集成功能,可以查看应用配置的详细信息,例如自动化配置信息、创建的Springbeans以及一些环境属性等。
当然SpringBootActuator虽然可以监控一个SpringBoot应用的健康情况,实际上现在的系统都是需要很多的服务相互配合来完成工作,如何通过一个监控软件来监控所以的SpringBoot项目将变得比较紧迫。
在开源界也有人意识到了这个问题,并且基于Springbootactuator做出了一款强大的监控软件,这个软件就是SpringBootadmin。
SpringBootAdmin是一个管理和监控SpringBoot应用程序的开源软件。每个应用都认为是一个客户端,通过HTTP或者使用Eureka注册到adminserver中进行展示,SpringBootAdminUI部分使用AngularJs将数据展示在前端。
SpringBootAdmin是一个针对spring-boot的actuator接口进行UI美化封装的监控工具。他可以:在列表中浏览所有被监控spring-boot项目的基本信息,详细的Health信息、内存信息、JVM信息、垃圾回收信息、各种配置信息(比如数据源、缓存列表和命中率)等,还可以直接修改logger的level。
使用SpringBootAdmin不仅可以监控SpringBoot项目,还可以监控SpringCloud项目,因此使用了SpringBoot项目之后我们监控SpringBoot集群效果如下:
简单、直观、易用是它的特点,针对一些特殊情况还可以提供报警服务。所以说使用SpringBootActuator解决了单个SpringBoot的监控问题,使用SpringBootAdmin就是解决了整个集群监控的问题。
Spring、SpringBoot和SpringCloud的关系
Spring最初最核心的两大核心功能SpringIoc和SpringAop成就了Spring,Spring在这两大核心的功能上不断的发展,才有了Spring事务、SpirngMvc等一系列伟大的产品,最终成就了Spring帝国,到了后期Spring几乎可以解决企业开发中的所有问题。
SpringBoot是在强大的Spring帝国生态基础上面发展而来,发明SpringBoot不是为了取代Spring,是为了让人们更容易的使用Spring。所以说没有Spring强大的功能和生态,就不会有后期的SpringBoot火热,SpringBoot使用约定优于配置的理念,重新重构了Spring的使用,让Spring后续的发展更有生命力。
SpringCloud是一系列框架的有序集合。它利用SpringBoot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用SpringBoot的开发风格做到一键启动和部署。
Spring并没有重复制造轮子,它只是将目前各家公司开发的比较成熟、经得起实际考验的服务框架组合起来,通过SpringBoot风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂、易部署和易维护的分布式系统开发工具包。
根据上面的说明我们可以看出来,SpringCloud是为了解决微服务架构中服务治理而提供的一系列功能的开发框架,并且SpringCloud是完全基于SpringBoot而开发,SpringCloud利用SpringBoot特性整合了开源行业中优秀的组件,整体对外提供了一套在微服务架构中服务治理的解决方案。
综上我们可以这样来理解,正是由于SpringIoc和SpringAop两个强大的功能才有了Spring,Spring生态不断的发展才有了SpringBoot,使用SpringBoot让Spring更易用更有生命力,SpringCloud是基于SpringBoot开发的一套微服务架构下的服务治理方案。
用一组不太合理的包含关系来表达它们之间的关系。
Springioc/aop>Spring>SpringBoot>SpringCloud
给大家推荐一个专门收集SpringBoot学习资源的网站:SpringBoot中文索引
总结
不知道什么时候起,行业里一些开发人员愿意相信,使用复杂的软件就意味着采用了高深的技术;使用了大量的配置,就意味着软件有着很多比较强大的功能。在产品设计的时候有一个理念就是让产品操作足够的傻瓜化,假设用户是一个智商并不高的群体,却可以使他很容易的学会使用其产品,将此特性做为产品设计的一项标准之一。
其实我们的开源软件也是一款产品,繁琐并不意味着功能强大,反而有可能是设计不够合理;简洁也并不意味着简单,很有可能它只是将众多复杂的功能进行了封装,让我们在使用的时候足够的简单。好的产品如此,好的开源软件也应该如此,SpringBoot的出现就是让编程变得更简单一些。
在此引用Python的经典设计格言,格言来源于Python但不限于Python。
美丽优于丑陋。
清楚优于含糊。
简单优于复杂。
复杂优于繁琐。
平坦优于曲折。
宽松优于密集。
重要的是可读性。
特殊的案例不足以特殊到破坏规则。
尽管实践可以打破真理。
错误却不可置之不理。
除非另有明确要求。
面对模棱两可,拒绝猜测。
总会有一个——最好是只有一个——显而易见的方式来明辨。
哪怕这种方式在开始的时候可能并不明显。
现在有比没有好。
尽管没有经常好于现在。
如果如何实现很难被解释清楚,那么这个想法就是一个坏想法。
如果如何实现可以被很好的解释,那么这是一个好想法。
文章摘录于我在gitchat上的分享
本文内容总结:Spring历史,SpringBoot的诞生,什么是SpringBoot,使用SpringBoot的优势,SpringBoot让开发变得更简单,SpringBoot使测试变得更简单,SpringBoot让配置变得更简单,SpringBoot让部署变得更简单,SpringBoot让监控变得更简单,Spring、SpringBoot和SpringCloud的关系,总结,
原文链接:https://www.cnblogs.com/ityouknow/p/9175980.html