JavaNote1.0.1_Java middleware technology

Preface

What I want to write about today is the construction of distributed systems, the construction of large websites, and the basic knowledge of Java middleware.

1. Introduction to distributed systems

Definition of distributed system: A distributed system is one in which components located at networked computers communicate and coordinate their actions only by passing messages. Distributed systems have
two focuses. One is that the components are distributed on networked computers, and the second is that among the components Communicate and coordinate actions only through message passing.

1.1 Basic knowledge of distributed systems

Distributed systems were invented to solve the performance problem of a single computer system. The components of a single computer include: input devices , output devices , operators , controllers and memories . A distributed system is actually like a supercomputer composed of multiple computers cooperating with each other.

1.1.1 Execution modes of threads and processes

Our final development must be run on threads in the process. The most important things in the multi-threading process are communication between threads , concurrency control of threads , and thread coordination . One characteristic of multithreading is that concurrency control has a huge impact on program concurrency performance.

1.1.2 Several execution modes of multi-threading
  1. Multi-threading mode that does not communicate with each other: there is no intersection between them. Two threads execute in parallel without conflict.
  2. Multi-threading mode based on container sharing collaboration: There is shared data between multiple threads , such as producers and consumers. The way to control concurrency is generally locking or mutual exclusion .
  3. Through event coordination: not only data is shared, but also logically related. You need to wait for other threads to trigger events . Deadlocks are prone to occur, that is, the problem of 1 locking a waiting for b, and 2 locking b waiting for a needs to be solved. Either the thread execution order is changed, or the entire block is directly locked.

The relationship between multiple processes is much simpler, because the memory relationships between them will not conflict.

1.1.3 Basic knowledge of network communication
  1. The first method is BIO, which is blocking io. This method blocks and locks when establishing a connection and performing read and write operations.
  2. The second method is NIPO, which is nonblocking IO. It adopts an event-driven idea and does not need to allocate a thread for each Socket socket. A dispatch distribution strategy is adopted, and the content of the request is distributed to different handlers for processing.
  3. The third way is AIO, which is AsynchronousIO, which is asynchronous IO. AIO adopts the Proactor mode, which means that when performing read and write operations , the corresponding read/wirte method is called, and then the CompletionHandler (processor for action completion) is passed in. That is to say, the main program will be notified after completion.
1.1.4 The process of expanding from single machine to distributed
  1. Changes in input devices: A distributed system is composed of points connected by multiple systems. In addition to traditional human-computer interaction devices , input devices include every input device on the network that can receive input .
  2. Changes in output devices: There are also two types of output devices, traditional output devices such as screens and terminals. There is also a node that transmits information to other nodes .
  3. Changes in the controller: The controller in a stand-alone system is the CPU . In a distributed system, it is not the specific components, but the distributed control method . For distributed devices, the first is hardware for load balancing , and the second is software load system LVS . The third type is the name service . The request initiator and the request handler are directly connected, but they will be connected to a name server together, which can collect the request handler's information and then give it to the request initiator. Complete the role of address exchange. The fourth type is the rule server , which does not require the rule server to connect to the request handler. The last one is the Master+Worker method. In this method, a master management node is assigned to different requesters for processing.
  4. Changes in the operator: The operator is similar to the controller. It first has a rule server DNS, and then publishes it to the website server through load balancing .
  5. Memory changes: Same rules for distributed storage.
1.1.5 Difficulties of distributed systems
  1. Lack of global clock: In a distributed system each node has its own clock.
  2. Independence in the face of failures: If some nodes have problems, not all systems will fail, and some of them will be good.
  3. Handling single points of failure: A certain function of a certain role is only supported at a single point.
  4. How to satisfy transaction (ACID) characteristics: atomicity, consistency, independence, Durability
2. Large websites and architecture evolution process

Definition of large websites: Large websites are a common distributed system. I think large websites must have a high amount of data and visits . Both are indispensable.

2.1 Website built using Java technology and stand-alone machine

A basic stand-alone website usually includes an open source server, and then uses a database relational system to manage our data.
Let's start with a trading website:
An ordinary Taobao website includes transaction and product users. Each server contains a table: user table, product table, transaction table. The server and database are related through JDBC .
We were very lucky:
our external visits continued to increase, so we first added servers. The database is placed on another server and essentially changes little.
The transaction volume is getting bigger and bigger:
the pressure on the application server is getting bigger and bigger, so we must increase the number of application servers, and which application server the user accesses depends on the load balancing strategy.

Session issues after application server clustering

Building Java middleware

Definition of middleware: Middleware is a kind of software glue that enables communication between different applications. It is neither part of the operating system, nor the database management system, nor the software application.

Java concurrent programming classes, interfaces and methods

Thread pool : The thread pool does not need to create threads all the time, but reuses threads. If the thread pool is not used, a thread must be created every time. If the execution work within the thread is particularly simple, the overhead of creating the thread will account for a large proportion of the entire time.
synchronized: It is a method of declaring mutual exclusion. It can declare the entire class, static function, or code block parameter is (class name.class). Mutual exclusion can also be declared for objects, and the non-static function or code block parameter is ( this ).

ReentrantLock : ReentrantLock is a class in java.util.concurrent.locks, but the difference is that unlock must be declared in finally. You can also specify whether to use fair locking.

volatile : can only provide visibility of updated data, but when multiple data are read, the threads will execute together, so it is not thread-safe.

Atomics : As a program counter scenario, there is no need to lock the code block, the class itself is atomic, and it is particularly fast due to the internal implementation of the hardware-supported CAS instruction .

Wait, Notify and NotifyAll:
These three categories are the three methods of Java's Object object. wait is used for process waiting, while Notify and NotifyAll are used for process notification. The difference is that one will wake up a single process, while the other will wake up all waiting threads.
CountDownLatch:
The function is to trigger an event after multiple threads have completed their work. Other threads can wait for this event to trigger their own subsequent work.
Here is an example
of using CountDownLatch to sort multiple cores. The link is here
: CyclicBarrier:
Cyclic Barrier, which can coordinate multiple threads and let multiple threads wait in front of the barrier. Until all threads reach this barrier, they will perform subsequent actions together. Modify the above procedure
for multi-core sorting . If there are too few threads in the thread pool, it will cause some programs that are not assigned threads to wait, but the waiting programs have to wait for programs that are not assigned threads to enter await, so a deadlock will occur . CountDownLatch cannot be used cyclically, but CyclicBarrier can be used cyclically. Semaphore: This function is used to manage semaphores . The main function is to control the number of concurrency . If the number of concurrency is exceeded, wait for other methods to return before executing. Exchanger: The name means exchange




. Exchanger is used to exchange data between two threads. The thread will block on exchanger until another thread also enters the exchange method in Exchanger. An example of an exchange queue
Future and Future Task:
Future is an interface and FutureTask is a concrete implementation class. In other words, when we need to call a remote data, we do not need to wait for the result to be returned, but wait for the result to be returned when we need to use it.
Concurrent container :
A concurrent container is a thread-safe container. Common containers include CopyOnWrite and Concurrent .

dynamic proxy

Dynamic proxy is a very important foundation for middleware implementation. Everyone is familiar with the proxy mode in programming. The proxy class and the delegation class have the same interface, and the proxy class also has many practical applications. The specific implementation is divided into static proxy and dynamic proxy .

The method of static proxy is to construct a corresponding proxy class for each proxied object. This will cause a lot of duplicated code, so we need to use dynamic proxies.
Dynamic proxy uses a proxy class implementation object that dynamically generates a specific delegate class. a concrete example

reflective technology

Reflection technology is also an important technical basis for middleware. Reflection is a very convenient and powerful function provided in Java. The reflection mechanism means that in the running state, all properties and methods of any class can be known. Any method and property of any object can be called.
Java reflection mainly provides the following functions :

  1. Determine the class to which any object belongs at runtime
  2. Construct an object of a class arbitrarily at runtime
  3. Determine the member variables and methods of any class at runtime, and call the method of any object at runtime.
  4. Call the method of any object at runtime to generate a dynamic proxy

Java's reflection mechanism brings dynamics to Java itself , so that Java does not know the information of the class object during operation but can call the corresponding method and modify the attribute value. Reflection is of great help in implementing remote calls .
Reflection usage collection:

  1. Get which class the object belongs to
  2. Get class information
  3. Build objects
  4. Dynamic execution method
  5. Dynamic operation properties

The specific implementation is here

Network communication implementation options

It can be implemented using MINA or Netty.

Guess you like

Origin blog.csdn.net/h201601060805/article/details/130736076