Java从单体到微服务打造房产销售平台

概述

最近在学习某课的《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-mail
spring.mail.host=smtp.163.com  #163邮箱
spring.mail.username=chenwuguii@163.com 
spring.mail.password=czy123456  #163邮箱授权码
spring.mail.properties.mail.smtp.auth=truehouse
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

MailService

@Service
public 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目录下去寻找图片。

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理