第一节:为什么越来越多的开发者使用springboot?它解决了什么问题?
1,spring4推出之前,编码存在什么问题?
(1)大量的xml文件存在项目中,配置相当繁琐;
(2)整合第三方框架的配置问题;
(3)低效的开发效率和部署效率等问题。
2,spring boot是什么?
Spring boot是伴随着spring4.0诞生的;
Spring boot就是一个内嵌Web容器(tomcat/jetty)的可执行程序的框架;
你开发的web应用不需要作为war包部署到web容器中,而是作为一个可执行程序,启动时把web服务配置好,加载起来。
3,spring boot解决了什么问题?
(1)spring boot使配置变简单
(2)spring boot使编码变简单
(3)spring boot使部署变简单
(4)spring boot使监控变简单
第二节:springboot入门例子
1,采用spring自带模板,搭建springboot。
(1)首先登录spring官网(spring.io),点击PROJECTS,在Main Projects下面点击“spring boot”,在打开的页面中,选择Overview下面的Quick start,点击Spring Initializr,在打开的页面中,按步骤选择,
(注意第6步,我们要在输入框中输入web,然后输入框下面会出现符合条件的结果,我们选择web依赖就可以了。)
若不知道哪个版本号带GA标志,可以在这里查看:
(2)生成项目,下载到本地,打开eclipse,新建一个Maven项目,命名为spring-cloud-in-action,packaging选择pom。
建立好之后,在此基础上,项目右键–》maven–>New Maven Module Projects,新建一个module,命名为springboot,建好之后,将之前在spring官网生成的项目拷贝到该springboot的目录下,然后刷新可以看到springboot-helloworld项目,选中springboot-helloworld,右键–》import–>exist Maven project -->会自动出现要导入的东西,点击finish即可导入。
若pom文件出现报错,update一下。
完成这一系列之后的操作,项目结构如下:
(3)介绍一下几个重要文件
#1#启动文件:HelloworldApplication.java
#2#pom.xml文件
#3#application.properties文件
该文件暂时没有内容,不过之后可以根据需要配置不同的内容。
(4)运行程序
直接在HelloworldApplication.java类中,右键Run as–>Java Application即可。
(5)写一个测试接口。
新建一个helloWorldController.java,里面加上如下代码:
启动项目,然后在浏览器中输入:http://127.0.0.1:8080/hello,结果如下:
2,采用eclipse自带模板,搭建springboot。
https://www.cnblogs.com/txsblog/p/8566416.html
第三节:分析spring boot的核心配置–分析常规和日志配置
配置要操作application.properties文件
(1)普通配置
比如配置端口:
(2)自定义配置
获取自定义配置,在HelloWorldController.java文件中,做如下修改:
运行效果:
(3)配置变量的引用
效果:
(4)随机值配置
效果:
5,yml配置文件
YAML是一种语言,配置文件的语言。树状结构。
#1#新建一个application.yml文件
调用方式不变。
6,日志配置
直接引入logback.xml文件即可,百度搜索可得。
第四节:分析spring boot的核心配置-多环境的配置
(1)新建两个配置文件
#1#一个是开发环境的:application-dev.properties
server.port=8080
twf.msg="hello,spring boot!"
twf.number=${random.int}
twf.param3=random number is ${twf.number}
#2#一个是测试环境的:application-test.properties
server.port=9090
twf.msg="hello,spring boot!"
twf.number=${random.int}
twf.param3=random number is ${twf.number}
然后打包,点击pom.xml–>右键–>Rus As–>maven build…,在打开的界面的Goals中输入“clean install”,然后apply–>Run。
打包成功之后,在target目录下生成了springboot-helloworld-0.0.1-SNAPSHOT.jar的可执行的jar文件。
将该jar文件拷贝到G:\springcloud\jar。
cmd打开两个控制台,都进入G:\springcloud\jar,
一个控制台输入命令:
java -jar springboot-helloworld-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
即可启动dev环境下的jar文件。
一个控制台输入命令:
java -jar springboot-helloworld-0.0.1-SNAPSHOT.jar --spring.profile.active==test
即可启动测试环境下的jar文件。
浏览器分别访问127.0.0.1:8080/hello和127.0.0.1:9090/hello,都可以访问到数据。
这就是多环境配置了。
第五节:分析spring boot的核心注解
1,@SpringBootApplication
2,@SpringBootConfiguration:和@Configuration的功能一样,用来代替xml的bean。
3,@EnableAutoConfiguration:自动加载jar的默认配置。
4,@ComponentScan:代替了context:component-scan
5,@RestController
6,@Controller
7,@ResponseBody
8,@RequestMapping
9,@Value
10,@controllerAdvice
第六节:一种springboot的异常统一处理方法
1,全局异常处理
(1)新建一个全局异常处理类,加上如下代码:
在接口中加入一行除0代码:
其中程序,效果如下:
2,自定义异常处理
(1)新建一个自定义异常类
package com.twf.springboot.helloworld.exception;
public class BusinessException extends RuntimeException{
private static final long serialVersionUID = -1931913478775595866L;
private String code;
private String msg;
public BusinessException(String code, String msg) {
super();
this.code = code;
this.msg = msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
(2)在全局异常处理类中使用这个类
(3)在接口中抛出这个自定义异常类(自定义异常信息)
(4)效果
第七节:采用actuator检查与监控spring boot的健康
(1)在pom.xml文件中加入如下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
(2)在application.properties中加入如下配置:
management.security.enabled=false
(3)启动项目,就可以访问下面这些路径
比如你访问http://localhost:9090/health,效果如下:
第八节:一种简洁可视化的监控报表-spring boot admin
(1)首先在Spring initializr中生成一个springboot项目,记得依赖要选中Actuator,然后把这个项目导入eclipse中。
(2)然后我们到Spring Boot Admin的官网 https://github.com/codecentric/spring-boot-admin, 往下拉找到Reference Guide,选择一个版本,点击进入,然后找到这段内容:
(3)配置文件中,设置端口为8090,然后启动项目。这就算是一个服务端了。
(4)设置客户端。我们以springboot-helloworld作为客户端,然后根据如下指示修改文件:
启动项目,在浏览器输入 http://127.0.0.1:8090 就可以看到如下的界面,点击details,可以查看很多详细信息。
这就是可视化的监控报表。