概述
最近在学习某课的《Java从单体到微服务打造房产销售平台》
,也算是我学习的第一门微服务课程,在此开贴记录一下知识点,如有不当请多指教!
Spring Mail发送激活链接
功能实现:在注册用户时通过spring mail
发送激活链接到用户的邮箱,在有效期内用户点击链接后更新用户状态为激活状态。
引入spring-mail
依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
配置appliacation.properties
文件
#用来发送邮件domain.name=127.0.0.1:8090#spring-mailspring.mail.host=smtp.163.com #163邮箱spring.mail.username=chenwuguii@163.com spring.mail.password=czy123456 #163邮箱授权码spring.mail.properties.mail.smtp.auth=truehousespring.mail.properties.mail.smtp.starttls.enable=truespring.mail.properties.mail.smtp.starttls.required=true
MailService
类
@Servicepublic class MailService { @Autowired private JavaMailSender mailSender; @Value("${spring.mail.username}") private String from; @Value("${domain.name}") private String domainName; @Autowired private UserMapper userMapper; //缓存key-email键值对,当超过15分钟有效期后,若用户还未激活则从数据库删除用户信息 private final Cache<String, String> registerCache = CacheBuilder.newBuilder().maximumSize(100).expireAfterAccess(15, TimeUnit.MINUTES) .removalListener(new RemovalListener<String, String>() { @Override public void onRemoval(RemovalNotification<String, String> notification) { String email = notification.getValue(); User user = new User(); user.setEmail(email); List<User> targetUser = userMapper.selectUsersByQuery(user); if (!targetUser.isEmpty() && Objects.equal(targetUser.get(0).getEnable(), 0)) { userMapper.delete(email);// 代码优化: 在删除前首先判断用户是否已经被激活,对于未激活的用户进行移除操作 } } }).build(); /** * 发送邮件 */ @Async public void sendMail(String title, String url, String email) { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(from);//发送方邮箱 message.setSubject(title);//标题 message.setTo(email);//接收方邮箱 message.setText(url);//内容 mailSender.send(message); } /** * 1.缓存key-email的关系 * 2.借助spring mail 发送邮件 * 3.借助异步框架进行异步操作 * * @param email */ @Async public void registerNotify(String email) { String randomKey = RandomStringUtils.randomAlphabetic(10); registerCache.put(randomKey, email); String url = "http://" + domainName + "/accounts/verify?key=" + randomKey; //发送邮件 sendMail("房产平台激活邮件", url, email); }}
Nginx代理
前提:当用户上传图片时,我们在数据库存放的是相对地址,然后保存图片到本地,在浏览器需要展示图片时我们取出相对路径后拼接上前缀路径,这里我们使用nginx代理我们图片的存放位置
配置application.properties
文件
#本地存放的文件路径,对应nginx.conf里alias对应目录(D:\user\images)file.path=/user/images/#静态资源地址前缀(若本地安装了nginx服务器,开启如下配置)file.prefix=http://127.0.0.1:8081/images
配置nginx.conf
文件
server { listen 8081;//监听8081端口 server_name localhost; charset utf-8; //代理 location /images { alias /user/images/; expires 1d; }
这样配置的话,当nginx监听到http://localhost:8081/images
的路径时,会代理到http://lcoalhost:8081/user/images
下,也就是D:\user\images目录下去寻找图片。