At present, the Spring Boot+Spring Cloud architecture has become one of the essential skills for Java programmers. When I first started learning, I saw a dazzling array of Spring family buckets, and I might feel unable to start. If you only understand the knowledge points in microservices, and ignore the order of learning the system's architecture in the way of microservices distributed architecture, beginners may not know how to use microservices to build a distributed system. In order to enable readers to quickly grasp the basic knowledge and architecture methods of Spring Boot+SpringCloud, the order of chapters in this article is the order of architecture of the application system.
Distributed can be understood as human organs, and the human body can be regarded as a distributed system. The brain is a cluster of registration centers. The limbs and organs are microservices that provide services. The distance forward is the return value after the microservice runs, and the physical energy consumed is microservices. The logic that is processed in the service, and the memory that affects it is the addition, deletion, modification, and check of the database by some microservices. Distributed can be regarded as an idea, and Spring Cloud and Spring Boot are tools that realize this idea.
No matter how complex the distributed application, how many servers are integrated, how many service interfaces are called, how many protocols are used, what kind of architecture is clustering or high-availability, what kind of load balancing is used on the client or the server, and which message is used Middleware, which database cluster, how many storage media such as search engines/non-relational databases/time-series databases/relational databases/file management are used, are the distributed nature of the idea of distributed itself. It is recommended that readers start typing all the examples of this article in the IDE, and build clusters on Linux servers, so the above seemingly difficult problems will not be difficult.
table of Contents
Introduce the related concepts of microservice distribution, build the first microservice project, and understand the operation process of the microservice project. Integrate the Consul registry through microservices, and build the first microservice + registry distributed system. Multiple microservices are connected to the Consul registry and communicate with each other. The microservices obtain each other's interfaces and addresses, and call each other's interfaces and services.
Then it is inevitable to deal with the situation of reporting errors when communicating with each other, lest a single microservice cannot provide services normally, causing the paralysis of the entire distributed system. At this time, the Ribbon client load balancing solution is adopted, and multiple servers are deployed to deploy multiple same microservice projects to improve the performance of the system. When the distributed communication is not enough to solve all the error reporting problems, you can choose Hystrix to perform finer division to ensure that when any one of the microservices has problems, the system as a whole can still operate normally.
So far, a distributed system was initially built, and then business functions such as addition, deletion, modification, and checking of microservices were added. While the system handles the process of adding, deleting, modifying, and checking, it also needs the support and management of business functions. After the initial addition, deletion, modification, and checking, the microservice cache is used to increase the performance of the microservice. At the same time, it is necessary to ensure the consistency of addition, deletion, modification, and checking between Redis and MySQL. In addition, if there are special business requirements, distributed message communication can be used to collaborate, communicate, and process with each other.
After the basic business is processed, there will be business requirements such as timed tasks. At this time, task scheduling of microservices is required for processing. The single-node task scheduling microservices may have related problems such as downtime or repeated execution. Such problems can only be solved when multiple microservices are scheduled to perform task scheduling at the same time and cooperate with each other. At this time, Quartz distributed task scheduling is needed to solve the problems of coordination and mutual management among multiple task scheduling. If there are related requirements such as file upload and download, you need to use the file upload management of microservices. Single-node file upload may have problems such as insufficient disk space or difficult management. FastDFS distributed file management is required to solve the problems of coordination of multiple file management servers and disk expansion.
Chapter 1 Design Principles of Microservice Distributed Architecture;
Section 1.3 of this chapter uses Spring Boot to create the first microservice application. Section 1.5 modifies the port number of the application to 9090. Section 1.7 modifies the properties configuration file of the application to a YAML configuration file. Section 1.8 allows the project to adapt to multiple application scenarios through a single configuration file. Section 1.9 makes the project adapt to multiple environments for development through multiple configuration files.
In actual engineering, distributed environment integration often uses multi-environment configuration, and multi-environment configuration is a very important part of microservices. In addition, it is recommended to use bootstrap.yml and bootstrap.properties files in actual projects to facilitate future maintenance.
Chapter 2 Distributed Registration Center;
In section 2.3, we have a preliminary understanding of the Consul registry, and set up a cluster of Consul registry, so that the Consul registry can operate more well.
Section 2.4 uses Spring Cloud to integrate Consul to register and obtain the registration address of other services.
Section 2.5 uses Spring Cloud to integrate Consul's Config function. You can obtain configuration parameters on Consul through Java code, which facilitates multiple microservice projects to manage the same configuration information.
After getting other microservice information, Chapter 3 will introduce the use of Feign framework to call other microservice interfaces through the Consul registry.
Chapter 3 Distributed Communication;
Based on the distributed architecture of microservices, this chapter uses Feign to achieve the purpose of multiple microservices communicating with each other. When an exception occurs in the call to the Server side, the Feign downgrade rollback function returns.
Microservice integration Swagger reduces the communication cost between multiple programmers. After a programmer provides Swagger UI, other programmers can directly understand the interface address, name, input parameters, return value and other information. After getting the information of other interfaces, the interface provided by the server side microservice can be called through the Feign Client side. When Feign's interceptor calls other Server interfaces on the Feign Client side, the request is processed uniformly.
If an error occurs on the Server side called by Feign Client, Feign's Fallback class and Feign's Fallback factory use the downgraded fallback class and the downgraded fallback factory respectively to directly roll back the request to the front desk through the downgrading function, which will not cause Blocking of threads.
If the Feign Client side calls the Server side overtime, it can be solved by configuring Feign's built-in Ribbon load balancer. Once a timeout occurs on the server side, Feign will directly return the request to the foreground through the downgrade function, without causing thread blockage.
Chapter 4 Distributed Client Load Balancing;
By registering multiple microservices with the same microservice name in the same registry, Feign Client can use Ribbon to call any one of the microservices according to the algorithm, and improve the concurrency tolerance of the application through multiple servers. In a distributed architecture, distributed communication is one of the most important links. The registry ensures that distributed communication addresses can be obtained. Client load balancing reduces the concurrency pressure of distributed communication.
Chapter 5 Distributed Circuit Breakers;
Section 5.2 implements a more advanced downgrade rollback method. Multiple Feign Client Services are used in one function. If there is any exception, the entire function will fall back to the downgraded function.
Section 5.4 realizes that after calling a function, the return result of the function is cached to prevent high concurrency caused by multiple calls. The form of "function name + input parameters" is used as the key value, and the returned result is cached as the value value. . Use multiple Feign Client Services in a function to reduce the pressure of concurrency in communication between multiple microservices.
Section 5.5 realizes that after multiple calls to a function, multiple threads are merged into one thread for calling, which reduces the pressure on system memory and the number of concurrent threads. Use a Feign Client Service in a function to merge the IDs obtained multiple times into a List to request other microservices, reducing the pressure of concurrent communication between multiple microservices.
Section 5.6 uses Hystrix's built-in Dashboard performance monitoring dashboard page and performance monitoring controls to monitor the Hystrix thread pool of other microservices, including the current number of remaining threads in the Hystrix thread pool, thread pool capacity, concurrency, running status, execution times, etc. .
In fact, Hystrix's own Dashboard dashboard is very simple to use. In the microservices that need to be monitored, open the called address through the application resource configuration file, configure the performance control related content on the Spring container through the @Bean annotation, and monitor the Dashboard instrument on the end. Use the @EnableHystrix-Dashboard annotation to open the page, and call the relevant address of the Hystrix performance control on the page.
The three distributed swordsmen: Consul registration center (including communication Feign), microservice Spring Application, and circuit breaker Hystrix have all been introduced. Generally, in the initial stage of building a distributed system, three basic elements must be built first, and then business logic such as addition, deletion, modification, and query is added to each microservice.
Chapter 6 Asynchronous thread pool of microservices;
Asynchronous thread pool is a technology with complex underlying implementation but very simple method of use. It is more complicated to stop the test execution process of asynchronous thread pool gracefully. Pay attention to every detail here, especially after gracefully stopping the asynchronous thread pool and executing all the threads that should be executed, whether the number of threads in the thread pool is correctly reset to zero.
If you want to know more about optimizing the asynchronous thread pool, you can refer to Java's own thread pool, Java third-party thread pool, Java concurrent programming and other related content. Regardless of whether you use the thread pool that comes with Java or a third-party open source thread pool, it is just a different implementation for the asynchronous thread pool, and there is no difference. Because the graceful stopping of each thread pool is different, the graceful stopping of each asynchronous thread pool is also different.
Chapter 7: Microservices integrate persistent data sources;
This chapter integrates MyBatis annotations and JPA annotations through Spring Boot microservices to achieve the effect of adding, deleting, modifying, and checking operations on the database.
Since the pagination of JPA annotations and MyBatis annotations is very similar to multi-table, this chapter will not repeat the introduction. Through the use of @Select, @Query and other annotations in Section 7.2 and Section 7.4, you can understand the idea of using annotations for database development.
Chapter 8 Microservice Affairs;
A more complete understanding of @Transational annotations through the examples in Section 8.2, including the application of the annotations to multiple dao layers in the @Service layer, has expanded the content of Chapter 7. It is hoped that through this chapter, readers can have a clearer understanding of the use of transactions Methods, dissemination behavior, isolation level, etc.
Chapter 9: Microservice caching and distributed message communication;
Redis is the most concise non-relational cache database and is used more in actual work. Sections 9.2 and 9.6 use Spring Data Redis and Spring Cache to integrate Redis to implement the addition, deletion, and modification of the cache.
Section 9.5 uses Redis as a message communication middleware to communicate between multiple microservices.
Chapter 10 Task Scheduling and Distributed Task Scheduling of Microservices;
Section 10.1 integrates single-point task scheduling, and Section 10.4 integrates distributed task scheduling, which introduces the distributed method of task scheduling.
Quartz is very simple to use, and other distributed solution frameworks can also be used in actual projects. In fact, it is more important to understand the distributed principle of task scheduling, rather than memorizing APIs by rote.
Chapter 11 File upload and distributed file management of microservices;
Section 11.2 implements the microservice file upload, and Section 11.7 implements the microservice distributed upload.
Section 11.5.4 introduces the principle of distributed upload and the splitting of the upload server and file server into two servers for operation of the microservice. FastDFS installation is more complicated, please refer to related books.
Chapter 12 Expansion and Deployment;
This chapter expands the related solutions of the distributed architecture, and summarizes the Spring Boot and Spring Cloud framework extensions not covered in the previous chapters of this article.
to sum up
Because this [Microservices Distributed Architecture Foundation and Actual Combat] contains too much content, I can't show you all the content in detail. Friends who need the full version of the document can look below to get it!
Friends who need the full version of the document can get three links with one click, and get the free method below!