1、异步任务
解决的问题:一般情况下,使用一条线程的话后端到前端页面响应,需要一定的时间,异步任务可以很好地解决掉这个问题,前端快速响应,后端的话同时开启新的线程去执行
启动类:
package com.hema.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication
@EnableAsync//开启异步注解功能
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
controller:
package com.hema.test.controller;
import com.hema.test.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AsyncController {
@Autowired
AsyncService asyncService;
@RequestMapping("/hello")
public String hello() {
asyncService.hello();
return "ok";
}
}
2、邮件任务
- 导入架包:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
- 配置文件
spring.mail.username=1745103615@qq.com
spring.mail.password=
spring.mail.host=
#开启加密验证‘
spring.mail.properties.mail.smtp.ssl.enable=true
-
测试:
-
package com.hema.test; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.internet.MimeMessage; import java.io.File; @SpringBootTest class TestApplicationTests { @Autowired JavaMailSenderImpl mailSender; @Test void contextLoads() { SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); simpleMailMessage.setSubject("小花,您好"); simpleMailMessage.setText("谢谢您"); simpleMailMessage.setTo("1745103615@qq.com"); simpleMailMessage.setFrom("1745103615@qq.com"); // 发送消息 mailSender.send(simpleMailMessage); } @Test void contextLoads2() throws MessagingException { // 一个复杂的邮件 MimeMessage mimeMessage = mailSender.createMimeMessage(); // 组装 MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8"); helper.setSubject("你好,小花"); helper.setText("<p></p>",true); // 附件 helper.addAttachment("1.jpg",new File("1.txt")); helper.setTo("1745103615@qq.com"); helper.setFrom("1745103615@qq.com"); // 发送消息 mailSender.send(mimeMessage); } }
3、定时任务
TeskScheduler 任务调度者
TaskExecutor 任务执行者
@EnableScheduling 开启定时功能的注解
@Scheduled 什么时候去执行
启动类上加上开启定时任务的注解
package com.hema.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableAsync//开启异步注解功能
@EnableScheduling//开启定时任务的注解
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
测试一个定时任务:
package com.hema.test.service;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Service
public class ScheduledService {
//在特定的时间执行这段代码,
// cron表达式
//秒 分 时 日 月 周几~
@Scheduled(cron = "0 * * * * 0-7")
public void hello() {
System.out.println("你被执行了!");
}
}
4、分布式
分布式系统是若干独立计算机系统的集合,这些对于用户来说,就像是一个相关系统,他是有一组通过网络进行通信,为了完成共同的任务而协调工作的计算机1节点组成的系统,分布式的出现是为了用廉价的、普通的机器完成单个的计算机无法完成的计算和存储任务,其目的是为了利用更多的机器,处理更多的数据。
分布式系统是建立在网络上的软件系统
只有单个节点的处理能力无法满足日益增长的计算,存储任务的时候,并且在随着硬件的提升,应用程序也无法进行进一步的优化的时候,我们才需要考虑使用分布式系统,因为分布式系统要解决的问题本身就是和单机系统一样的
单机时刻:
RPC:
RPC是指远程方法调用过程,是一种进程之间的通信方式,他是一种技术思想,而不是规范,它允许程序调用共享网络上的另一台机器上的过程或者函数,也就是说两台服务器A,B,一个应用部署在A服务器上,想要通过B服务器上的应用提供的函数,由于不在一个内存空间,所以不能直接进行调用,需要通过网络来表达调用的语义和传达调用的数据,RPC就是要像调用本地函数一样的去调用远程的函数,RPC两个核心模块:通讯,序列化
5、Dubbo
dubbo是一个高可用的,高性能的,轻量级的RPC框架,他提供了三大核心功能,面向接口的远程方法调用,智能容错和负载均衡,以及服务自动的注册和发现
- 服务启动,将服务注册到注册中心(相当于中介)
- 服务消费者在启动的时候,向注册中心订阅自己所需的服务
- 注册中心向服务消费者去推送服务提供者的地址列表,如果有变更的话,注册中心将基于长连接推送变更的数据给消费者
- 监控中心,服务消费者和提供者,在内存中累计调用的次数和调用的时间,定时每分钟发送一次统计数据到监控中心
6、注册中心 zookeeper
dubbo-admin是为了让我们可视化的进行监控注册中心
Zookeeper:注册中心
Doubbo: RPC的jar包
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
1.提供者提供服务
-
导入依赖
-
配置注册中心地址,以及服务发现名,和要扫描的包
-
想要在被注册的服务上面增加一个注解@Service
-
server.port=8001 #注册中心地址: dubbo.registry.address=zookeeper://127.0.0.1:2181 #服务应用的名字 dubbo.application.name=provider-server #那些服务被注册 dubbo.scan.base-packages=com.hemma.providerserver.service
2.消费者进行消费
- 导入依赖
- 配置注册中心,以及服务发现名
- 从远程注入服务@Reference