[principle, design and practice of distributed service architecture] Reading Notes (I) principle of distributed microservice architecture design

Time:2020-3-25

1.1 from traditional single architecture to service architecture

1.1.1 Jee architecture

Jee divides the enterprise software architecture into three levels:

  • Web layer: responsible for interacting with users or providing external interfaces.
  • Business logic layer: process processing and calculation processing module designed to realize business logic.
  • Data access layer: persist the results processed by the business logic layer for subsequent queries, and maintain the life cycle of objects in the domain model.

Jee platform aggregates different modular components and runs them on common application servers, such as Weblogic, WebSphere, JBoss and Tomcat

Figure 1-1 typical architecture of Jee Era
[principle, design and practice of distributed service architecture] Reading Notes (I) principle of distributed microservice architecture design

1.1.2 SSH architecture

  • Struts、Spring、Hibernate
  • View, model, controller (struts MVC)

Figure 1-3 MVC model
[principle, design and practice of distributed service architecture] Reading Notes (I) principle of distributed microservice architecture design

  • Spring IOC — >
  • Spring AOP — >

Figure 1-4 architecture in SSH Era
[principle, design and practice of distributed service architecture] Reading Notes (I) principle of distributed microservice architecture design

1.1.3 service architecture

From Jee to SSH, services are still characterized by singleness, and the granularity of services is abstracted into modular components. All components are coupled in a project, and configured and run in a JVM process.
To solve these problems, SOA has emerged

  • SOA stands for service-oriented architecture, commonly known as servitization.
  • SOA passes the modular components of an application through【Defined interfaces and contracts】Interfaces are defined in a neutral way, independent of a language, hardware, and operating system.
  • SOA is usually completed through network communication, but it is not limited to some kind of network protocol. It can be the underlying TCP / IP, the application layer HTTP, the message queuing protocol, or even some kind of agreed database storage form.

The development of SOA Service

1、 Web Service
An implementation of SOA Service makes it possible for services running on different machines and operating systems to discover and call each other, and exchange data through some protocol.
Figure 1-5 working principle of Web Service
[principle, design and practice of distributed service architecture] Reading Notes (I) principle of distributed microservice architecture design

  • The service providers web service 2 and web service 3 register the service into the web service directory service through UDDI protocol.
  • The service consumer Web Service 1 queries the service from the web service directory through UDDI protocol, and obtains the WSDL service description file of the service.
  • The service consumer Web Service 1 calls and consumes the services provided by web service 2 and web service 3 remotely through the WSDL language.

Two, ESB
ESB is the abbreviation of enterprise service bus. It is a software model designed and implemented by users for network service interaction and communication. It is another way to implement SOA, which is mainly used in the integrated service scenario of enterprise information system.
ESB is also suitable for the scenarios of event processing, data transformation, mapping, message and event asynchronous queue sequence processing, security and exception processing, protocol transformation, and ensuring the quality of communication services.
Figure 1-6 ESB architecture
[principle, design and practice of distributed service architecture] Reading Notes (I) principle of distributed microservice architecture design
Each service provider is inserted into the system through the bus mode. The bus is responsible for transforming the output of the service according to the arrangement of the process and sending it to the next service required by the process for processing.

The core of ESB is the function and responsibility of enterprise service bus

  • Monitor and control message routing between services.
  • Control pluggable, service-oriented features and versions.
  • Analyze the content and format of interaction and communication between services.
  • Through the combination of services, resources and message processors to unify the information processing flow required by the business.
  • Use redundancy to provide backup capabilities for services.

1.2 from service to micro service

Figure 1-7 microservice architecture
[principle, design and practice of distributed service architecture] Reading Notes (I) principle of distributed microservice architecture design

1.3 core points and implementation principles of microservice architecture

1.3.1 functional team division in microservice architecture

Conway’s law

1.3.2 decentralized governance of microservices

1.3.3 interaction mode of microservices

1. Tolerance reader

How to make fault tolerance for interface changes between service providers and consumers in microservices.

In the process of processing the messages returned by service providers, service consumers need to filter the messages returned by services, extract the content they need, and discard the redundant or location content, which is not a hard mistake.

Loose verification strategy is recommended. Only when the information is unrecognized and the process continues, can errors be thrown.

2. Consumer driven contract model

The consumer driven contract pattern is used to define the best rules for the change of the interaction interface between services.
Service contract is divided into provider contract, consumer contract and consumer driven contract. It describes the linkage between service provider and consumer from the perspective of expectation and constraint.

  • Provider contract: provider centered, unconditional compliance by consumers
  • Consumer contract: a more precise description of a consumer’s needs can be used to identify existing provider contracts, or to discover a provider contract that has not yet been defined.
  • Consumer driven contract: a constraint that represents a service provider’s commitment to all of its current consumers. Once consumers tell their specific expectations to the provider, the provider should not break the contract at any time and in any scenario.

Figure 1-10 three service contracts needed for interaction between services
[principle, design and practice of distributed service architecture] Reading Notes (I) principle of distributed microservice architecture design

3. To data sharing mode

1.3.4 decomposition and composition mode of microservices

1. Service agent mode

According to the business requirements, select a service to call the backend. Before returning to the user, you can process the output of the backend service, or directly return the return result of the backend service to the user.
Figure 1-12 service agent mode
[principle, design and practice of distributed service architecture] Reading Notes (I) principle of distributed microservice architecture design

[typical case: smooth system migration]

  1. Double write on old and new systems.
  2. Migrate historical legacy data before double write.
  3. Switch read request to new system“Service agent mode”
  4. Down double write logic, write only the new system.

Step 3: generally, only one switch will be designed for read request switching. When the switch is turned on, the new system will be queried, and when the switch is turned off, the old system will be queried.

Figure 1-13 switch logic in migration case
[principle, design and practice of distributed service architecture] Reading Notes (I) principle of distributed microservice architecture design

2. Aggregate service pattern pattern

The most commonly used service composition mode, according to the needs of business process processing, calls multiple dependent microservices in a certain order, combines, processes and transforms the data returned by the dependent microservices, and returns it to the user.
Figure 1-14 architecture of service aggregation mode
[principle, design and practice of distributed service architecture] Reading Notes (I) principle of distributed microservice architecture design

3. Service tandem mode

Similar to workflow, the first service 1 is responsible for receiving the request and the corresponding user. After cascading services, it interacts with service 1, then service 1 interacts with service 2. Finally, the results generated from service 2 are processed by service 1 and cascading services one by one and returned to the user.

Figure 1-17 architecture of service tandem mode
[principle, design and practice of distributed service architecture] Reading Notes (I) principle of distributed microservice architecture design

● using restful style remote call implementation;
● adopt synchronous calling mode, all services will block and wait before the serial service returns;
● a request will occupy a thread to process;
● too many levels are not recommended. If service aggregation mode can be used, service aggregation mode is preferred;
● add nodes on the serial link, as long as they are not in the front and back, the serial service has no perception

Fig. 1-18 the last and no sense architecture for calling chain in serial services.
[principle, design and practice of distributed service architecture] Reading Notes (I) principle of distributed microservice architecture design

Figure 1-19 architecture of service series mode case
[principle, design and practice of distributed service architecture] Reading Notes (I) principle of distributed microservice architecture design

4. Service branch mode

Service branch mode is a combination of service agent mode, service aggregation mode and service tandem mode.
● Branch services can have their own storage, call multiple back-end services or service chain in series, and then combine the results and return them to the client.
● the branch service can also use the proxy mode to simply call a service or service chain in the back end, and then return the returned value directly to the user.

Figure 1-20 architecture of service branch mode
[principle, design and practice of distributed service architecture] Reading Notes (I) principle of distributed microservice architecture design

There are multiple layers in the call chain that repeatedly call a basic service, resulting in the cumulative effect of the traffic affected when the basic service hangs up:
Assuming that the number of machines in the basic service resource pool is I, the number of machines that hang up at one time is j, and the basic service in a call chain is called X.

Success rate = ((I-J) / I) x

The branch pattern enlarges the dependency of services, and tries to keep the service call level simple in the real design. When using the service composition and service proxy pattern, do not use the service concatenation pattern and service branch pattern to keep the service dependency clear.

5. Service asynchronous message mode

The core system service uses synchronous call, and the service outside the core link can use asynchronous message queue for asynchronization.

Figure 1-20 architecture of service asynchronous message mode
[principle, design and practice of distributed service architecture] Reading Notes (I) principle of distributed microservice architecture design

5. Service sharing data mode

It’s anti model

Figure 1-25 service sharing data mode
[principle, design and practice of distributed service architecture] Reading Notes (I) principle of distributed microservice architecture design

[in the following two scenarios, data sharing mode is still needed]:

1、 Cellular architecture

High performance requirements.
Figure 1-26 schematic diagram of modular architecture
[principle, design and practice of distributed service architecture] Reading Notes (I) principle of distributed microservice architecture design

2、 Legacy overall services

In the process of microservice reconstruction, it is found that the database tables that the monomer services depend on are coupled together, and their splitting needs to be de normalized, which may cause data consistency problems.

1.3.4 fault tolerant mode of microservices

1. Bulkhead isolation mode
1) Microservice container grouping
Each node service pool of microservices is divided into three groups:

  • Quasi production environment;
  • Gray environment;
  • Production environment.

Figure 1-27 service grouping
[principle, design and practice of distributed service architecture] Reading Notes (I) principle of distributed microservice architecture design

2) Thread pool isolation
Figure 1-28 thread pool isolation
[principle, design and practice of distributed service architecture] Reading Notes (I) principle of distributed microservice architecture design

2. Fusing mode

  • Use circuit safety switches such as fuse mode.
  • For microservice system, when the input load of service increases rapidly, if there is no effective measure to fuse the load, the service will collapse rapidly.

Figure 1-29 fusing mode
[principle, design and practice of distributed service architecture] Reading Notes (I) principle of distributed microservice architecture design

3. Current limiting mode
There are several mainstream methods to achieve current limitation as follows:
1) Counter

2) Token bucket
Figure 1-32 token bucket structure
[principle, design and practice of distributed service architecture] Reading Notes (I) principle of distributed microservice architecture design

3) Semaphore

Similar to the loopholes in life
Example:

public class SemaphoreExample {
    private ExecutorService exec = Executors.newCachedThreadPool();
        public static void main(String[] args) {
            final Semaphore sem = new Semaphore(5);
            for (int index = 0; index < 20;  index++) {
                Runnable run = new Runnable() {
                    public void run() {
                    try {
                        //Get permission
                        sem.acquire();
                        //At the same time, only five requests can arrive here
                        Thread.sleep((long) (Math.random()));
                        //Release license
                        sem.release()
                        System. Out. Println ("remaining licenses:" + SEM. Availablelimits());
                    } catch(InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            exec.execute(run);
        }
        exec.shutdown();
    }
}

4. Implementation of transfer mode
[if the microservice architecture is broken or current limiting, how to handle the rejected request]

  • Use [quick failure] to directly return the user’s error
  • Whether there is [backup], if so, switch to the backup service quickly
  • [failover strategy] is solved by retry, which requires the service provider to implement idempotence

1.4 project organization form of Java platform microservice architecture

1.4.1 dependency of microservices

In the field of microservices, jar packages are divided into:

  • One party Library: jar package that the service depends on in the JVM process.
  • Second room Library: jar package of the service called through network communication or RPC outside the service.
  • Tripartite Library: raised by other companies or organizations

1.4.2 hierarchy of microservices

Java microservice projects are generally divided into service export layer, interface layer and logic implementation layer
[principle, design and practice of distributed service architecture] Reading Notes (I) principle of distributed microservice architecture design

  • Service export layer: finally, it will be packaged into a war package, including the service implementation jar package, interface jar package, and the configuration files required by the web project to export RPC services.
  • Service interface layer: it includes business interface, dependent dto, enumeration class, etc. Finally, it is packaged into jar package, published to Maven server, and also included in war package of service export layer.
  • Service implementation layer: it includes the business logic implementation class, the packaging class of dependent third-party services, and the Dao class of lower level database access. Finally, it is packaged into jar.

[architecture diagram of service implementation layer]
[principle, design and practice of distributed service architecture] Reading Notes (I) principle of distributed microservice architecture design

[service implementation layer’sAnti patternArchitectural diagram]
[principle, design and practice of distributed service architecture] Reading Notes (I) principle of distributed microservice architecture design

1.4.3 continuous release of microservices

Microservice projects need CI / CD for automation, including:

  • Code management
  • Automatic compilation
  • Release OA
  • automated testing
  • performance testing
  • Quasi production deployment and testing
  • Build environment Publishing

1.5 technical selection of service management and governance framework

1.5.1 RPC

1、JDK RMI

Java to Java distributed call framework, services in one java process can call services in other Java processes, using the built-in serialization and deserialization protocol of JDK.

  • Serialization protocol: the special serialization protocol of JDK, which cannot be cross language
  • Network transmission protocol: underlying network protocol

2、Hessian & Burlap

  • Serialization protocol: Hessian serialization to binary protocol; burlap serialization to XML data
  • Network transport protocol: http protocol

3、Spring HTTP Invoker

  • Serialization protocol: Hessian serialization to binary protocol; burlap serialization to XML data
  • Network transport protocol: http protocol

1.5.2

1、Dubbo

  • Provides high-performance, transparent RPC remote service calls, as well as basic service monitoring, quality of service and service scheduling
  • It supports multiple serialization protocols and communication coding protocols. By default, Dubbo protocol is used to transmit Hessian serialized data
  • Use zookeeper as a registry to register and discover services
  • The load balancing algorithm includes: random, polling, minimum number of active calls, consistent hash and so on.

2、HSF

High Speed Framework

3、Thrift

  • Define and create services using an intermediate interface description language
  • Support cross language service development and invocation, and include intermediate interface description language and code generation and transformation tools
  • Using binary serialization to transfer data

4、AXIS

From IBM “soap4j”, using SOAP Protocol

5、Mule ESB

The enterprise service bus product based on Java language can integrate multiple heterogeneous systems through the bus pattern

1.5.3 microservices

1、Spring Boot
[figure 1-37 architecture of application contained in container in Jee era]
[principle, design and practice of distributed service architecture] Reading Notes (I) principle of distributed microservice architecture design

Spring boot, on the contrary, embeds the container in the jar package of self startup. When the spring boot application is started, the embedded container is started internally
[architecture diagram of the container of spring boot included in the application]
[principle, design and practice of distributed service architecture] Reading Notes (I) principle of distributed microservice architecture design

The design of spring boot has the following obvious points under the microservice architecture:

  • Independent, self starting applications can be created
  • There is no need to build the War package and publish it to the container, and there is also a cost to build and maintain the War package, the configuration and management of the container
  • With Maven’s customized tags, you can quickly build spring boot applications
  • You can maximize the automatic configuration of spring without having to manually configure various parameters
  • Product features such as performance analysis, health check and external configuration are provided
  • No XML configuration
  • It is an important foundation for spring cloud to build microservice architecture

2、Netflix

Provide service discovery, breaker and monitoring, intelligent routing, client load balancing, easy-to-use rest client and other necessary functions

3、Spring cloud Netflix

  • Service discovery component Eureka
  • Fault tolerant component hystrix
  • Intelligent routing component zuul
  • Client load balancing component ribbon

[figure 1-39 spring cloud Netfix Architecture]
[principle, design and practice of distributed service architecture] Reading Notes (I) principle of distributed microservice architecture design

[Netflix interaction process]

  1. Service is registered in Eureka server instance
  2. Zuul registers and discovers services on Eureka as a special service
  3. Zuul, as a gateway, exports discovered services to clients
  4. Resttemplate and feignclient use simple service call methods to call services 1 and 2

[Netflix features]

  • Services are registered in Eureka and are discovered and called by spring managed beans
  • The embedded Eureka server can be started by configuration
  • Feign client can import service agent by declaration
  • Zuul uses ribbon service to achieve client load balancing
  • The client of hystrix can be inserted by declaration
  • Configure to start the hystrix panel server
  • Netflix components can be configured directly in spring environment
  • Zuul can automatically register filters and routers to form a reverse proxy server
  • The hystrix panel can monitor the status of services and provide fault tolerance mechanism

Recommended Today

Understanding and deepening of relative path and absolute path

What is relative path and absolute path Last week’s report solved some problems, but also exposed many problems, one of which is the relative path and absolute path. For PHP using xampp to build a server, the relative path refers to the current file relative to the user’s access, and the absolute path refers to […]