Coding construction of springcloud microservice architecture

People in Huajian Temple 2021-05-04 16:49:45
coding construction springcloud microservice architecture


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

image-20210504161621174

Microservice module

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

1. Parent project space construction

image-20210503150526178
image-20210503205756270
image-20210503205819548
image-20210503150957709
image-20210503210510371
image-20210503211155830

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 )

image-20210503212217252

2. Payment module construction

2.1 On

image-20210503212629485
image-20210503214024060 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

image-20210504103512447 image-20210504103306582 image-20210504103330018 image-20210504103655962
image-20210504103817420 image-20210504103408044 image-20210504103734923 image-20210504103832382

3. Consumer order module

image-20210504104453627
image-20210504110126517
Reference producers
image-20210504162102263

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

image-20210504111810424
image-20210504113130436
image-20210504113248420
image-20210504113454822
image-20210504113731321
image-20210504113843852

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)

image-20210504114330860
image-20210504114444152
image-20210504115201879 then install
Delete the respective entitiesimage-20210504115509636
image-20210504120436023
<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

image-20210504125536148

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

image-20210504134234816

image-20210504131103730
image-20210504132645504
image-20210504132603288
image-20210504133416895
image-20210504133340459
image-20210504133842802

7.Eureka Cluster environment construction

image-20210504134537496
image-20210504135753258
image-20210504135917059
image-20210504140112225
image-20210504140149620
image-20210504142231112
image-20210504150512508
image-20210504150433118

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

image-20210504161210987

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 .

image-20210504161249441

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 .

image-20210504161405533

版权声明
本文为[People in Huajian Temple]所创,转载请带上原文链接,感谢
https://javamana.com/2021/05/20210504164408240y.html

  1. Realization of reactor Kafka through spring boot Webflux
  2. RPC框架设计----Socket与I/0模型
  3. Problems in upgrading from Java 8 to Java 11
  4. RPC framework design -- socket and I / 0 model
  5. RPC框架设计----I/0模型
  6. RPC framework design: I / 0 model
  7. RPC框架设计----NIO编程缓冲区Buffer
  8. RPC框架设计----NIO编程缓冲区Buffer
  9. RPC framework design -- NiO programming buffer
  10. RPC framework design -- NiO programming buffer
  11. Java多线程基础
  12. Java multithreading Foundation
  13. 码农飞升记-00-Java发展历程
  14. Development history of coder-00-java
  15. 码农飞升记-00-Java发展历程
  16. Development history of coder-00-java
  17. Spring and Autumn Moon
  18. Node.js与Spring Boot比较? - Ryan Gleason
  19. Spring WebFlux的明显陷阱 - ŁukaszKyć
  20. Spring创始人Rod大叔对YAML的真实想法
  21. Compare node.js with spring boot- Ryan Gleason
  22. Obvious pitfalls of spring Webflux- Ł ukaszKy ć
  23. Spring founder uncle rod's real thoughts on yaml
  24. 码农飞升记-02-OracleJDK是什么?OracleJDK的版本怎么选择?
  25. What is manong feisheng-02-oracle JDK? How to choose the version of Oracle JDK?
  26. Spring tide surging Xinanjiang
  27. Linux内核软中断
  28. Linux kernel soft interrupt
  29. Linux内核软中断
  30. Linux kernel soft interrupt
  31. Java multithreading Foundation
  32. The construction of Maven private library nexus
  33. I / O stream in Java
  34. JDK 16:Java 16的新功能 - InfoWorld
  35. 在Java中本地进行线程间数据传输的三种方式和源码展示
  36. jdon导致cpu 99%最后tomcat死掉---banq给予回复
  37. 用领域事件模拟AOP注入
  38. JDK 16: new function of Java 16 - InfoWorld
  39. Cartoon: from JVM lock to redis distributed lock
  40. Spring 3.1 终于加入了Cache支持
  41. Prototype与JQuery对比
  42. Three ways of data transmission between threads in Java and source code display
  43. Jdon causes 99% of CPU and Tomcat dies -- banq replies
  44. docker 原理之 user namespace(下)
  45. Simulating AOP injection with domain events
  46. Spring 3.1 finally adds cache support
  47. Comparison between prototype and jquery
  48. User namespace of docker principle (2)
  49. The way to learn java IO stream and XML
  50. Why does a seemingly correct code cause the Dubbo thread pool to be full
  51. 0 基础 Java 自学之路(2021年最新版)
  52. 0 basic Java self study road (latest version in 2021)
  53. c#—基础拾遗(1) 面向对象
  54. C - basic information (1) object oriented
  55. 技术分享|SQL和 NoSQL数据库之间的差异:MySQL(VS)MongoDB
  56. Technology sharing differences between SQL and NoSQL databases: MySQL (VS) mongodb
  57. PHP教程/面向对象-3~构造函数和析构函数
  58. Spring Cloud的Feign客户端入门
  59. 优化Spring Boot应用的Docker打包速度
  60. PHP tutorial / object oriented - 3 ~ constructor and destructor