Teach you how to build a SpringCloud project (1) Detailed graphic explanation

What are microservices? You’ll know the series at a glance!

1. Teach you step by step how to build a Spring Cloud project (1) Detailed explanation with pictures and text, fool-proof operation

2. Teach you step by step how to build a SpringCloud project (2) Producers and consumers

3. Teach you step by step how to build a SpringCloud project (3) Integrate the Eureka service registration center

4. Teach you step by step how to build a SpringCloud project (4) Building the Eureka cluster version

5. Teach you step by step how to build a Spring Cloud project (5) Build the producer cluster version

6. Teach you step by step how to build a SpringCloud project (6) Eureka implements service discovery

7. Teach you step by step how to build a Spring Cloud project (7) Integrate the Consul service registration center

8. Teach you step by step how to build a Spring Cloud project (8) Integrate Ribbon load balancer

9. Teach you step by step how to build a Spring Cloud project (9) Integrate OpenFeign service interface call

10. Teach you step by step how to build a Spring Cloud project (10) Integrating Hystrix service downgrade

11. Teach you step by step how to build a Spring Cloud project (11) Integrating Hystrix service circuit breaker

12. Teach you step by step how to build a Spring Cloud project (12) Integrate Hystrix for graphical Dashboard real-time monitoring

13. Teach you step by step how to build a SpringCloud project (13) Integrate Gateway new generation gateway

14. Teach you step by step how to build a SpringCloud project (14) Integrate Config distributed configuration center

15. Teach you step by step how to build a Spring Cloud project (15) Integrate Bus message bus

16. Teach you step by step how to build a Spring Cloud project (16) Integrate Stream message driver

17. Teach you step by step how to build a SpringCloud project (17) Integrating Sleuth distributed link tracking

Continuing to update, please like and follow!

What is Spring Cloud?

Spring Cloud is an ordered collection of a series of frameworks. It uses the development convenience of Spring Boot to simplify the development of distributed systems, such as service discovery, service gateway, service routing, link tracking, etc. Spring Cloud does not reinvent the wheel, but integrates and encapsulates well-developed modules on the market, thus reducing the development cost of each module. In other words: Spring Cloud provides the "family bucket" needed to build distributed systems.

Spring Cloud status quo

At present, there are not many companies using Spring Cloud technology in China. It is not because Spring Cloud is not good. The main reasons are as follows:

  1. Spring Cloud has few Chinese documents, and there are not many solutions to problems on the Internet.
  2. Most of the technical bosses of domestic entrepreneurial companies are employees of the Ali department, and the Ali department mostly uses Dubbo to build the microservice architecture.
  3. Large companies basically have their own distributed solutions, while many small and medium-sized companies do not use microservices in their architectures, so there is no need to use Spring Cloud.

However, microservice architecture is a trend, and Spring Cloud is the leader in microservice solutions. This is also the significance of the author writing this series of courses.

Spring Cloud advantages and disadvantages

Its main advantages are:

集大成者,Spring Cloud 包含了微服务架构的方方面面。
约定优于配置,基于注解,没有配置文件。
轻量级组件,Spring Cloud 整合的组件大多比较轻量级,且都是各自领域的佼佼者。
开发简便,Spring Cloud 对各个组件进行了大量的封装,从而简化了开发。
开发灵活,Spring Cloud 的组件都是解耦的,开发人员可以灵活按需选择组件。

Its disadvantages:

项目结构复杂,每一个组件或者每一个服务都需要创建一个项目。
部署门槛高,项目部署需要配合 Docker 等容器技术进行集群部署,而要想深入了解 Docker,学习成本高。

The advantages of Spring Cloud are obvious. Therefore, for students who want to study microservice architecture, learning Spring Cloud is a good choice.

Spring Cloud project construction

Reminder: You must have a certain springboot foundation!

In the last article, we learned about microservices. Understanding it is not enough. The most important thing is to practice it, because practice is the only criterion for testing truth. Only when both theory and practice are mastered can we really learn it. We know that microservices have multiple services, put different businesses into different services, and then call each other between the services. So we need to create a new general project (parent project) to manage other microservice projects below. The whole process is built using maven project, which is the same as springboot building. Let’s start building it!

Project construction diagram:
Insert image description here

We first build the parent project, here we use the maven project to build.

Insert image description here

Fill in gav, fill in according to your own habits, and then click Next
Insert image description here

The following interface appears, click Finish, and the new project is completed.
Insert image description here

Next, let's configure it first. First, we configure the POM.XML file.

<?xml version="1.0" encoding="UTF-8"?>
 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>com.study.springcloud</groupId>
  <artifactId>mcroservice</artifactId>
  <!-- 首先修改打包方式 -->
  <packaging>pom</packaging>
  <version>1.0-SNAPSHOT</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>
    <mysql.version>5.1.47</mysql.version>
    <druid.version>1.1.10</druid.version>
    <junit.version>4.1.2</junit.version>
    <lombok.version>1.16.10</lombok.version>
    <log4j.vsrsion>1.2.17</log4j.vsrsion>
  </properties>
  <!--  因为是总项目 所以用dependencyManagement来管理  因为其他的子项目就不会来管理版本了了 可以直接引用 -->
  <dependencyManagement>
    <dependencies>
 
      <!-- springcloud的依赖-->
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Greenwich.SR1</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <!-- springboot的依赖-->
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.1.4.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>
      <!-- springboot启动器-->
      <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.2</version>
      </dependency>
      <!--单元测试 -->
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
      </dependency>
      <!-- lombok-->
      <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.8</version>
      </dependency>
 
      <!-- log4j-->
      <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>${log4j.vsrsion}</version>
      </dependency>
      <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>1.2.3</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
 
</project>

Because our parent project does not write business, we can delete the src folder and set some useless files not to be displayed or delete them. At this point we have completed building the parent project, it’s that simple.

Next, we can continue to build other microservice projects. We first build a service specifically for our entity class so that other services can call it.

Select our parent project and click New, then click Module, as shown below:

Insert image description here

This is the same as the parent project.
Insert image description here

Fill in the project name of our Module.

Insert image description here

Note that in the following step, the - in our project name is omitted in the Module name, because there are more projects in the actual project. In order to see it more clearly, we need to add - to split it! Then we click Finish.

Insert image description here

At this point our first microservice project has been established, and the next step is configuration. We first configure the POM.xml file. Here we introduce the Hutool jar package, which is a basic Java tool class that encapsulates JDK methods such as files, streams, encryption and decryption, transcoding, regularization, threads, XML, etc., to form various Util tool classes, and also provides the following components :

布隆过滤
缓存
克隆接口
类型转换
日期处理
数据库ORM(基于ActiveRecord思想)
基于DFA有限自动机的多个关键字查找
HTTP客户端
IO和文件
有用的一些数据结构
日志
反射代理类的简化(AOP切面实现)
Setting(一种扩展Properties的配置文件)
System(JVM和系统信息等)
WatchService的封装(文件变动监控)
XXXUtil各种有用的工具类

It is also very convenient to use, making it an artifact in our java development.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>mcroservice</artifactId>
        <groupId>com.study.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
 
    <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>
 
 
</project>

This is the pojo class we can write in the service. We take payment as an example here, so we first create a payment table in the data. The sql statement is as follows

CREATE TABLE `payment` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `serial` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

We add data to the table and the sql statement is as follows:

insert  into `payment`(`id`,`serial`) values (1,'百度'),(2,'alibaba'),(3,'京东'),(4,'头条');

After creating the table and adding data, we will create a new corresponding entity class. The picture below shows the project structure diagram:

Insert image description here

The entity class corresponding to the payment table. Lombok is used here, and the above pom file imports dependencies, but the lombok plug-in needs to be installed, otherwise an error will be reported! As shown below:

Insert image description here

package com.buba.springcloud.pojo;
 
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
 
import java.io.Serializable;
 
//网络通信 一定要实现序列化
//使用lombok  没有配置的童鞋要去百度查一下  jar我们导入了  需要在idea装一个插件就可以了
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class Payment implements Serializable {
    
    
    private Long id;
    // 微服务 一个服务对应一个数据库,同一个信息可能存在不同的数据库
    private  String serial;
}

For the convenience of data transmission and the separation of front-end and back-end projects, we encapsulate the returned data into an entity class.

package com.buba.springcloud.pojo;
 
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
 
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class CommonResult<T> {
    
    
    private Integer code;//返回状态码
    private String  message;//返回是否调用成功
    private  T data; //返回的数据
 
    public CommonResult(Integer code, String message) {
    
    
        this(code,message,null);
    }
}

Our entity class service mainly stores entity classes and does not write business logic. We can also delete the src folder. At this point we have successfully built the general project and the microservice project that provides entity classes. Because the service project of the entity class needs to be called by other service projects, the services of the entity class must be packaged and put into a common local library.

We first clean the project to ensure that the maven configuration of the current project is successful.

Insert image description here

The following interface appears, indicating that there is no problem with the maven configuration environment of the current project.

Insert image description here

Then we will start to install and package it into the local library. The following is the successful interface:

Insert image description here

At this point we can see that the pom.xml file of our general project has introduced cloud-api-commons, as shown below:

Insert image description here

Our next article will build a microservice project for producers and consumers. The article is being updated continuously, please like and follow!

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_39570655/article/details/131760874