文章目录
为什么要有SpringBoot
- 前面的文章中说到, Spring是一个非常方便的开发框架, 在开发过程中基于IOC技术可以极简化我们的开发流程, 但是Spring有一个缺点就是在任务较多, 使用频繁的时候, 效率就是降低, 所以才有了SpringBoot这个微服务框架
- SpingBoot可以是秒级别的创建项目, 创建项目的速度很快 而且无需xml文件配置, 可以直接运行文件, 其内部提供了内置的容器, 可以直接运行项目和发布项目
- 还有一点就是今天学到的东西提供了监控的指标, 方便监控项目的运行情况, 提供了起步的依赖, 急速添加框架的支持
实现缓存
缓存的优点
- 使⽤缓存可以避免访问数据库,节省数据库服务器的资源
- 性能更快,缓存属于内存级的服务器,⽽ DB 牵扯到更多的业务逻辑判断和磁盘操作,因此缓存的性能更⾼。
- 数据库一般不能做集群,而缓存redis做集群非常简单
开启缓存
package listen.example3;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching //开启缓存的注解
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
敲缓存编辑代码
实现缓存数据的增删改查
package listen.example3.serviceDemo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
/**
* Created with IntelliJ IDEA.
* Description: If you don't work hard, you will a loser.
* User: Listen-Y.
* Date: 2020-08-23
* Time: 15:36
*/
@Service
public class PersonService {
//这里我们首先自定义一个类和对想, 将这个对象保存在缓存中
static class Person{
public String name;
public int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
private static Person person = new Person("listen", 21);
//缓存的查询 并将查到的数据保存至缓存中
@Cacheable(cacheNames = "person", key = "#id")
public Object getPersonById(int id) throws InterruptedException {
//我通过id查询我的person 如果此时缓存中没有这个id对应的person就会进行查询
System.out.println("进入查询...");
//方便我们演示如果进入查询我们多等待3秒
TimeUnit.SECONDS.sleep(3);
if (id == 1) {
return person;
}
return "Id错误...";
}
//修改这个id绑定的对象的属性
@CachePut(cacheNames = "person", key = "#id")
public Object updatePerson(int id, String newName) {
System.out.println("进入修改方法...");
if (id == 1) {
person.name = newName;
return person;
}
return "Id错误...";
}
//删除缓存
@CacheEvict(cacheNames = "person", key = "#id")
public boolean delPerson(int id) {
return id == 1;
}
}
编写触发代码
package listen.example3.serviceDemo.personController;
import listen.example3.serviceDemo.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created with IntelliJ IDEA.
* Description: If you don't work hard, you will a loser.
* User: Listen-Y.
* Date: 2020-08-23
* Time: 15:54
*/
@RestController
@RequestMapping(value = "per")
public class PersonController {
//注入对象
@Autowired
private PersonService personService;
//演示获取缓存
@RequestMapping(value = "get")
public Object getPerson(int id) throws InterruptedException {
return personService.getPersonById(id);
}
//演示修改
@RequestMapping(value = "update")
public Object updatePerson(int id, String newName) {
return personService.updatePerson(id, newName);
}
//演示删除
@RequestMapping(value = "del")
public Object delPerson(int id) {
return personService.delPerson(id);
}
}
- 最后运行项目使用postman访问就会发现有缓存的好处
简述原理
- Cache本质上是用conCurrentMap的数据结构实现, 其key便是缓存的cacheName value便是我们的#id中的id, 可以有效节约数据库资源, 加速程序的查询效率
实现定时任务
定时任务:顾名思义,定时执⾏的任务我们称之为定时任务。它包含了两层含义,第⼀是定时,第⼆是 任务。也就是说预计某个时间去执⾏某件事的这个⾏为就被称之为定时任务。
为什么需要定时任务?
定时任务的作⽤:防⽌错过某项重要的任务。
开启定时任务功能
package listen.example3;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableCaching //开启缓存的注解
@EnableScheduling //开启定时任务的注解
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
编写定时任务的代码
package listen.example3.serviceDemo;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* Created with IntelliJ IDEA.
* Description: If you don't work hard, you will a loser.
* User: Listen-Y.
* Date: 2020-08-23
* Time: 16:21
*/
@Component //表示这是一个组件
public class SchedulingService {
//下面的cron表达式表示不管何时何月何年从启动开始 每三秒执行一次
//每位代表含义:秒 分钟 ⼩时 ⽇ ⽉ 周 年
@Scheduled(cron = "0/3 * * * * ?")
public void schedule() {
System.out.println("定时任务执行中...");
}
}
cron表达式
cron 表达式可以是 6 位或者是 7 位,每个位置从左到右代表的含义如下:
在线生成cron表达式
最后运行代码就会发现定时任务的存在
简述定时任务原理
SpringBoot 在初始化 bean 之后,通过拦截器(postProcessAfterInitialization)拦截到所有⽤到 @Scheduled 注解的⽅法,再将这些⽅法放⼊到定时任务列表中,然后按照顺序执⾏这些定时任务。 定时任务的执⾏细节是,先解析 cron 表达式,获取到定时任务下次执⾏的时间,然后放⼊线程中等到对 应的时间到了之后进⾏执⾏,之后会按照执⾏的频次,定时执⾏这些任务。
监控和管理工具——Actuator
它的⽤途有哪些?
Spring Boot 有⼀个⾮常实⽤的指标监控监控功能,使⽤它我们可以来监测⽣产环境的信息,例如:
查询项⽬中⽬前所有的缓存信息;
查询项⽬中的所有定时任务列表;
查询项⽬中的所有 beans;
查询项⽬中的所有 mappings;
查看项⽬运⾏信息 env;
查看⽇志信息;
停⽌ Spring Boot 项⽬等。
访问路径 描述
/auditevents 显示应⽤暴露的审计事件(⽐如认证进⼊)
/beans 显示应⽤程序中所有 Spring Bean 的完整列表
/caches 公开可⽤的缓存
/conditions 显示在配置和⾃动配置类上评估的条件以及它们匹配或不匹配的原 因 /configprops 显示所有 @ConfigurationPropertie 的整理列表
/env 获取全部环境属性
/flyway 提供⼀份 Flyway 数据库迁移信息
/health 显示应⽤程序运⾏状况信息
/httptrace 显示 HTTP 跟踪信息(默认情况下,最近 100 个 HTTP 请求-响 应交换)
/info 获取应⽤程序的定制信息,这些信息由 info 开头的属性提供
/integrationgraph 显示 Spring Integration 图,需要依赖于 spring-integration- core /loggers 显示和修改应⽤程序的配置
/liquibase 显示已应⽤的所有 Liquibase 数据库迁移
/metrics/{name} 报告指定名称的应⽤程序度量值
/mappings 显示所有 @RequestMapping 路径的列表
/scheduledtasks 显示应⽤程序中的计划任务
/sessions 允许从 Spring Session ⽀持的会话存储中检索和删除⽤户会话, 需要使⽤
Spring Session 基于 Servlet 的 Web 应⽤程序
/shutdown 使应⽤程序正常关闭,默认禁⽤
/threaddump 获取线程活动的快照
添加actuator框架
在 pom.xml 中添加如下信息:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
配置监控
在配置⽂件 application.properties 配置如下信息:
# 开启所有的监控,也可单独配置
management.endpoints.web.exposure.include=*
# 开启 beans、mappings 访问
# management.endpoints.web.exposure.include=beans,mappings
运行代码访问地址实现监控
- 这里我举例监控我们自己写的定时任务
http://localhost:8080/actuator/scheduledtasks
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200823164536758.png#pic_center
使用监控停止SpringBoot
- 配置开启HTTP停止SpringBoot
# 开启关闭功能 默认是false
management.endpoint.shutdown.enabled=true
- 使⽤ POST ⽅式访问并停⽌项⽬。
http://localhost:8080/actuator/shutdown