Talking about microservices and SpringCloud

Insert picture description here

Traditional single application:

The so-called single application program, in layman's terms, is to pile all the functions together. Most of this application is a war package or jar package. With the development of business and the increase of functions, this single project will become more and more bloated after many years.

Such a monolithic application is a relatively good solution in the initial stage of the company's establishment. It is relatively simple to quickly add new features or deploy and release. However, as time goes by, the crisis will gradually become apparent. Any one bug may cause the entire application to be paralyzed, and it will affect the whole body.

Microservice overview:

Microservices are an architectural style, that is, a single application is divided into small service units, and HTTP APIs are used for resource access operations between microservices.

In my opinion, the evolution of microservice architecture is more like the development process of a company, from a small company at the beginning to a large group later. A large group can split into multiple subsidiaries, each of which has its own independent business and employees, and develops independently without affecting each other. Together, they are infinitely powerful.

Advantages and disadvantages of using microservice architecture:

Microservices: bloated systems, repetitive codes, and long start-up time bring developers only infinite complaints, without the feeling of comfortable and smooth code writing. They spend most of their time solving problems and starting projects.

Advantage:

The use of microservice architecture can bring us the following benefits:
1. Independent deployment of services, each service is an independent project, can be deployed independently, does not depend on other services, and has low coupling.

2. Fast service startup, the service startup speed after the split is bound to be much faster than before the split, because there are fewer dependent libraries and less code.

3. More suitable for agile development: agile development takes the evolution of user needs as the core and adopts an iterative and gradual approach. Service splitting can quickly release a new version. To modify which service only needs to release the corresponding service, it does not need to be re-released as a whole.

4. The responsibilities are single-minded, and a dedicated team is responsible for specialized services: When the business develops rapidly, R&D personnel will also come in more and more. Each team can be responsible for the corresponding business line. The division of services is beneficial to the division of labor between teams .

5. Services can be dynamically expanded on demand: When a service has a large number of visits, we only need to expand the service.

6. Code reuse: Each service provides rest api, all basic services must be extracted, and many underlying implementations can be provided in interface mode.

Disadvantages:

Microservices are actually a double-edged sword, since there are advantages and disadvantages. Let's talk about the drawbacks of microservices and what can be done to avoid them.

1. Distributed deployment, high complexity of calls: In a single application, all the previous calls of the modules are performed locally. In microservices, each module is deployed independently and communicates through http , There will be many problems, such as network problems, fault tolerance problems, call relationships, etc.

2. Independent database, the challenge of distributed transactions: Each microservice has its own database, which is the so-called decentralized data management. The advantage of this model is that for different services, you can select the data that suits your business. For example, MySql can be used for order services, Mongodb can be used for comment services, and ElasticSearch can be used for product search services. The disadvantage is the problem of transactions, and the most ideal solution is currently The solution is the ultimate consistency in flexible transactions.

3. Increasing the difficulty of testing: Services and services interact through interfaces. When the interface changes, it will affect all the calling methods, and the workload will be too great. It is emphasized here that the management of api documents is particularly important.

4. Increased difficulty in operation and maintenance: When using traditional monolithic applications, we may only need to focus on a tomcat cluster, a Mysql cluster, but this is not feasible under the microservice architecture. When the business increases, there will be more and more services, and the deployment and monitoring of services will become very complicated. At this time, the requirements for operation and maintenance will be high.

Preparation before reconstruction:

The key to the lack of access to microservices lies in the level of development of the company. Has the system really reached the point where it must be decomposed? Before microservices, technology selection must be done. What framework is used to build microservices? Does the company support refactoring? These issues are very important, and everything is empty talk without the support of the company.

Before refactoring, the architect must sort out all the company's products and come up with a refactoring plan. It is best to adopt a gradual approach when refactoring. First, refactor a product to plan, extract business services, and then extract the basic services that the product depends on. Basic services are the most important. After one product is stable, restructure other products and put the core business to the end. Don't think about getting to the sky one step at a time. Refactoring is like piles of wood. When piled up, it will be high. One microservice a week will gradually become a microservice.

Springcloud overview

springcloud is an ordered collection of a series of frameworks. It utilizes the convenience of springboot development, and cleverly simplifies the development of distributed system infrastructure, such as service registration, service discovery, configuration center, message bus, load balancing, circuit breakers, data monitoring, etc. All of these can be started and deployed with one click using the springboot development style. In layman's terms, springcloud is a collection of frameworks used to build microservice development and governance (not a specific framework)

Five components

1. Eureka Registry

Insert picture description here

The registry is an indispensable part of the microservice architecture, mainly to implement service governance functions. Eureka is a REST-based service and provides a Java-based client component, which can easily register services to springCloud Eureka Perform unified management. The registry is nothing more than managing the information and status of all services. If we use examples in our lives to illustrate, the 12306 website is more suitable.
First of all, the 12306 website is like a registration center, and customers are like calling clients. When they need to take a train, they will log in to check the remaining tickets on the 12306 website, and they can buy tickets if they have them, and then get the train number, time, etc. Finally set off.

The program is the same. When you need to call a certain service, you will first go to Eureka to pull the service list, check whether the service you call is in it, and if it is, get the service address, port and other information, and then call it.

The advantage of the registration center is that you don’t need to know how many providers there are, you only need to pay attention to the registration center, just like customers don’t need to care about how many trains are running, just go to the 12306 website to see if there are tickets. .

2. Client Load Balancing Ribbon

The current mainstream load balancing schemes are divided into two types: one is centralized load balancing, which uses independent proxy methods for load between consumers and service providers. There are hardware and software (such as Nginx). The other is that the client does load balancing by itself, and does load according to its own request. Ribbon is a tool for client load balancing.

Ribbon is a client-side load balancing framework. The default load strategy is polling, and it also provides many other strategies to allow users to choose according to their own business needs. As shown

3.Hystrix service fault tolerance processing

Insert picture description here

There are multiple directly callable services in the microservice architecture. If these services fail during the call, it will cause a knock-on effect, that is, the entire system may become unavailable. This situation is called the avalanche effect of services.

We can solve the service avalanche service through Hystrix. Hystrix is ​​a fuse protection middleware used for microservice distributed systems, which is equivalent to a fuse in a circuit. Under the microservice architecture, many services are dependent on each other. If the dependent services cannot be isolated, then the service itself may fail. Hystrix isolates the calls through HystrixCommand, which can prevent the chain effect of failures and make interface calls fast Fail and quickly return to normal, or fall back and gracefully degrade.

4. API gateway

Insert picture description here

The API gateway is an entrance to external services. It hides the implementation of the internal architecture. The API gateway can manage a large number of API interfaces for us. It can also interface with customers, adapt protocols, perform security authentication, forward routing, limit traffic, monitor logs, Prevent crawlers, perform grayscale releases, etc., for example, users evaluate a community. After the evaluation is completed, they need to display the details of the community, housing price trends, transaction data, listing data, etc. These service information are in different services. The front-end service wants to achieve such a The function needs to interact with many services and call the interfaces they provide, so the performance is definitely low, and the logic of the front-end system is more complicated, it needs to know all the microservices that provide information. At this time, the role of the Api gateway is reflected. It aggregates internal services through API, provides a unified external API interface to the front-end system, and shields internal implementation details.

As the gateway in the springcloud ecosystem, Gateway not only provides a unified routing method, but also provides the basic functions of the gateway based on the Filter chain, such as security, monitoring/buying points, and current limiting.

5. Distributed configuration

Insert picture description here

Under the microservice architecture, the number of services can range from dozens to hundreds or even thousands. Each time you modify a configuration, you need to follow up and modify multiple items, and then restart these items. The centralized management of the configuration is particularly important in this case. Distributed configuration management can centrally manage the configuration of multiple projects. Unified modification, effective in real time, avoiding duplication of labor, saving time and reducing the probability of errors.

Spring cloud Config is a service used to provide centralized management of configuration for distributed systems. It is divided into two parts: client and server. The client service pulls configuration data from the server, and the server is responsible for providing configuration data.

The underlying storage of Springcloud Config provides a variety of ways. The best is to use git to store configuration information. You can also track the version and restore to the specified version at any time. Of course, it also supports svn, local file storage and other methods.

Guess you like

Origin blog.csdn.net/weixin_46011971/article/details/108750811