环境要求
Java8及以上
Maven 3.3及以上:https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started.html#getting-started-system-requirements
学习资料
• 文档地址: https://www.yuque.com/atguigu/springboot
• 文档不支持旧版本IE、Edge浏览器,请使用chrome或者firefox
• 视频地址: http://www.gulixueyuan.com/ https://www.bilibili.com/video/BV19K4y1L7MT?p=1
• 源码地址:https://gitee.com/leifengyang/springboot2
为什么用SpringBoot
2.1、SpringBoot优点
• Create stand-alone Spring applications
• 创建独立Spring应用
• Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
• 内嵌web服务器
• Provide opinionated ‘starter’ dependencies to simplify your build configuration
• 自动starter依赖,简化构建配置
• Automatically configure Spring and 3rd party libraries whenever possible
• 自动配置Spring以及第三方功能
• Provide production-ready features such as metrics, health checks, and externalized configuration
• 提供生产级别的监控、健康检查及外部化配置
• Absolutely no code generation and no requirement for XML configuration
• 无代码生成、无需编写XML
2.2、SpringBoot缺点
• 人称版本帝,迭代快,需要时刻关注变化
• 封装太深,内部原理复杂,不容易精通
3、时代背景
- 微服务是一种架构风格
- 一个应用拆分为一组小型服务
- 每个服务运行在自己的进程内,也就是可独立部署和升级
- 服务之间使用轻量级HTTP交互
- 服务围绕业务功能拆分
- 可以由全自动部署机制独立部署
- 去中心化,服务自治。服务可以使用不同的语言、不同的存储技术
分布式的困难
• 远程调用
• 服务发现
• 负载均衡
• 服务容错
• 配置管理
• 服务监控
• 链路追踪
• 日志管理
• 任务调度
分布式的解决SpringBoot + SpringCloud
上云的困难
• 服务自愈
• 弹性伸缩
• 服务隔离
• 自动化部署
• 灰度发布
• 流量治理
• …
4、如何学习SpringBoot
4.1、官网文档架构
5、入门
(1)maven设置
在maven的安装目录中设置阿里云镜像与使用的jdk
<mirrors>
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
(2)HelloWorld
需求:浏览发送/hello请求,响应 Hello,Spring Boot 2
创建maven工程
引入依赖
引入parent项目(必须),添加web启动场景
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
创建主程序
/**
* 主程序类
* @SpringBootApplication:这是一个SpringBoot应用
*/
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class,args);
}
}
编写业务
@RestController
public class HelloController {
@RequestMapping("/hello")
public String handle01(){
return "Hello, Spring Boot 2!";
}
}
测试
直接运行main方法
简化配置
在resource中新建application.properties,可以添加配置。
server.port=8888
简化部署
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
把项目打成jar包,直接在目标服务器执行即可。