今天一起说说微服务,之前写docker多服务部署的时候也说微服务,一起说说Spring boot快速开始及核心功能介绍。源码:https://github.com/limingios/netFuture/tree/master/源码/『互联网架构』软件架构-解密电商系统-Spring boot快速开始及核心功能介绍(上)(84)/
(一)Spring boot认知,核心功能
一般的框架出现主要是为了解决问题的。微服务和springboot出现,是一个非常重大的更新,spring已经没落了很久的(也可以说整个java),自从springboot出现spring二次发育第二春。吐糟:java开发起来非常的笨重,没有动态语言go,python,nodejs轻。每次搞java的spring配置一坨一坨的,用技术也需要配置很多,管理很多的配置,开发效率低下。
- 微架构
- 1.快速集成
- 2.开发快速
- 3.启动快速
- 4.部署快速
- 5.测试部署
spring的微架构落地的实现springboot。
- Spring Boot是为了简化Spring应用的创建、运行、调试、部署等而出现的,使用它可以做到专注于Spring应用及业务的开发,而无需过多关注XML的配置[理想情况零配置] (0配置只是说说基本不可能,可以通过0配置来配置应用,但是我们应用还是要做一点点的配置)
- 简单来说,它提供了一堆Starter POM依赖包,并按照使用习惯解决了一些依赖问题及配置问题[习惯优于约定](反例:契约优先就是定义WebService接口,先定义wsdl的xml,然后在开始写代码。)。
- Spring Boot默认使用内嵌的tomcat作为应用服务器,使用logback作为日志记录。
- Spring Boot提供了一系列的依赖包,所以需要构建工具的支持:maven 或 gradle。
核心功能
- 以jar包方式独立运行(jar -jar xxx.jar) (只需要安装个jdk就可以运行)。
- 内嵌Servlet容器(tomcat, jetty),无 需以war包形式部署到独立的servlet容器中 。
- 提供starter简化maven依赖包配置。
- 自动装配bean(大多数场景) 。
- 零配置(理论上),Spring 4.x新特性,提倡使用java配置和注解配置结合而无需xml配置。
(二) springBoot的搭建
- 官方向导搭建boot应用
https://start.spring.io/springboot的一个搭建的向导。
点击Generate Project
下载的springboot-first解压,只用idea打开
idea导入完毕
新建立包com.idig8.springboot.controller
新建立controller
编写HelloController
package com.idig8.springboot.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;/** * @program: springboot-first * @description: controller文件编写 * @author: LiMing * @create: 2019-06-04 22:09 **/@Controllerpublic class HelloController { @RequestMapping("/") @ResponseBody public String index(){ return "Hello world!"; }}
修改SpringbootFirstApplication,增加扫描包
package com.idig8.springboot.springbootfirst;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.ComponentScan;@SpringBootApplication@ComponentScan(basePackages = {"com.idig8.springboot.controller"})public class SpringbootFirstApplication { public static void main(String[] args) { SpringApplication.run(SpringbootFirstApplication.class, args); }}
进入SpringbootFirstApplication如下图选择运行
springboot 已经成功启动,端口8080扫描到了对应/ 对应的HelloController的index方法启动时间2秒就启动就可以了。
访问:http://127.0.0.1:8080
从上边的看,是不是感觉做个web开发很简单,1分钟搞定。
- 普通maven工程搭建boot应用
新建一个普通的maven工程【注意:Spring boot是web工程,但是我们这里只需要建立quickstart即可,因为spring boot内嵌了servlert容器】
查看官方文档:https://projects.spring.io/spring-boot/
点击learn-选择版本1.5.10.RELEASE。建议:生产环境中选择稳定的版本
拷贝依赖的父pom到自己的工程pom文件中
org.springframework.boot spring-boot-starter-parent 1.5.10.RELEASEorg.springframework.boot spring-boot-starter-web
从上面的第一个boot项目的pom中拷贝项目构建的内容到当前工程中(以下内容为每个maven项目都必须要的)
UTF-8UTF-81.8org.springframework.boot spring-boot-maven-plugin
官方文档的例子,尝试启动springboot
import org.springframework.boot.*;import org.springframework.boot.autoconfigure.*;import org.springframework.stereotype.*;import org.springframework.web.bind.annotation.*;@RestController@EnableAutoConfigurationpublic class Example { @RequestMapping("/") String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(Example.class, args); }}
运行结果
- Starter POMs
1.Spring boot由大量starter组成,每个starter就是一个pom文件,里面包含必要的jar依赖,所以Starter主要是用来简化依赖2.Spring boot支持需要引入其提供的父POM
org.springframework.boot spring-boot-starter-parent 1.5.10.RELEASE
父POM中主要做了两件事情1.所有jar包的版本统一管理。2.所有jar的依赖管理,其中包含springboot 给我们提供的很多的starter启动器。
PS:pom中引入的parent中的spring-boot-starter-parent,进入仓库可以看到对应的里面没有jar包,只有一个pom文件,这个pom文件中有个属性dependencyManagement 这是实现定义好的依赖jar包进行版本管理的管理器。实现都定义好了需要依赖的jar。
(三)统一父POM管理
- 建立boot-parent工程
首先我们建立一个 boot-parent的maven工程
删除src目录
然后修改pom.xml。packaging改为为pom格式。
pom
加入dependencyManagement, 同时去掉version, 直接使用父pom中的版本即可。
org.springframework.boot spring-boot-parent 1.5.10.RELEASEpomimport
加入 build
org.springframework.boot spring-boot-maven-plugin
加入properties添加
UTF-8UTF-81.8
父pom完整代码
<?xml version="1.0" encoding="UTF-8"?>4.0.0com.idig8.springboot springboot-parent 1.0-SNAPSHOTpomvip-springboot-parenthttp://maven.apache.orgUTF-8UTF-81.8org.springframework.boot spring-boot-parent 1.5.10.RELEASEpomimportorg.springframework.boot spring-boot-maven-plugin
- 创建module-base项目
加入java文件
package com.idig8.springboot;/** * @program: springboot-second * @description: ${description} * @author: LiMing * @create: 2019-06-04 22:42 **/import org.springframework.boot.*;import org.springframework.boot.autoconfigure.*;import org.springframework.stereotype.*;import org.springframework.web.bind.annotation.*;@RestController@EnableAutoConfigurationpublic class Example { @RequestMapping("/") String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(Example.class, args); }}
pom文件修改
<?xml version="1.0" encoding="UTF-8"?> springboot-parent com.idig8.springboot1.0-SNAPSHOT4.0.0 com.idig8.springboot-base springboot-basehttp://maven.apache.orgUTF-8UTF-81.8org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test testorg.springframework.boot spring-boot-devtools
然后访问:http://localhost:8080/
spring boot一个很重要的特点:解决了所有依赖的版本问题。
(四)spring boot 测试
添加测试支持依赖:spring-boot-starter-test
org.springframework.boot
spring-boot-starter-test
test
注意:加入这个依赖之后,junit包就可以不用了,因为test的starter中包含了junit。备注:怎么找到所有的starter。进入官网查询Reference:https://start.spring.io/
这里面ctrl +f 搜索:starter,就可以看到spring boot中的所有starter
编写测试类
package com.idig8.springboot;import junit.framework.TestCase;import org.junit.Ignore;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.web.WebAppConfiguration;@Ignore@SpringBootTest(classes=Example.class)@WebAppConfiguration@RunWith(SpringJUnit4ClassRunner.class)public class ExampleTest { @Autowired private Example controller; // @Test public void testHome() { TestCase.assertEquals("Hello World!