Java from monomer to micro-service to build real estate sales platform

Posted by TMX on Sat, 05 Oct 2019 20:35:43 +0200

Summary

Recently in learning a course "Java from monomer to micro-service to build a real estate sales platform", is also the first micro-service course I learned. Here I open a post to record the knowledge points, if there is improper, please give more advice!

Spring Mail Sends Activated Links

Function realization: Send activation link to user's mailbox through spring mail when registering user, update user's status to activation state after clicking on link during validity period.

Introducing spring-mail dependencies

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-mail</artifactId>
 </dependency>

Configure the appliacation.properties file

#Used to send mail
domain.name=127.0.0.1:8090
#spring-mail
spring.mail.host=smtp.163.com  #163 mailbox
spring.mail.username=chenwuguii@163.com 
spring.mail.password=czy123456  #163 Mailbox Authorization Code
spring.mail.properties.mail.smtp.auth=truehouse
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

MailService class

@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;

    //Cache key-email key-value pairs and delete user information from the database if the user is not activated after more than 15 minutes of validity
    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);// Code optimization: Before deleting, first determine whether the user has been activated, and then remove the inactive user.
                            }
                        }
                    }).build();


    /**
     * Send mail
     */
    @Async
    public void sendMail(String title, String url, String email) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);//Sender mailbox
        message.setSubject(title);//Title
        message.setTo(email);//Receiver mailbox
        message.setText(url);//content
        mailSender.send(message);
    }

    /**
     * 1.Cache key-email relationships
     * 2.Send mail with spring mail
     * 3.Asynchronous operation with asynchronous framework
     *
     * @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;
        //Send mail
        sendMail("Real Estate Platform Activation Mail", url, email);
    }
}

Nginx agent

Prerequisite: When users upload pictures, we store the relative address in the database, and then save the pictures to the local area. When browsers need to display pictures, we take out the relative path and stitch the prefix path. Here we use nginx to proxy the storage location of our pictures.

Configure the application.properties file

#Locally stored file path, corresponding to the directory of nginx.conf alias (D: user images)
file.path=/user/images/
#Static resource address prefix (if the nginx server is installed locally, turn on the following configuration)
file.prefix=http://127.0.0.1:8081/images

Configure nginx.conf file

 server {
        listen       8081;//Listen on port 8081
        server_name  localhost;
        charset utf-8;
        //agent   
        location /images {
            alias /user/images/;
            expires 1d;
        }

With this configuration, when nginx monitors the path of http://localhost:8081/images, it will be proxyed to http://lcoalhost:8081/user/images, that is, the directory D:userimages.

Topics: Java Spring Nginx Database