Detailed Explanation of SpringCloud Microservice Architecture

SpringCloud Alibaba

1.1. Monolithic distributed cluster

Monolith : also known as stand-alone structure, a project is all deployed on one server, and all service resources of the entire project are provided by this server.
insert image description here

Distributed : As the project becomes larger and larger, the processing capacity of the server in the single system is limited, so the project service and the MySQL service are stored on two or more servers respectively, and the server hardware can be controlled by reasonable deployment of the project. Customization.
insert image description here

Cluster : In a distributed structure, there may be a single point of failure problem. At this time, the service is backed up to provide the same service, thus forming a "cluster", and each server in the cluster is a node; in order to make these nodes They can all have the same workload, and the load balancer will work.

insert image description here

name advantage shortcoming
monomer Easy to develop, deploy and test The processing capacity of a single machine is limited, and the business grows to a certain extent, and the hardware resources cannot meet the business needs
distributed Configure server hardware according to the project and rationalize resources There is a single point of failure problem, once the server goes down, the service cannot be provided
cluster Ended the problem of single machine failure, load balancing It is relatively complicated to implement, and once the cluster is huge, it is difficult to maintain each node

1.2. Evolution of system architecture

Single Application Architecture -> Vertical Application Architecture -> Distributed Architecture -> SOA Architecture -> Microservice Architecture

1.2.1. Monolithic application architecture

In the early days of the Internet, applications were relatively small and simple, and all functional codes were deployed together to reduce development, deployment, and maintenance costs.

insert image description here

Advantages :

  • The project structure is simple, and the development cost is low for small projects
  • The project is deployed on one node, which is easy to maintain

Disadvantages :

  • All functions are integrated in one project, which is not easy to develop and maintain for large projects
  • Tight coupling between project modules, low single-point fault tolerance
  • Unable to perform targeted optimization and horizontal expansion for different modules

1.2.2. Vertical application architecture

​ With the increase of visits, the single structure can only be dealt with by adding nodes, but it is found that not all modules have a large number of visits. Taking the above e-commerce as an example, the increase in user visits may affect Only the user and order modules are used, but the impact on the message module is relatively small. So at this time we hope to add only a few more order modules, but not the message module. At this time, the single application cannot be done, and the vertical application should be used. And was born.

​ The so-called vertical application architecture is to split an original application into several independent applications to improve efficiency. For example, we can split the e-commerce single application above into:

  • E-commerce system (user management, product management, order management)
  • Background system (user management order management customer management)
  • CMS system (advertising management marketing management)

insert image description here

Advantages :

  • System splitting realizes traffic sharing, solves concurrency problems, and can be optimized and horizontally expanded for different modules
  • Problems in one system will not affect other systems, improving fault tolerance

Disadvantages :

  • The systems are independent of each other and cannot call each other
  • The systems are independent of each other, and there will be repeated development tasks

1.2.3, layered architecture

But with more and more vertical applications, there will be more and more duplicate business codes. We consider extracting repetitive codes, which forms a layered architecture that combines the presentation layer and the service layer. The business layer contains business logic; the presentation layer only needs to handle page interactions.

insert image description here

Advantages :

  • Extract common functions as service layer to improve code reusability

Disadvantages :

  • The degree of coupling between systems becomes high, and the relationship between applications is intricate and difficult to maintain

1.2.4, SOA architecture

Under the distributed development, the waste of small service resources is gradually emerging. Add a scheduling center to manage the cluster in real time, user resource scheduling and governance center, emphasizing service- oriented

insert image description here

Advantages :

  • Use the registration center to solve the automatic adjustment of the call relationship between services

Disadvantages :

  • There will be dependencies between services, and once a certain link goes wrong, it will have a greater impact (service avalanche)
  • The service relationship is complex, and O&M, testing and deployment are difficult

1.2.5. Microservice architecture

The microservice architecture is to some extent the next step in the continued development of the service-oriented architecture SOA, which puts more emphasis on the "complete split" of services.

insert image description here

Advantages :

  • Atomized splitting of services, independent packaging, deployment and upgrades, ensure clear task division of each microservice, and facilitate expansion
  • Microservices use lightweight Http protocols such as RESTful to call each other

Disadvantages :

  • The technical cost of distributed system development is high (fault tolerance, distributed transactions, etc.)

1.3. Introduction to microservice architecture

Microservice architecture : Simply put, it is to further split the single application into smaller services, and each service is a project that can run independently.

insert image description here

1.4. Introduction to Spring Cloud

​Spring Cloud is a collection of frameworks . The use of Spring Boot development simplifies the development of distributed systems, such as service discovery registration, configuration center, message bus, load balancing, circuit breakers, and data monitoring; Spring Cloud combines the mature and tested frameworks of various companies, and finally develops A set of distributed system development tools that are easy to understand, deploy and maintain.

​ SpringBoot focuses on the rapid and convenient development of individual microservices; while SpringCloud focuses on the global microservice coordination and governance framework, which integrates and manages individual microservices developed by SpringBoot to provide configuration management, Integration services for service discovery, circuit breakers, routing, event bus, distributed systems, etc. In general: SpringBoot focuses on the quick and convenient development of individual microservices, and SpringCloud focuses on the collection of global service governance components.

Service Governance Nacos Discovery

2.1. What is service governance

Service governance : the most core and basic module in the microservice architecture, users can realize the automatic registration and discovery of each microservice

Service registration : Each service unit first registers the detailed information of its own service in the registration center. The service registration center checks whether the services in the list are available by means of heartbeat, and eliminates unavailable services.

Service Discovery : The service caller consults the service registration center to access specific service instances.

insert image description here

2.2. Common registration centers

  • Zookeeper: It is a distributed service framework, mainly used to solve some data management problems in distributed applications, such as: state synchronization service, cluster management.
  • Eureka: The main function is to do service registration and discovery.
  • Consul: It mainly provides service registration, service discovery and configuration management functions for distributed and service-oriented systems.
  • Nacos: It is a dynamic service discovery, configuration management and service management platform that is easy to build cloud-native applications.

2.3 Introduction to Nacos

nacos is committed to discovering, configuring, and managing microservices, and quickly realizes dynamic service discovery service configuration, service metadata, and traffic management.

Core functions :

  • Service registration : Send a REST request to register your own service with Nacos Server
  • Service heartbeat : Nacos Server is maintained through the heartbeat mechanism, indicating that the service is always available to prevent it from being eliminated. By default, a heartbeat is sent every 5s
  • Service synchronization : The clusters will synchronize service instances with each other to ensure the consistency of service information
  • Service discovery : The service consumer obtains the service list registered on Nacos Server, caches it locally in Nacos Client, and starts a scheduled task in Nacos Client to pull the latest registry information of the service and update it to the local cache.
  • Service health check : Start a scheduled task to check the health of the service instance. If the instance has no heartbeat for more than 15s, its healthy property will be set to false, and if the instance has no heartbeat for more than 30s. Remove the instance directly, and re-register if the removed instance responds to the heartbeat

2.4. Getting started with Nacos

2.4.1. Install Nacos

下载地址: https://github.com/alibaba/nacos/releases 
下载zip格式的安装包,然后进行解压缩操作,上课使用的Nacos Server版本是1.3.2

2.4.2. Start Nacos

# 进入bin目录
cd bin
#在cmd中启动
startup.cmd -m standalone

2.4.3. Access to Nacos

Open the browser and enter http://localhost:8848/nacos to access the service. The default password is nacos/nacos

insert image description here

2.5. How to use it in the project

2.5.1. Add Nacos dependency in pom.xml

<!--nacos客户端-->
<dependency>
	<groupId>com.alibaba.cloud</groupId>
	<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

2.5.2. Paste **@EnableDiscoveryClient** annotation on the startup class

@SpringBootApplication
@EnableDiscoveryClient
public class ProductServer {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(ProductServer.class,args);
    }
}

2.5.3. Add Nacos service address in application.yml

spring:
  cloud: 
    nacos: 
      discovery: 
        server-addr: localhost:8848

Remote call load balancing Ribbon

Load balancing : It is to distribute the load to multiple operating units for operation. According to the location of the occurrence, it is divided into server-side load balancing and client-side load balancing .

In microservices, we generally use client-side load balancing, that is, the party calling the service decides which service to provide.

insert image description here

Guess you like

Origin blog.csdn.net/weixin_49137820/article/details/128218016