Java Electric Business Interview introduction

Source: Internet
Author: User
Tags memcached redis nginx reverse proxy rabbitmq redis cluster advantage

The first two projects are based on the SSH framework, and the most recent projects are based on the SSM framework. In the actual development, I think these two frameworks, their biggest difference is hibernate and mybatis difference.

Hibernate compared with MyBatis, MyBatis is lighter, more flexible and easier to master. MyBatis can separate the SQL statements from the Java code, placed in the configuration file to write, greatly reduce the coupling between Java code and SQL statements, easier to operate on SQL statements, it is important that mybatis can also write dynamic SQL statements, But MyBatis also has some drawbacks, such as MyBatis's own caching mechanism is not hibernate so perfect, hibernate in addition to its own good caching mechanism, you can also use Third-party caching. Hibernate a more complete package of JDBC, but it is more difficult to learn than MyBatis. Hibernate's DAO layer development is simpler than mybatis, and it is better to maintain and cache objects than MyBatis.

(The difference between SPRINGMVC and struts: Springmvc is a method-level interception, a method corresponding to a request context, and the method at the same time with a URL, the transfer of parameters is directly injected into the method, is unique to this method.)

STRUTS2 is class-level interception, a class corresponds to a request context, and struts can accept parameters with attributes when it accepts parameters, which means that the parameters are shared by multiple methods, and it is not possible to identify the method by annotation or other means.

The mall is a comprehensive consumer platform, mainly for female consumers, the main sales of women cosmetics, jewelry, clothing and other female supplies. Merchants entered the mall to sell their own products, and can get shopping malls to provide a variety of services.

Throughout the project, we are using Nginx+tomcat to deploy (the interviewer may ask who Nginx is deployed.) How it is deployed. Nginx implementation process, advantages), Nginx on the one hand to do load static resources of the server, on the other hand to do reverse proxy and load balance. Because the project needs to run in multiple environments, we use the Nginx reverse proxy to solve the problem of different environments with the system access address.

Because the whole project realizes the function is many, therefore uses the distributed architecture design, the entire project includes the backstage management system, the foreground system, the order system, the login system, the search system, the shopping cart system and so on, the benefit is makes each function module independent, reduced the coupling between each system, Adding or deleting a feature does not affect other functional modules.

Because the project is designed with a distributed architecture, the modules are independent of each other, and the access paths of each module are different, so when data is requested across domains, cross-domain constraints are encountered. For example, when a user first visits the home page of the site, the home page will request the background management system to load the class of the product asynchronously, this is the problem that Cross-domain is restricted, if in this module, we are asynchronously request data through Ajax, but Ajax does not support Cross-domain, Therefore, the problem of Cross-domain request data cannot be solved with Ajax. Finally, we use JSONP to solve this problem. Jsonp through the script label SRC can cross domain request characteristics, load resources, will load the resources (through a method name data package) as a JS script parsing, define a callback function (how to achieve.) ) to get the incoming data. We use JSONP because JSONP compatibility is good, and the result can be returned by callback way after the request is completed. A disadvantage of JSONP is that only get requests are supported and other types of HTTP requests such as Post are not supported.

This solves the problem that the browser accesses the current page to load the background system data, but another question arises, how can other systems get the data that invokes the backend system? We wanted to be able to send an HTTP request to access the background data, and we thought of using httpclient to solve the problem because httpclient could use Java code to simulate the browser to send an HTTP request (how the Get method passes parameters.) Defines the UriBuilder object, sets the parameters in the UriBuilder, with key and value, and is of type string, then places the UriBuilder in the URI, and then the URI to the HttpGet request. Post method if the data is transferred. Simulate form submission, encapsulate the data into the list collection, and then put the collection data into the constructed form entity, placing the form entity request in the HttpPost object. Throws an interface outward, the execution procedure is: 1, creates the HttpClient object 2, constructs the Request object post, GET request 3, if has the parameter, constructs the request parameter

3.1 Get

Using UriBuilder to construct request parameters

3.2 Post

Build the form entity and put the form entity into the POST request object.

4, execute the request, and accept the response

5, processing response results

6. Release the connection. The connection must be freed, regardless of whether the execution method succeeds.

The httpclient implementation is considered thread-safe.

Each time the connection initiates an HTTP request, the connection is reconnected (3 handshake), and the connection is closed (4 waves), which consumes a lot of time, all of which we use the connection pool. If you do not use a connection pool, each connection will open a port, the system's port resources will soon run out in large concurrency, resulting in the inability to establish a new connection.

Like the project in the home page of the big ads and commodity categories do not need to constantly modify the data, if the user every time to refresh the page to the database query, which will waste resources and increase the pressure of the database. So we want to add this data to a buffer, the user to visit, first go to the cache hit, if the hit failed, then go to the database query, and then the query to the data added to the cache. The current mainstream caching technology has redis and memcached, simply from the point of view of cache hit, memcached higher, can be redis and memcache the gap is not large, but the Redis provides a more powerful function, read and write speed is also very fast. So we use Redis to cache the data. Redis the data into memory in the form of Key-value, provides a variety of data storage types (String,set,list,hash, etc.), and provides the persistence function (2 kinds), You can also back up data to disk (the Redis Save command is used to create a backup of the current Redis database) to prevent data loss at Redis downtime. (periodically writes the updated data to the disk or writes the modification operation to the appended record file, and on this basis realizes the Master-slave (master-Slave) synchronization). We are using spring and Jedis integrated client, can use Jedis to do fragmented cluster, solve the problem of redis memory limitations.

The previously implemented logins and registrations were done within the same tomcat, and now the system architecture is that each system is maintained by a team, each of which is individually deployed to run a separate tomcat, so The user's logon information cannot be saved to the session (multiple Tomcat sessions are not shared) (session sharing). , so we need a separate system to maintain the user's login information. We do this, users go to login page login, go to the database query whether there is the user, if not prompted the user, if there is the user information to save the Redis, and generate a token saved to the cookie,

In the background management system, the use of MAVEN's multiple modular management, which uses a horizontal segmentation (vertical and horizontal division of the difference: Vertical: Functional Modules Clear, the level is not clear, code reusability is poor. Level: Clear level, high code reusability, independent maintenance. , layered development of each layer, the advantage of this is the high code reusability, clear hierarchy, easy to independent maintenance. System internal interface call using HttpClient, interface provider to use the RESTful interface definition (a software architecture style, design style rather than standard, just provide a set of design principles and constraints), the system between the notification mechanism using MQ, using RABBITMQ implementation , using the message mechanism of the RABBITMQ message subscription mode, the deployment aspect adopts the Nginx+tomcat mode, in which the function of Nginx is to do the reverse proxy, load balance, on the other hand, do the server of static resources such as picture;

In this project I am mainly responsible for the background management module, mainly to achieve commodity management and commodity specifications for the management of commodity and commodity specifications for CRUD operations. When implementing the foreground call background data, the HttpClient technology is used to implement the call between systems, and the interface that needs to be invoked is provided in the background. (HttpClient Introduction, working principle, advantages and disadvantages). If the goods in the background operation, in order to make the foreground data and background data to achieve synchronization, we use the RABBITMQ Message Queuing mechanism to achieve the product synchronization function (RABBITMQ Introduction, working principle, advantages and disadvantages);

In this project, I also participated in the development of the shopping cart module. In the development of this module, we consider the members of the two cases are not logged in and login to the goods into the shopping cart, the background of how to save the merchandise information.

When you click on the Product Details page to add to the shopping cart, we use a login interceptor to determine whether the user is logged in; the shopping cart first identifies the unique, because each account must correspond to a shopping cart, in the login state, we can directly save the data to the database, uses the user's ID to express oneself buys the goods, But if you are not logged in state, or a large number of car visits, this has drawbacks, because such high-speed reading and writing database, the database will be more pressure, here we will see how to use Redis and RABBITMQ solve this problem.

One: The login state to add merchandise to the shopping cart

At this time the shopping cart is corresponding to a user, very simple, that is, the product data into the database can be, but if read and write frequently, there are pressure problems, at this time we can use Redis as part of the reading function.

When inserting data into a database, you use RABBITMQ to send a message, and then there is a message system that listens for messages, saves the message content in the RABBITMQ (the product data that is inserted into the database) to Redis, but what storage structure do we use in Redis at this time, There are many kinds of storage structure in Redis, here we use hash structure (introduction is how to use the hash structure to save the product data), when the user inquires their own shopping cart data, do not go to the database query, but directly from the Redis the data can be taken out, So the reading pressure of the database is Redis shared out.

Two: not login to join the shopping cart, login under the merged shopping cart

In the state of not being logged in, no specified user, at this time the shopping cart should be how to allocate, data can be in what position, this is not difficult, we can temporarily save the data to Redis, not into the database, because there is no corresponding user, Redis generate a unique Outerkey, Save to a cookie, add a product each time, bring this cookie, so that each time you join the same shopping cart, this data will be saved for a period of time, when the user log in, how we will be in the state of the car and the status of the car in the state of the purchase data merge. This will require the use of the message, we can send a message to the background system, the Outerkey to the background system, the background system to the Redis in the query to the state of the shopping cart, the shopping cart data inserted into the database, and before the state of the purchase of the car data merge, Re-cache to Redis, at which time the shopping cart cached in Redis is different from the login status, because the cached shopping cart is owned by the owner, and the cached temporary shopping cart is saved to the logged-in user's shopping cart when it is not logged in.

(Set yourself two questions to ask yourself)

Database can also do read and write separation, why to use Redis as read, direct use of read and write separation is not OK.

The reading and writing of database can solve the problem, but the obvious advantage of this kind of redis database is that the efficiency of data processing is high, the efficiency of read and write separation and Redis is comparatively, the personal feeling is still redis reliable.

Redis served as a reading question when, like a double 11 such a large number of visits, Redis would not crash.

I have also thought about this question, this we can consider using Redis cluster, so we can solve most of the problems.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.