<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-cacheartifactId>
dependency>
@SpringBootApplication
// 开启缓存
@EnableCaching
public class CacheApplication {
public static void main(String[] args) {
System.out.println(Arrays.toString(args));
SpringApplication.run(CacheApplication.class, args);
}
}
value相当于一个命名空间,key必须是唯一的变量
) @Override
// 使用缓存
@Cacheable(value = "cacheSpace",key = "#id")
public Book getById(Integer id) {
return bookDao.selectById(id);
}
@Component
public class CodeUtils {
private String[] bu = {"00000","0000","000","00","0",""};
public String generator(String tele){
int hash = tele.hashCode();
int encryption = 20206666;
long result = hash^encryption;
long nowTime = System.currentTimeMillis();
result = result^nowTime;
long code = result % 1000000;
code = code<0 ? -code : code;
String codeStr = code + "";
int length = codeStr.length();
return bu[length-1]+codeStr;
}
@Cacheable(value = "smsCode",key = "#tele")
public String get(String tele){
return null;
}
public static void main(String[] args) {
CodeUtils codeUtils = new CodeUtils();
String generator = codeUtils.generator("18771496933");
System.out.println(generator);
}
}
@Data
public class SMSCode {
private String tele;
private String code;
}
@RestController
@RequestMapping("/sms")
public class SMSCodeController {
@Autowired
private SMSCodeService service;
@GetMapping
public String getCode(String tele){
String s = service.sendCodeToSMS(tele);
return s;
}
@PostMapping
public boolean checkCode(SMSCode smsCode){
Boolean code = service.checkCode(smsCode);
return code;
}
}
public interface SMSCodeService {
public String sendCodeToSMS(String tele);
public Boolean checkCode(SMSCode smsCode);
}
@Service
public class SMSServiceImpl implements SMSCodeService {
@Autowired
private CodeUtils codeUtils;
@Override
//@Cacheable(value = "smsCode",key = "#tele") 读和写
@CachePut(value = "smsCode",key = "#tele") // 只写不读
public String sendCodeToSMS(String tele) {
String code = codeUtils.generator(tele);
return code;
}
@Override
public Boolean checkCode(SMSCode smsCode) {
String code = smsCode.getCode();
String cacheCode = codeUtils.get(smsCode.getTele());
return cacheCode.equals(code);
}
}
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC
username: root
password: 123456
cache:
type: redis
redis:
use-key-prefix: true
cache-null-values: false
time-to-live: 10s
key-prefix: ssm_
<dependency>
<groupId>net.sf.ehcachegroupId>
<artifactId>ehcacheartifactId>
dependency>
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC
username: root
password: 123456
cache:
type: ehcache
ehcache:
config: ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
<diskStore path="D:\ehcache" />
<defaultCache
eternal="false"
diskPersistent="false"
maxElementsInMemory="1000"
overflowToDisk="false"
timeToIdleSeconds="60"
timeToLiveSeconds="60"
memoryStoreEvictionPolicy="LRU" />
<cache
name="smsCode"
eternal="false"
diskPersistent="false"
maxElementsInMemory="1000"
overflowToDisk="false"
timeToIdleSeconds="10"
timeToLiveSeconds="10"
memoryStoreEvictionPolicy="LRU" />
ehcache>
多级缓存
,缓存统计
,自动刷新
,异步调用
,数据报表
等功能。 <dependency>
<groupId>com.alicp.jetcachegroupId>
<artifactId>jetcache-starter-redisartifactId>
<version>2.6.2version>
dependency>
jetcache:
remote:
default:
type: redis
host: localhost
port: 6379
poolConfig:
maxTotal: 50
@SpringBootApplication
// 启用主开关
@EnableCreateCacheAnnotation
public class JetCacheApplication {
public static void main(String[] args) {
SpringApplication.run(JetCacheApplication.class, args);
}
}
@Service
public class SMSServiceImpl implements SMSCodeService {
@Autowired
private CodeUtils codeUtils;
@CreateCache(name = "jetCache",expire = 3600,timeUnit = TimeUnit.SECONDS)
private Cache<String ,String > jetCache;
@Override
public String sendCodeToSMS(String tele) {
String code = codeUtils.generator(tele);
jetCache.put(tele,code);
return code;
}
@Override
public Boolean checkCode(SMSCode smsCode) {
String code = jetCache.get(smsCode.getTele());
return smsCode.getCode().equals(code);
}
}
jetcache:
remote:
default:
type: redis
host: localhost
port: 6379
poolConfig:
maxTotal: 50
local:
default:
type: linkedhashmap
keyConvertor: fastjson
cacheType = CacheType.REMOTE
@CreateCache(name = "jetCache",expire = 3600,timeUnit = TimeUnit.SECONDS,cacheType = CacheType.REMOTE)
jetcache:
remote:
default:
type: redis
host: localhost
port: 6379
keyConvertor: fastjson
valueEncode: java
valueDecode: java
poolConfig:
maxTotal: 50
Serializable
@Data
public class Book implements Serializable {
private Integer id;
private String type;
private String name;
private String description;
}
@Override
@Cached(name = "book",key = "#id",expire = 3600)
public Book getById(Integer id) {
return bookDao.selectById(id);
}
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-quartzartifactId>
dependency>
public class MyQuartz extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("Quartz run ...");
}
}
@Configuration
public class QuartzConfig {
@Bean
public JobDetail printJodDetail(){
return JobBuilder.newJob(MyQuartz.class).storeDurably().build();
}
@Bean
public Trigger printJobTrigger(){
ScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");
return TriggerBuilder.newTrigger().forJob(printJodDetail()).withSchedule(scheduleBuilder).build();
}
}
@EnableScheduling
@SpringBootApplication
// 启用主开关
@EnableCreateCacheAnnotation
@EnableScheduling
@EnableMethodCache(basePackages = "com.ssc.springboot_17_jetcache")
public class JetCacheApplication {
public static void main(String[] args) {
SpringApplication.run(JetCacheApplication.class, args);
}
}
@Scheduled(cron = "0/1 * * * * ?")
@Component
public class MyBean {
@Scheduled(cron = "0/1 * * * * ?")
public void print(){
System.out.println("哈哈哈哈。。。");
}
}
spring:
mail:
host: smtp.qq.com
username: 2683885184@qq.com
password: wpumbmnpejxddigi # 授权码
default-encoding: utf-8 #这个是字符集,咱们用utf-8就可以了
public interface SendMailService {
void sendMail();
}
@Service
public class SendMailServiceImpl implements SendMailService {
@Autowired
private JavaMailSender javaMailSender;
// 发送人
private String from = "2683885184@qq.com";
// 接收人
private String to = "1667568637@qq.com";
// 标题
private String subject = "测试邮件";
// 正文
private String context = "收到这封邮件不要慌张,这是学习过程中的测试邮件,可以忽略。。。爱你mua~!";
@Override
public void sendMail() {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(context);
javaMailSender.send(message);
}
}
@SpringBootTest
class MailApplicationTests {
@Autowired
private SendMailService service;
@Test
void contextLoads() {
service.sendMail();
}
}
<dependency>
<groupId>de.codecentricgroupId>
<artifactId>spring-boot-admin-starter-adminartifactId>
<version>2.3.1version>
dependency>
port: 8080
@SpringBootApplication
@EnableAdminServer
public class Springboot19AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot19AdminServerApplication.class, args);
}
}
<dependency>
<groupId>de.codecentricgroupId>
<artifactId>spring-boot-admin-starter-clientartifactId>
<version>2.3.1version>
dependency>
spring:
boot:
admin:
client:
url: http://localhost:8080
server:
port: 80
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: "*"
management:
endpoint:
health:
show-details: always
info: # 单独关闭info信息
enabled: false
endpoints:
# 开启web访问功能
web:
exposure:
include: "*"
enabled-by-default: true
/actuator
/actuator/端点名称
info:
verson: project.version
artifactId: project.artifactId
author: ssc
@Component
public class InfoConfig implements InfoContributor {
@Override
public void contribute(Info.Builder builder) {
builder.withDetail("runtime",System.currentTimeMillis());
Map infoMap = new HashMap();
infoMap.put("buildTime","2006");
builder.withDetails(infoMap);
}
}
最好加入真实状态,而不是纯文本
)@Component
public class HealthConfig extends AbstractHealthIndicator {
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
builder.withDetail("runtime",System.currentTimeMillis());
Map infoMap = new HashMap();
infoMap.put("buildTime","2006");
builder.withDetails(infoMap);
builder.up();
}
}
@Component
@Endpoint(id = "pay",enableByDefault = true)
public class PayEndpoint {
@ReadOperation
public void getPay(){
System.out.println("============");
System.out.println("============");
System.out.println("============");
System.out.println("============");
}
}