Knowledge points of building a website based on SpringBoot (2)

1. Interceptor: Define an interceptor that implements the HandlerInterceptor interface. The interceptor is to make some judgments on the various steps of a complete HTTP request. To implement the HandlerInterceptor interface, you need to implement the preHandler, postHandler, and afterCompletion methods.

Some things that preHandler needs to do before the request starts, judgments, etc. (For example, through request.getCookies(), read the ticket in the cookie, determine whether the ticket is not empty, not expired, etc. so that the user is valid, and determine the current thread user. Each thread accesses a different user, and it needs to be for each thread Save a user [using thread local variable ThreadLocal to save])

postHandler: before rendering, pass the user to the template (front-end html page);

afterCompletion: Clean up the current user after completion.

>>After the interceptor is written, it must be registered in MVC. The registration class WebConfiguration needs to inherit the WebMvcConfigurerAdapter class and override the addInterceptors() method;

>>You need to add the annotation @Component when defining the interceptor and defining the registration class of the interceptor

2. AOP: Aspect-oriented programming. All business needs to be handled. Separate the functions throughout the application to form reusable components. Such as log, transaction management, etc.

 Through the annotation @Aspect of the aspect, cut into all the business of the point of contact.

@Before("execution(* com.example.third.controller.*Controller.*(..))")//前置通知,()内表示切点。然后定义在执行所有的ocntroller方法之前要做的事情public void beforeMethod(JoinPoint joinPoint){...}
@After("execution(* com.example.third.controller.IndexController.*(..))")//后置通知,然后定义在执行所有的ocntroller方法之后要做的事情 public void afterMethod(JoinPoint joinPoint){...}

Aspect = tangent point + notification; where the tangent point indicates, and the notification indicates the timing and method content. 

Weaving: The process of applying aspects to target objects to create new proxy objects. Spring's aspect is implemented by a proxy class that wraps the target object. The proxy class handles method calls, executes additional aspect logic, and calls target methods. 

3.Based on Redis:

(1) Redis is a kv (key-value) database, which reads or sets data through various commands.

(2) Redis supports get, set, numeric operations, list operations (used for recent visits, fan lists, message queues), hash (variable fields, set some variable attributes of some objects), set (like User group, mutual friends), sort set (SortSet, set score for each attribute. Used for ranking)

(3) Redis connection method:

a. A simple connection Jedis jedis=new Jedis();

b. Connection pool JedisPool pool=new JedisPool(); Jedis jedis=pool.getResource(); After using up, close jedis.close(); 

(4) For highly concurrent web page visits, the relevant values ​​are first stored in the redis memory, and then synchronized to the database after a period of time.

    (If you operate directly on the database, for web pages that are accessed by tens of millions of users at the same time, the database will be continuously updated. The update process has a lock, and the process will be very slow); application redis has web pages accessed by tens of millions of users at the same time When the time, the number of visits will not always change, but the value will increase greatly after refreshing.

(5) Redis is used for caching. A cache (redis) is added to the upper layer of the database MySQL. After the user is updated, the cache becomes invalid. Each time the data is read, it is first read from the cache, and the cache is no longer searched from the database, reducing the pressure on the database.

4. Asynchronous framework: To execute all data, not immediately, but through a queue, using EventProducer and EventConsumer to process.

When a user is logging in, he wants to do something asynchronously after logging in, sending an event through EventProducer, and then there will be a return. EventConsumer will be initialized and data will be continuously checked. When EventProducer sends an event, EventConsumer will receive the event and process the event according to all registered handlers.

Asynchronization: Separate the complex business processing flow, timely feedback the data that needs timely feedback, and gradually update the lagging data from the background through the event processing function.

Asynchronous advantage: Assuming that the latter part of the process is abnormal, it has no impact on the main business, but some operations are delayed. After the server is restarted, the corresponding operations can be continued as long as the queue is still there.

5. Cloud Storage (Qiniu Cloud) SDK: You can directly upload the picture MultipartFile to Qiniu Cloud and return the url generated by Qiniu Cloud

6. Login, the core of registration is HostHolder; the current user hostHolder is saved in the interceptor, and this user is obtained from the cookie of each visit. The cookie stores user data when the user logs in through the loginTicket, that is, each user will distribute a unique ticket; identify a user through the ticket.

7. Unit testing: (JUnit) initialize data; execute the business to be tested; verify the test data; clean up the data.

Guess you like

Origin blog.csdn.net/lvliang2017232003/article/details/96020102