Micro service

Microservices architecture is a way of replacing the development of a single large application with a handful of microservices, each running in its own process and communicating with a lightweight mechanism, usually HTTP RESTful apis. Micro service emphasizes small and quick, any relatively independent function service is no longer a module, but an independent service. Microservices are an ecosystem, not a technology

Features of microservices

Autonomy (loose coupling)

Each component service in the microservices architecture can be developed, deployed, operated, and extended without affecting the functionality of other services. These services do not need to share any code or implementation with other services. Any communication between components takes place through well-defined apis.

specificity

Each service is designed for a set of functions and is focused on solving a specific problem. If developers gradually add more code to a service and the service becomes complex, it can be broken up into smaller services.

Flexible extend

With microservices, you can independently extend each service to meet the needs of the application functionality it supports. This enables the team to appropriately adjust infrastructure requirements, accurately measure the cost of features, and maintain availability during spikes in service demand.

Easy to deploy

Microservices support continuous integration and continuous delivery, make it easy to try out new ideas, and can be rolled back when they don’t work properly. With lower failure costs, you can experiment, update code more easily, and reduce time-to-market for new features.

The current development of micro services

The current mainstream is springCloud and Dubbo, so let’s make a comparison between them:

Dubbo

A high-performance, lightweight, open source Java RPC framework that provides three core capabilities: interface-oriented remote method invocation, intelligent fault tolerance, load balancing, and automatic service registration and discovery.

Node roles:

Provider: exposes the service Provider of the service. Consumer: Service Consumer that invokes the remote service. Registry: A Registry where services are registered and discovered. (Dubbo types generally use ZooKeeper as the registry.) Monitor: Monitors the call times and call time of services. Container: service running Container.

Call relationship description:

The service container is responsible for starting, loading, and running the service provider. At startup, service providers register their services with the registry. At startup, service consumers subscribe to the registry for the services they need. The registry returns a list of service provider addresses to the consumer, and if there are changes, the registry pushes the change data to the consumer based on the long connection. The service consumer, from the provider address list, selects one provider to call based on the soft load balancing algorithm. If the call fails, selects another one to call. Service consumers and providers accumulate calls and call times in memory and regularly send statistics to the monitoring center every minute.

SpringCloud vs. Dubbo

SpringCould is a microservice architecture based on SpringBoot. Including package service discovery (Eureka), Circuit breaker (Hystrix), Service Gateway (Zuul), client load Balancing (Ribbon), service Tracing (Sleuth), Message Bus (Bus), message Driver (Stream), batch Task (Task), etc. Spring Cloud abandons Dubbo’s RPC communication in favor of an HTTP-based REST approach. Dubbo and Spring Cloud are not completely competitive and address different problem domains: Dubbo was always positioned as an RPC framework, while Spring Cloud was intended as a one-stop solution for microservices.

SpringCloud

Service discovery — Netflix Eureka

Eureka consists of two components: Eureka Server and Eureka Client

Eureka Server provides service discovery capability. When each micro-service is started, it registers its own information (service name, IP address, port number, etc.) with Eureka Server, and the Eureka Server stores this information. Zookeeper Eureka Client is a Java Client used to simplify the interaction with Eureka. Heartbeat mechanism: After each micro-service is started, it will send a heart to Eureka Server every certain time (default 30s) to let it know that it throws healthy survival. If the Eureka Server does not receive the heartbeat of a microservice instance within a certain period of time (default 90s), the Eureka Server will cancel the instance Eureka cluster: By default, the Eureka Server is also a Eureka Client. Data in the service registry is synchronized between multiple Eureka servers through replication. The Eureka Client caches the information in the service registry and does not need to request the Eureka Server every time, which relieves the pressure on the Eureka Server. Even if all Eureka Server nodes go down, service consumers can still query their cache for information to invoke the service provider. Self-protection mechanism: This is not allowed if the instance is deregistered because the heartbeat mechanism detects that the service is dead due to network reasons. Therefore, the self-protection mechanism solves this problem. When The EurekaServer node loses too many clients in a short period of time (due to network reasons), the node will enter the self-protection mode. Once it enters this mode, EurekaServer will protect the information in the service registry. Data in the service registry is no longer deleted (that is, no microservices are logged out). When the network recovers, the EurekaServer exits the self-protection mechanism. The self-protection mechanism makes Eureka cluster more stable and robust.

The principle of CAP

The CAP principle, also known as the CAP theorem, refers to the importance of Consistency, Availability and Partition tolerance in a distributed system. CAP principle is the cornerstone of NOSQL (Redis, mongDB) database.

Consistency: Whether all data backups in a distributed system have the same value at the same time. Availability: Whether the cluster as a whole can respond to read/write requests from clients when some nodes fail. Partition tolerance: In practical effect, partitioning is a time-limit requirement for communication. If the system cannot achieve data consistency within the time limit, it means that partitioning has occurred and it must choose between C and A for the current operation (P must be guaranteed under clustering).

Only two of CAP’s three features can be satisfied:

CA without P: C (strong consistency) and A (availability) are guaranteed if P is not required (partitioning is not allowed). However, giving up P means giving up the expansibility of the system, that is, the distributed nodes are limited and cannot deploy child nodes, which is against the original intention of the distributed system design. Traditional relational database RDBMS: Oracle, MySQL is CA.

CP without A: If A (available) is not required, it means that each request needs to maintain strong consistency between servers, while P (partition) will lead to infinite synchronization time (that is, wait for data synchronization to complete before normal access to the service). Once network failure or message loss occurs, user experience will be sacrificed. Wait for all data to be consistent before allowing users to access the system. In fact, there are many systems designed as CP, the most typical of which is distributed database, such as Redis and HBase. For these distributed databases, data consistency is a fundamental requirement, because if this standard is not met, then simply use a relational database, and there is no need to waste resources deploying a distributed database.

AP WiHTout C: To be highly available and allow partitioning, consistency is waived. Once partitioning occurs, nodes may lose contact with each other, and for high availability, each node can only serve with local data, which can lead to global data inconsistencies. A typical application is like the scene of buying up a mobile phone in a certain meter. It may be that there is stock on the page when you browse the product in the first few seconds. When you finish selecting the product and prepare to place an order, the system will prompt you that the order has failed and the product has been sold out. In fact, this is to ensure the normal service of the system in terms of A (availability), and then make some sacrifices in terms of data consistency. Although some user experience will be affected, it will not cause serious congestion in the shopping process of users.

Zookeeper and Eureka

Zookeeper ensures that CP ZooKeeper has the following problems: When the master fails, a new master is elected. However, the cluster is unavailable during the election period and the election lasts for a long time. Eureka guarantees AP Eureka guarantees availability, so that does not happen. Because every Eureka node is equal, the failure of several nodes will not affect the overall work. When a Eurek client fails to register, it will automatically switch to other nodes for registration. As long as there is still one surviving node, it will ensure availability, but the data queried is not the latest. Therefore, Eureka can cope well with the loss of nodes due to network failure, rather than the whole registration service breakdown like ZooKeeper.

Customer service load balancing – Netflix Ribbon

The key to Ribbon implementation is the RestTemplate, which is customized for Ribbon. Ribbon uses the interceptor mechanism of RestTemplate to implement Ribbon load balancing in the interceptor. The basic implementation of load balancing is to use applicationName to get a list of available service addresses from the service registry, and then decide which service address to use for HTTP calls through some algorithmic load.

Load Balancing classification

Centralized LoadBalance uses a separate LoadBanlance device, such as Nginx (reverse proxy server), between the consumer and provider. Access requests are distributed to providers by the device. The process LoadBalance integrates the logic of LoadBanlance into the consumer, who retrieves the available address from the registry and then selects the appropriate server by policy. The Ribbon is typically progressive.

Load balancing algorithm

Random load balancing (default) : Servers in the UP state are randomly selected. Simple polling Load balancing: Servers with different request scheduling schedules are loaded in polling mode. Weighted response time load balancing: A weight is assigned according to the response time. The longer the response time is, the smaller the weight is and the less likely the server is to be selected. Area aware Polling Load balancing: Select the server based on the performance and availability of the server in the region

Circuit breaker — Netflix Hystrix

Hystrix provides strong fault tolerance for delays and failures in distributed systems. When a node fails, Hystrix ensures that the whole does not fail, increasing the resiliency of distributed systems

Service fusing

When a microservice fails in the microservice architecture, the service should be cut off quickly, prompting the user to return directly to release resources without invoking the service in subsequent requests. This is called service circuit breaker. After the circuit breaker takes effect, a request is invoked to test whether the dependency is restored after a specified period of time. After the dependent application recovers, the circuit breaker is disabled.

Service degradation

Under the high concurrency of the server, when the pressure increases sharply, according to the business situation and traffic, some services and pages are strategically degraded (can be understood as closing unnecessary services), so as to relieve the pressure on server resources and ensure the normal operation of core tasks. During the Period of Singles’ Day, many functions of Alipay will prompt, [During the period of singles’ Day, core transactions will be guaranteed, and certain service data will be delayed].

Service Gateway — Netflix Zuul

Gateway: the unique external gateway of the system, located between the client and server, used for request authentication, traffic limiting, routing, and monitoring.

Zuul provides two main functions: routing and filtering

The routing function is responsible for forwarding external requests to specific microservice instances and is the basis for achieving a unified entry point for external access. The filtering function is responsible for intervention in the process of request processing. It is to realize the integration of Zuul and Eureka in the processing of request verification and authentication, register Zuul itself as an application under Eureka service governance, and obtain the messages of other micro-services from Eureka. In other words, the access to microservices in the future is obtained through Zuul jump. Note: The Zuul service will eventually be registered with Eureka

Popular point is unified access address

Distributed configuration — Spring Cloud Config

In a distributed system, due to a large number of services, a distributed configuration center is required to facilitate unified management and real-time update of service configuration files. In Spring Cloud, there is a distributed configuration center component, Spring Cloud Config, which enables configuration services to be placed in the memory of the configuration service (that is, locally) as well as in a remote Git repository. In the Spring Cloud Config component, there are two roles: Config Server and Config client. The server stores the configuration file and provides the content of the configuration file through an interface. The client obtains data through the interface and initializes its own application based on the data.

The last

You can leave a comment below to discuss what you don’t understand. You can also pay attention to my private letter and ask me. I will answer it after I see it. Also welcome everyone to pay attention to my official account: bright future, Golden Three silver four job-hunting interview season, sorted out more than 1000 nearly 500 pages of PDF documents of Java interview questions, articles will be updated in it, sorted out the information will be placed in it. Thank you for watching, think the article is helpful to you remember to pay attention to me a thumb-up support!