Implementation of Serverless Email Push System
Serverless Email Push System.Java is the top programming language used by developers over the years. It is well known by many developers because of its rich ecosystem. Nowadays, with the continuous promotion of Serverless, Java function computing is gradually moving towards the vision of developers. Recently, I am responsible for the preparation of system service support functions. After stripping out the mail service. Taking into account the characteristics of its supporting system and the special relationship with the business system. I just wanted to use Alibaba Cloud Function Compute to strip it from the system, thereby reducing the system load. The email push system mainly includes three scenarios for pushing verification code result information. This paper mainly analyzes and thinks about the business migration to function computing.
Serverless Email Push System.My scene choices and reflections
A WEB website will send a welcome email after the user successfully registers, and the content of the email is customized into a template through function computing. Every time it is triggered, every execution is idempotent and stateless. Because I have done the corresponding business, I am familiar with Ali's corresponding documents, and there are some implementation codes. Therefore, this business scenario was chosen.
is the first time to choose function computing to implement the corresponding business scenario, and individuals are more cautious in the selection of scenarios. Since I have done the SpringCloud project's integration of Alibaba Cloud's mail service, and completed the code logic implementation of post-registration email push, I was thinking about the feasibility of moving traditional code implementation to the cloud and using function computing.
Serverless Email Push System.In the original SpringCloud project implementation, I mainly used services such as Redis MySQL Alibaba Cloud SDK integration. The first thing to consider is how well the Alibaba Cloud Function Compute Java
Serverless Email Push System.SDK supports Alibaba Cloud's own SDK and other third-party SDKs. After I first asked Ningzhong about the support of Alibaba Cloud Function Compute Java SDK for MySQL (feasible), I was determined to transfer the original code design to the cloud.
System Design and Code Implementation
First of all, the email push system I designed mainly includes the following functions:
1.Complete the push of registration results
2.Complete the sending of the email verification code
3.Complete regular email pushes for subscribers
Serverless Email Push System.For a general email system, the above three items should cover most of the business scenarios, so I mainly discuss the above three points. Since the system uses function computing, and the business logic is not overly complicated for the mail system, we try to implement code as lightweight as possible to reduce the introduction of heavy-consuming services.
Serverless Email Push System.When I was making technology choices (when I was doing SpringCloud 's mail service), I referred to many mail services. Why did I choose Alibaba Cloud? In fact, the interface documentation is relatively sufficient and it has visualization and statistical services. Considering the stability of the system, Alibaba Cloud's email push service is adopted. Then, in order to consider scalability, I used the form of uploading HTML templates and then modifying reserved keywords with code to customize the code. In fact, the main point is that I will explain these two specific analyses in Service Dependency.
The system is a Maven project, so it is more convenient to manage dependencies. I mainly use several components
1.Alibaba Cloud Email Service SDK
Serverless Email Push System.I have explained the reasons for using Alibaba Cloud Mail SDK above. Now I will mainly talk about the use of Redis MySQL. Let’s talk about MySQL first. Some people will ask if it’s just to send an email, just write a few templates and throw them into the project code. There is nothing wrong with that, and it can be done. But there is a problem, if the template format changes, then the entire code also needs to be changed. In this case, it is quite unfavorable for scalability, and there are many configurations in the SDK, and each email will have its own different parameter configurations. If so, each one declares in the code that the problem is the same as above. For Modification and maintenance are quite disadvantageous. So I extracted it and made two classes 1, SmtpConfig (storage configuration) 2, SmtpTemplate (storage template). The code implementation of these two classes can be found in the code cloud link I posted later . In the design of these two classes, I adopted the congestion mode, and put some object encapsulation business and the corresponding core data processing business into the entity class. If you are interested, you can check it out in the code, and hope to make valuable comments.
Serverless Email Push System.Some people will say that you use MySQL for the maintenance of the code, so what about Redis? This is also incompatible with the lightweighting you mentioned earlier. To answer this question, we have to go back to the business. For the scenarios of email push and verification code sending, is the frequent sending of emails reasonable? Obviously unreasonable, continuous random sending and may cause your sending address to be marked as spam by the mail recipient. There is also the verification code sending scenario. In fact, it is not only the verification code that is sent out and it is completed, but also includes the verification logic, so the reason for using Redis is actually understood by everyone.
In fact, there is another reason: I can see from the above that my template is placed in MySQL in the form of an OSS link, so every time I call it, I need to request the OSS link to get the corresponding template and then parse it. Then replace the corresponding preset field for sending. From daily use, everyone knows that the speed of string replacement is actually very fast. However, the parsing of the file needs to be determined by the speed of the network, and it is actually very troublesome to read the file into a String string. So after I read the template for the first time, I put the previously read substring information into Redis, and when I call the same template for the second time, I directly fetch the data from Redis, if the template still does not exist after three days If it is called again, it means that the template is not very common and is relatively biased, so it is automatically deleted from Redis (the preset time is 3 days). In this way, the execution loss can be reduced from 300ms to about 100ms.
Serverless Email Push System. Decoupled thinking
Serverless Email Push System.Before the code is sent, some status checks and some conditional interception operations need to be done. If you only use Ifelse for simple code implementation, the code readability and code scalability will be reduced. I have read the book of Ali boss Zhang Jianfei (Big Brother Dafei) before, and have a certain understanding of the Cola framework written by him, so I used the chain of responsibility model and some implementations of exception handling.
First Look at Chain of Responsibility Design
* @author mac_zyj
* Rules for not sending SMS repeatedly within one minute
public class EmailOneMinNotRepeatFilter implements Filter
Knowledge Base Team
Knowledge Base Team
Knowledge Base Team
Knowledge Base Team
Explore More Special Offers
50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00