结合之前学习的技术从零开始搭建一个微服务项目

首先需要创建一个空的maven工程

导入标准的父工程pom依赖配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?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>org.cloud</groupId>
<artifactId>cloud-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 第一步 -->
<packaging>pom</packaging>

<!-- 统一管理 jar 包版本 -->
<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>8.0.21</mysql.version>
<druid.version>1.1.16</druid.version>
<mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
</properties>

<!-- 子块基础之后,提供作用:锁定版本 + 子module不用写 groupId 和 version -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</dependency>

<!-- 下面三个基本是微服务架构的标配 -->
<!--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 阿里巴巴-->
<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>

<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
<scope>runtime</scope>
</dependency>
<!-- druid-->
<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>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<!--log4j-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</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>
</project>

maven中的 dependencyManagement 和 dependencies 的区别?

1.dependencyManagement
一般出现在最顶层父工程的pom文件中,我们会看到dependencyManagement元素。通过它来管理jar包的版本,让子项目中引用一个依赖而不用显示的列出版本号。Maven会沿着父子层次向上走,直到找到一个拥有dependencyManagement元素的项目,然后它就会使用在这个dependencyManagement元素中指定的版本号。【注意】dependencyManagement 声明的依赖并没有被导入项目

  1. dependencies
    管理项目中所有需要导入的依赖
    dependencyManagement里只是声明依赖,并不实现引入,因此子项目需要显示的声明需要用的依赖。如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom;另外如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。

关于SpringBoot、SpringCloud、SpringCloudAlibaba版本选择

可以根据网站:https://start.spring.io/actuator/info来选择

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{
git: {
branch: "f43bda3cdd3be489aa8ce207fb10c6360aeefdd5",
commit: {
id: "f43bda3",
time: "2021-01-20T10:00:44Z"
}
},
build: {
version: "0.0.1-SNAPSHOT",
artifact: "start-site",
versions: {
spring-boot: "2.4.2",
initializr: "0.10.0-SNAPSHOT"
},
name: "start.spring.io website",
time: "2021-01-20T10:04:03.142Z",
group: "io.spring.start"
},
bom-ranges: {
azure: {
2.2.4: "Spring Boot >=2.2.0.RELEASE and <2.3.0.M1", 3.0.0: "Spring Boot >=2.3.0.M1 and <2.4.0-M1" }, codecentric-spring-boot-admin: { 2.2.4: "Spring Boot >=2.2.0.RELEASE and <2.3.0.M1", 2.3.1: "Spring Boot >=2.3.0.M1 and <2.5.0-M1" }, solace-spring-boot: { 1.0.0: "Spring Boot >=2.2.0.RELEASE and <2.3.0.M1", 1.1.0: "Spring Boot >=2.3.0.M1 and <2.5.0-M1" }, solace-spring-cloud: { 1.0.0: "Spring Boot >=2.2.0.RELEASE and <2.3.0.M1", 1.1.1: "Spring Boot >=2.3.0.M1 and <2.5.0-M1" }, spring-cloud: { Hoxton.SR9: "Spring Boot >=2.2.0.RELEASE and <2.3.9.BUILD-SNAPSHOT", Hoxton.BUILD-SNAPSHOT: "Spring Boot >=2.3.9.BUILD-SNAPSHOT and <2.4.0.M1", 2020.0.0-M3: "Spring Boot >=2.4.0.M1 and <=2.4.0.M1", 2020.0.0-M4: "Spring Boot >=2.4.0.M2 and <=2.4.0-M3", 2020.0.0: "Spring Boot >=2.4.0.M4 and <2.4.3-SNAPSHOT", 2020.0.1-SNAPSHOT: "Spring Boot >=2.4.3-SNAPSHOT"
},
spring-cloud-alibaba: {
2.2.1.RELEASE: "Spring Boot >=2.2.0.RELEASE and <2.3.0.M1" }, spring-cloud-gcp: { 2.0.0: "Spring Boot >=2.4.0-M1 and <2.5.0-M1" }, spring-cloud-services: { 2.2.6.RELEASE: "Spring Boot >=2.2.0.RELEASE and <2.3.0.RELEASE", 2.3.0.RELEASE: "Spring Boot >=2.3.0.RELEASE and <2.4.0-M1" }, spring-geode: { 1.2.12.RELEASE: "Spring Boot >=2.2.0.RELEASE and <2.3.0.M1", 1.3.8.RELEASE: "Spring Boot >=2.3.0.M1 and <2.4.0-M1", 1.4.1: "Spring Boot >=2.4.0-M1 and <2.5.0-M1" }, vaadin: { 14.4.6: "Spring Boot >=2.1.0.RELEASE and <2.5.0-M1" }, wavefront: { 2.0.2: "Spring Boot >=2.1.0.RELEASE and <2.4.0-M1", 2.1.0-RC1: "Spring Boot >=2.4.0-M1 and <2.4.3-SNAPSHOT", 2.1.0-SNAPSHOT: "Spring Boot >=2.4.3-SNAPSHOT"
}
},
dependency-ranges: {
okta: {
1.4.0: "Spring Boot >=2.2.0.RELEASE and <2.4.0-M1", 1.5.1: "Spring Boot >=2.4.0-M1 and <2.4.1", 2.0.0: "Spring Boot >=2.4.1 and <2.5.0-M1" }, mybatis: { 2.1.4: "Spring Boot >=2.1.0.RELEASE and <2.5.0-M1" }, camel: { 3.3.0: "Spring Boot >=2.2.0.RELEASE and <2.3.0.M1", 3.5.0: "Spring Boot >=2.3.0.M1 and <2.4.0-M1", 3.7.0: "Spring Boot >=2.4.0.M1 and <2.5.0-M1" }, open-service-broker: { 3.1.1.RELEASE: "Spring Boot >=2.2.0.RELEASE and <2.4.0-M1"
}
}
}

搭建微服务的消费者,服务者

1
2
3
4
5
6
@FeignClient(name = "userservice")
public interface UserFeigns {

@GetMapping("/user/{id}")
public User findById(@PathVariable("id") Long id);
}