Implementation of Serverless Email Push System

Serverless Email Push System Introduction:

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


scene selection

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.

think

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
system design

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.

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
2.Redis
3.Mybatis
4.FastJson
5.Junit
6.slf4j

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> {

private final Logger logger = LoggerFactory.getLogger(EmailOneMinNotRepeatFilter.class);

private static final String SMS_ERROR_MESSAGE = "Please do not re-send within a minute" ;

private RedisUtils redisService = new RedisUtils ( );

@Override
public void doFilter(Map contextMap, FilterInvoker nextFilter) {
String emailAddress= (String) contextMap.get("emailAddress");
String repeatCheckKey=emailAddress+ SmtpServiceConstants.REPEAT_CHECK;
if(null!=redisService.get(repeatCheckKey)){
logger.warn("SmsException with error code,error message");
throw new BizException ( SMS_ERROR_MESSAGE );
}
nextFilter . invoke ( contextMap );
}
}

The above code is mainly for the verification of repeated sending. If there is no duplication, the next conditional verification is performed. If there is duplication, an exception is thrown. Return an error message. The same is true for the rest: for example, too many transmissions within a certain period of time and too many Ip transmissions can be added later. The specific use is as follows

private final FilterChain filterChain= FilterChainFactory.buildFilterChain(
EmailOneMinNotRepeatFilter.class,
EmailSendTooMuchFilter.class
);
@Override
public void sendEmailCode ( String toAddress , String code ) {
// first perform the interception operation
Map < String , Object > content = new HashMap <> ( 16 );
content.put ( " emailAddress " , toAddress ) ; _
filterChain.doFilter ( content ) ; _
............
}

Serverless Email Push System.Project Deficiencies and Prospects

In the above text and code implementation, I completed the implementation of part of the code (only completed the verification code sending scene), and some of them did not have time to code due to time reasons, so there are some regrets. There is also the question of whether it is the optimal solution for the database to store email templates. In the follow-up time, I will improve the above cases on the code cloud and try to cover the remaining unimplemented scenarios I mentioned before, and optimize the existing code. I hope everyone can help. Provide valuable comments.

Code

I uploaded the code to the code cloud https://gitee.com/zhuyongjie1212/serverless-email
The follow-up plan will gradually improve the deficiencies mentioned above and update them continuously. Also, please give valuable opinions from those who are interested, so that we can make progress with each other.

summary

In this activity of Alibaba Cloud Cloud Development Platform, we analyzed specific business scenarios. The design and implementation of the code gave me a deeper understanding and understanding of serverless cloud development , especially the use of Java function computing, which made me understand the use of Java, an old-fashioned development language, outside of traditional business scenarios. It is helpful for me to expand the technology stack .

In addition, I would like to thank the Alibaba Cloud development platform for giving me this opportunity to participate in the event, so that I have the opportunity to share my understanding and specific use of serverless cloud development with you. You can visit the official website of the cloud development platform ( http://workbench.aliyun.com/ ). There are more serverless cloud practices. The cloud development platform is an environment that can satisfy developers and R&D teams to complete their daily development work based on "cloud + browser". Its design concept is to make itself a team leader. As part of the collaboration, it will be integrated with many R&D capabilities and tools of Alibaba Cloud. With the help of a more powerful Alibaba R&D ecosystem, it will provide users with greater collaborative R&D possibilities. For example, when you use the cloud development platform, you can According to your needs, take the initiative to choose to use other services such as project management, requirements management, and document management.

At the same time, in order to help users provide an environment for seamless application of Alibaba Cloud services , the cloud development platform will be integrated with Alibaba Cloud's many cloud products , ready for users' use at any time; you can create solutions based on various scenarios on the cloud development platform You can choose different cloud services for each application. These cloud services will be activated under your Alibaba Cloud master account , and the various cloud resources you activate automatically will be metered and billed normally according to your usage.

The cloud development platform encourages all scenario solutions to provide services based on Alibaba Cloud's serverless products as much as possible. Serverless products have real-time elasticity and pay-as-you-go features, which can help commercial R&D teams realize their business value at the lowest possible cost.

Code

I uploaded the code to the code cloud https://gitee.com/zhuyongjie1212/serverless-email
The follow-up plan will gradually improve the deficiencies mentioned above and update them continuously. Also, please give valuable opinions from those who are interested, so that we can make progress with each other.

Serverless Email Push System summary

Serverless Email Push System.In this activity of Alibaba Cloud Cloud Development Platform, we analyzed specific business scenarios. The design and implementation of the code gave me a deeper understanding and understanding of serverless cloud development , especially the use of Java function computing, which made me understand the use of Java, an old-fashioned development language, outside of traditional business scenarios. It is helpful for me to expand the technology stack .

Serverless Email Push System.In addition, I would like to thank the Alibaba Cloud development platform for giving me this opportunity to participate in the event, so that I have the opportunity to share my understanding and specific use of serverless cloud development with you. You can visit the official website of the cloud development platform ( http://workbench.aliyun.com/ ). There are more serverless cloud practices. The cloud development platform is an environment that can satisfy developers and R&D teams to complete their daily development work based on "cloud + browser". Its design concept is to make itself a team leader. As part of the collaboration, it will be integrated with many R&D capabilities and tools of Alibaba Cloud. With the help of a more powerful Alibaba R&D ecosystem, it will provide users with greater collaborative R&D possibilities. For example, when you use the cloud development platform, you can According to your needs, take the initiative to choose to use other services such as project management, requirements management, and document management.

Serverless Email Push System.At the same time, in order to help users provide an environment for seamless application of Alibaba Cloud services , the cloud development platform will be integrated with Alibaba Cloud's many cloud products , ready for users' use at any time; you can create solutions based on various scenarios on the cloud development platform You can choose different cloud services for each application. These cloud services will be activated under your Alibaba Cloud master account , and the various cloud resources you activate automatically will be metered and billed normally according to your usage.

Serverless Email Push System.The cloud development platform encourages all scenario solutions to provide services based on Alibaba Cloud's serverless products as much as possible. Serverless products have real-time elasticity and pay-as-you-go features, which can help commercial R&D teams realize their business value at the lowest possible cost.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00