Suitable for beginners to Java Web stack, reduce confusion and dettour. Know how, know why….
Keep updating j-days
In short: two lines, no confusion
- Clue 1 — Along the flow of data, think about the requirements and problems existing in each link and how Java can solve them. For example, Java implements HTTP protocol and accepts client requests as a prerequisite for Java Web services
- Clue 2 — Ultimate goal: Business first: 【 free web developers from low-level, repetitive work, focus on business 】, namely the problems existing in the old technologies/solutions, as well as new technology/solution how to solve and optimize, unceasingly will developers from the cumbersome and repetitive low-level labor, thus more attention to the business – Spring and Boot into the main cause of Java fact specification
The following is an overview of the main links according to the direction of data flow
1. [Starting point (premise of service)] — Question: How to establish communication link, which “official language”, i.e. network protocol is used
- IP protocol mounting: Identifies the unique server (server + listening port) on the network based on IP+port.
- TCP protocol family (reestablishing a connection) : Establishes a stable simplex/duplex connection between a server and a client by shaking hands.
- Core objective – Communication:
- Client-server rich communication-HTTP protocol: C-S architecture focuses on the rich presentation layer (JSON, XML, even files) (hypertext), and builds a variety of WWW web services and sites;
- Server-server Efficient communication – RPC protocol: The communication between servers is the exchange of service data. It is characterized by high frequency and small data. It pays more attention to load efficiency (body/header ratio) and performance than richness, such as Dubbo and THRIFT
2, [I/O (service process (user mode) <– > system (hardware) data flow between levels)]
After the client sends data to the server’s nic buffer over the above links and protocols, pay attention to how the operating system (OS) transfers the request data received by the nic (read buffer) (kernel state) to the server’s memory space (user (process) state) (request read) and vice versa (response output).
Core: Focus on the upgrade path of OS IO mechanisms (language layer (be it Java, Python, NodeJS, Go…) It just encapsulates the system call.
-
BIO blocks IO (❌Web service basically obsolete) :
-
read/write
System call (user mode <-> kernel mode) synchronous block, each thread specified to handle a connection (1000 connections =1000 threads, thread stack (1M or so)*1000, memory directly out of OOM, and thread switching (save and restore context) cost becomes the main contradiction ⚠️)
-
InputStream/OutputStream
The Java SDK’s input/output abstraction interface, which can be seen as a wrapper for Read /write and an abstraction for IO, provides an object-oriented IO Api
-
-
NIO/2 non-blocking (mainstream ✅) :
-
selector/epoll
Multiplexing, instead of blocking read/write waits, allows single threads (Acceptor threads) to listen for I/O events on ten thousand connections and distribute them to the processing (worker) thread pool. (Corresponding to the [1 Primary-multiple-slave] Reactor’s responsive threading model, see Netty threading Model)
-
Buffer+Channel
Replace direct InputStream/OutputStream to improve performance and functionality
- Convert random reads and writes to batch sequential reads and writes using Buffer [Improves performance]
- An enhanced API for reading and writing duplexes is provided via Channel. Read and write only occur on Channel+Buffer, no direct system call (read/write), no blocking (no kernel-mode <-> user-mode state).
-
-
AIO Asynchronous IO (non-mainstream ⚠️ system support is not mature) :
Operating system – dependent async_IO mechanism
3. Service (core) : execute a series of business logic according to the request and return the processing result (response output).
Focus on two chains of responsibility
-
Servlet specification and Container (Tomcat as an example) [traditional ⚠️] :
The servlet specification is a Java for service of abstract (typical Java programming interface), formulated the standard service application to the service process, functional components, and the container specification (if there is no specification, cannot become the industry standard, not big company/Daniel to join and support, so the interface thoughts is the most important of the Java programming ideas, Emphasis on abstraction (interface/specification design), more implementation (merit wins)
-
2 major components:
- Filter – Consists of pre – and post-chain of responsibilities for performing pre – and post-service preprocessing (such as permission validation) and post-service post-processing (such as service time statistics)
- Servlet – a true service component, the core service method is used to extend, distribute and execute different business logic based on different requests
-
Container (common Tomcat) :
Servlet container specification implementation, responsible for Servlet instantiation and life cycle management, and provide connection management, protocol resolution (such as HTTP) and other basic support, so that developers only focus on extending the Servlet, to achieve their own business, do not need to repeat the bottom labor
-
Problem: Servlet#service is too low-level and primitive, the parsing of parameters, validation, routing distribution and response packaging need to be handled manually by the developer, which is onerous and redundant ⚠️
-
-
2. Spring MVC responsibility chain [mainstream ✅] :
In view of the problems of the traditional Servlet mechanism, the core DispathServlet fully proxy all requests, solving most of the low-level, repetitive labor such as parameter parsing, binding, verification and automatic routing, response encapsulation, etc. By using annotations (declarative programming) and dynamic proxy mechanisms (dynamic programming) to make any class or method a “Service”, rather than just inheriting the heavyweight class Servle, developers are truly putting business first.
-
2 major components:
- Interceptor: Similar to the Filter in servlets, it has a front/back responsibility chain
- Controller: a collection of services in which each internal method can be declared as a service (similar to Servlet#service), but is concerned only with the current request parameters and business logic
-
Principle of stratification and isolation:
That is, although the Filter/Servlet in the Servlet specification and the Interceptor/Controller in Spring MVC have similar capabilities, the business layer should not easily bypass Spring MVC to use the Servlet mechanism, which will break the principle of hierarchical isolation. Causes confusion in code logic with no added benefit.
-
4. Spring/Boot (Fact specification – Help Java Web take off)
Improve Java development efficiency and force, the de facto specification for Java Web programming
-
Spring (Instance Management Factory/Container)
Instead of the traditional problem of manually creating isolated instances, managing the lifecycle, and passing layers of dependencies (especially deep nesting), delegating the creation, management, and interdependence of component instances to the Spring factory (IoC, control/dependency inversion) reduces the repetitive work of manual management. More container-based support for AOP and other advanced programming (centralized benefits are manageable and provide advanced functionality) to further improve the level of automation (such as automatic configuration of boot, automatic proxy of transactions)
-
Reverse (also known as delegate (to Spring))
Spring automatically creates dependent instances and establishes dependencies based on the dependencies (first), whereas in normal (normal, manual) cases, the dependent instances are created first and dependencies are set manually (later).
-
Causes — two basic facts:
- 1, functional components are mostly stateless singleton mode (stateless service principle), without manual redundant creation, management ** (need automation, transparency) **;
- 2. Components need to be combined with each other to achieve more complex business functions ** (dependency, combination mode) **;
-
-
Spring Boot (Development scaffolding/launcher) :
Based on the idea of “convention over Configuration”, on the basis of conditional configuration supported by the Spring container, the convention configuration of common Web components (various starter packages, such as Spring MVC starter, Spring Data starter… So on)
- Basic fact: When using a component (such as the Tomcat container), the default or generic configuration is used in most scenarios (90%). It is not wise to configure or copy it every time, so it needs to be further liberated.
-
Summary:
Through Sping and Boot, Java development is equipped with scripting language development (scaffolding) out of the box and agile development ability, which solves most low-level labor and further liberates developers. For example, as long as the starter Web dependency is introduced, Tomcat, MVC and other things run out of the box without any additional configuration. 6 to Fly (Those of you who have done traditional Java Web development will be familiar with the repetitive, onerous configuration of Web.xml)
5. Java Concurrent programming (key differences between front and back end development)
Concurrent requests from thousands of users to the server are concurrent processing requests (serial processing is not recommended, I/O is slow, and users on 50-1000 wait run away). Concurrency security must be guaranteed, so concurrency security and programming are important propositions for Java Web development.
- Concurrency: Multithreading and thread pooling (mainstream, but not unique)
- Concurrent security
- Concurrency security means: synchronized, lock, CAS, volatile, etc.
- Concurrency safe object/field: AtomicInteger Family of atoms
- Concurrent safe collections (containers) : ConcurrentHashMap series
Business model and storage
The essence of business is business model and service design, and ultimately persistence of business to form business data. Three storage devices with different emphasis
-
MySQL:
Transactional storage is essential to most businesses, especially core businesses, focusing on relationships and transactions between models
-
Basic facts:
A business either succeeds or fails, and should not be in the false intermediate state of half success and half failure ==> transaction (business) consistency
Note: If the DB layer does not provide this mechanism, it is necessary for the business side to implement and guarantee additional (undoubtedly repetitive, redundant and low-level labor).
-
-
Redis
K-v key-value storage, focus on hot data of query bottleneck, improve service experience (optional, but more friendly)
-
ES
It focuses on keyword retrieval to compensate for the weakness of MySQL text retrieval, and speeds up text search by inverting the index (searching documents by keyword, rather than (traversing, one by one) searching documents for keywords)
. Unfinished, to be continued…
The last
- Project practice * time, continuously deepen Java and various technology stacks, and transform theory into ability