SpringCloud

Spring Cloud It provides developers with tools to quickly build some common patterns in distributed systems ( For example, configuration management , Service discovery , Circuit breaker , Intelligent routing , The micro broker , Control bus ). The coordination of distributed systems leads to the template pattern , Use Spring Cloud Developers can quickly support services and applications that implement these patterns . They will work well in any distributed environment , Including developers' own laptops , Bare metal data center , as well as Cloud Foundry Wait for the hosting platform

Official website

Common help documents

1

2

3

4

5

6

Microservice module

  1. Build module
  2. Change pom
  3. Write YML
  4. Main start
  5. Business class

1. Parent project space construction

1.2 The parent project pom

<!-- Unified management jar Package version -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.12</junit.version>
<log4j.version>1.2.17</log4j.version>
<lombok.version>1.16.18</lombok.version>
<mysql.version>5.1.47</mysql.version>
<druid.version>1.1.16</druid.version>
<mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
</properties> <!-- After sub module inheritance , Provide a role : Lock version + Son modlue Do not write groupId and version -->
<dependencyManagement>
<dependencies>
<!--spring boot 2.2.2-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud Hoxton.SR1-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud alibaba 2.1.0.RELEASE-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.spring.boot.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<optional>true</optional>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>

1.3 Maven Medium DependencyManagement and Dependencies

dependencyManagement

Maven Use dependencyManagement Element to provide a way to manage dependent version numbers .

Usually at the top of an organization or project POM see dependencyManagement Elements .

Use pom.xml Medium dependencyManagement Element enables all subprojects to refer to a dependency without explicitly listing the version number .

Maven Will go up the father and son level , Until you find someone who has dependencyManagement Elements of the project , And then it will use this

dependencyManagement The version number specified in the element .

The advantage of doing so is : If multiple subprojects refer to the same dependency , You can avoid declaring a version number in each used subproject , So when you want to upgrade or switch to another version , Just update in the top-level parent container , It doesn't need to be modified one by one ; In addition, if a subproject needs another version , Just declare version Can be .

  • dependencyManagement It's just a statement of dependency , No introduction , So the child project needs to display the required dependencies for the declaration .
  •  If you don't declare dependencies in subprojects , Is not inherited from the parent project ; Only the dependency is written in the subproject , And no specific version was specified ,
    Will inherit the item from the parent project , also version and scope Read from parent pom;
  •  If the version number is specified in the subproject , Then you will use the... Specified in the subproject jar edition .

1.4maven Skip unit tests in

1 To configure

<build><!-- maven Skip unit tests in -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>

2 IDEA Tool support ( recommend )

2. Payment module construction

2.1 On

Create database

2.1.1 Change the sub module's pom

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!--mysql-connector-java-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

2.1.2 Write submodules of yml

server:
port: 8001 spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource # Current data source operation type
driver-class-name: org.gjt.mm.mysql.Driver # mysql Drive pack com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/cloud2021?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: Zwf51162139. mybatis:
mapperLocations: classpath:mapper/*.xml
type-aliases-package: com.wan.springcloud.entities # all Entity Package of alias class

2.1.3 Main startup class

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

2.1.4 Business class

2.1.5 test

3. Consumer order module

Reference producers

3.1.3 Main startup class

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

3.1.5 test

4. Engineering refactoring

1. There are repetitions in the system , Need to refactor

2. newly build cloud-api-commons

3. Change pom

<artifactId>cloud-api-commons</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>

4. Copy the duplicate part entitie

5.maven command clean , install

6. Order 80 And payment 8001 Reform separately ( Delete entities, Paste pom)

then install
Delete the respective entities
<dependency><!-- Introduce your own definition of api General package , have access to Payment payment Entity -->
<groupId>com.wan.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>

5.Eureka Basic knowledge of

5.1 What is service governance

 Spring Cloud Encapsulates the Netflix company-developed Eureka Module to implement service governance 
In traditional rpc In the remote call framework , Managing the dependencies between each service is complex , Management is more complicated , So we need to use service governance , Managing service to service dependencies , Service calls can be implemented 、 Load balancing 、 Fault tolerance, etc , Implement service discovery and registration .

5.2 What is service registration and discovery

Eureka Adopted CS Design framework ,Eureka Server A server that serves as a service registration function , It is a service registry . And other microservices in the system , Use Eureka The client connects to Eureka Server And maintain the heartbeat connection . In this way, the maintenance personnel of the system can pass Eureka Server To monitor the normal operation of each micro service in the system .

In service registration and discovery , There's a registry . When the server starts , Will put the current information of their own servers such as Service address, mailing address, etc. are registered in the registration center by alias . The other side ( consumer | Service providers ), Use this alias to get the actual service address in the registry , And then local RPC call RPC The core design idea of remote call framework : It's in the registry , Because the registry is used to manage a dependency between each service ( The concept of service governance ). In any rpc In the remote framework , There will be a registry ( Store service address information ( Address of the interface ))

The next picture on the left is Eureka System architecture , The picture on the right is Dubbo The architecture of , Please compare

5.3 Eureka There are two components :Eureka Server and Eureka Client

Eureka Server Provide service registration service

After each microservice node is started through configuration , Will be in EurekaServer Register in , such EurekaServer The service registry in will store information about all available service nodes , The information of the service node can be directly seen in the interface .

EurekaClient Access through the registry

It's a Java client , For simplification Eureka Server Interaction , The client also has a built-in 、 Use polling (round-robin) Load balancer of load algorithm . After the app starts , Will Eureka Server Send a heartbeat ( The default period is 30 second ). If Eureka Server The heartbeat of a node is not received in multiple heartbeat cycles ,EurekaServer This service node will be removed from the service registry ( Default 90 second )

6.IDEA Generate eurekaServer End services registry

7.Eureka Cluster environment construction

8.Eureka Self protection

summary

Protection mode is mainly used for a group of clients and Eureka Server There is protection in the network partition scenario . Once in protected mode ,

Eureka Server Will try to protect the information in its service registry , Data in the service registry is no longer deleted , That is, no microservices are logged off .

If in Eureka Server See the following tip on the homepage of , shows Eureka In protected mode :

EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT.
RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE

Why does it produce Eureka Self-protection mechanism ?

In order to prevent EurekaClient It works , however And EurekaServer When the network is not working ,EurekaServer Not immediately EurekaClient Service to eliminate

What is self-protection mode ?

By default , If EurekaServer The heartbeat of a microservice instance is not received for a certain period of time ,EurekaServer The instance will be deregistered ( Default 90 second ). But when the network partition fails ( Time delay 、 Carton 、 crowded ) when , Micro service and EurekaServer Unable to communicate between , The above behaviors may become very dangerous —— Because microservices are actually healthy , This is not the time to log off the micro service .Eureka adopt “ Self-protection model ” To solve this problem —— When EurekaServer When a node loses too many clients in a short time ( A network partition failure may have occurred ), Then this node will go into self-protection mode .

In self-protection mode ,Eureka Server Protects information in the service registry , No more service instances are logged off .

Its design philosophy is that it would rather keep the wrong service registration information , And don't blindly log off any possible healthy service instances . In a word : Better to live than to die

Sum up , Self protection mode is a kind of security protection measures to deal with network abnormalities . Its architectural philosophy is to retain all micro services at the same time ( Healthy microservices and unhealthy microservices will keep ) And don't blindly log off any healthy microservices . Use self-protection mode , It can make Eureka Clusters are more robust 、 Stable .

SpringCloud- More articles on the coding and construction of microservice architecture

  1. Java Fresh e-commerce platform -SpringCloud High concurrency parameter optimization of microservice architecture

    Java Fresh e-commerce platform -SpringCloud High concurrency parameter optimization of microservice architecture One . Write it at the front stay Java I believe many friends in the fresh e-commerce platform use it in their own company Spring Cloud Framework to build microservice Architecture , After all, this is ...

  2. Java Fresh e-commerce platform -SpringCloud The core points and implementation principles of microservice architecture

    Java Fresh e-commerce platform -SpringCloud The core points and implementation principles of microservice architecture explain :Java Fresh e-commerce platform , We will further understand the core points and implementation principles of microservice architecture , Design pattern of providing micro service for readers' practice , In order to make micro service ...

  3. Microservice architecture : structure PHP Micro service ecology

    Microservice architecture : structure PHP Micro service ecology   Linux System technology exchange QQ Group (1675603) Verify the answer to the question : Liu Chuang Reading guide Born in 2014 Year of “ Microservice architecture ”, His thought is through Martin Fowler After elaboration , In recent ...

  4. Java Fresh e-commerce platform -SpringCloud Distributed transaction solution in microservice Architecture

    Java Fresh e-commerce platform -SpringCloud Distributed transaction solution in microservice Architecture explain :Java Fresh e-commerce platform because of the use of micro service architecture for business processing , buyers , The seller , Delivery , sales , The service of suppliers , But it's inevitable ...

  5. Java Fresh e-commerce platform -SpringCloud Network request performance optimization and source code analysis in microservice architecture

    Java Fresh e-commerce platform -SpringCloud Network request performance optimization and source code analysis in microservice architecture explain :Java Fresh e-commerce platform , Because of the service split , Many business services lead to network delay and performance consumption of requests , Corresponding to these problems , We ...

  6. SpringCloud Microservice architecture and SOA framework

    1, The traditional three-tier architecture In the traditional architecture ,SSH,SSM, It is mainly divided into web Control layer , Business logic layer , Database access layer , Single point projects , The project is not split , All the development tasks are written in one project , Coupling is higher than price , If a function in the program appears ...

  7. Rest Building distributed SpringCloud Microservice architecture project

    One . development environment :jdk  1.8.Maven  3.x.IDEA  2019.1.4.SpringBoot   2.0.7.spring Cloud  The latest stable version   Finchley SR2   collocation ...

  8. springcloud Micro service architecture construction

    SpringCloud Micro service framework construction One . Microservice architecture 1.1 What is distributed Different modules are deployed on different servers effect : Distributed solution to the problem of high concurrency website 1.2 What is cluster Multiple servers deploy the same application to form a cluster effect : through ...

  9. About SpringCloud A little understanding of the concept of microservice Architecture

    At present, microservice is a very popular architecture or concept , It's also the architecture used in building large-scale Internet projects . 1. Monomer architecture monomer Architecture , It means to mark the developed project as war package , And then publish it to tomcat And so on . Suppose you're going to develop a product with Ub ...

  10. SpringCloud How to share distributed components in microservice architecture session object

    One . Make a brief background note 1. To illustrate , An example of a simple microservice architecture is as follows 2. Component description distributed architecture , Each component is a cluster or a master / standby . The details are as follows :zuul service: gateway ,API Call all go zuul service ...

Random recommendation

  1. css3 Many of them

                                             

  2. Simulation game 1101d2

    Lucky Numbers (number)Time Limit:1000ms Memory Limit:64MB Title Description LYK I've had bad luck recently , For example, in NOIP In the preliminaries, only 90 branch , Just got into the second round , So it decided to use some ...

  3. C++STL Its iterator 2

    I'm learning c++ STL When , Iterators all day , I use it all day , however , What the hell is it , A lot of people don't know . Here I go through a couple of small DEMO, Look at iterators . First of all, I realized a very simple vector class : templat ...

  4. iOS Development —— Practical Swift piece &amp; Common practical techniques for project development

    Common practical techniques for project development Make calls To make a phone call , The simplest and most direct way is : Go straight to the dialing interface ( Be careful : This needs real debugging , The simulator doesn't work )     UIApplication.sharedApplica ...

  5. iOS Develop a hundred questions (6)

    61. Warning "addexplicit braces to avoid dangling else" So-called " Critical else" It's similar to this kind of code : if(a== 10) ...

  6. python concurrent.futures

    python Because of its global interpreter lock GIL We can't achieve real parallel computing through threads . We don't start with this conclusion , But there's a concept that we want to explain ,IO intensive vs. Computationally intensive . IO intensive : Read the file , Read network socket frequently . Computationally intensive ...

  7. Highly available Spring FTP Upload and download tool class ( Common problems in the upload process have been solved )

    Preface Recently in the project need and ftp The server interacts , I found out about on the Internet ftp Tools for uploading and downloading , There are roughly two . The first is the class of singleton pattern . The second is to define another Service, Directly through Service To achieve ftp Above ...

  8. OAuth2 Certification and authorization :ResourceOwnerPassword authentication

    ResourceOwnerPassword stay ClientCredentials A new user name and password are added to the authentication But through RequestPasswordTokenAsync Can't get refresh_token, No ...

  9. 【BZOJ1823】【JSOI2010】 All the seats of man and Han

    Almost forgot 2-sat…… The original title is : Manchu and Han banquet is the most abundant banquet dish in China , There are many kinds of 不 The same material 料 Through Manchu or Han people 料理 The way , Presented in 數量 Among all kinds of dishes . Because of the variety of dishes , Only a few 數 Knowledgeable and skilled chefs can make Manchu and Han chefs ...

  10. SVD(6.5.1 Theorem proving observation 3)