学习了《Spring Cloud与Docker微服务架构实战》这本书,想要记录一下学习历程,同时把总结的经验以及自己补充的知识记录下来,于是就有了这篇文章。
创建一个Spring Cloud项目
本文及后续的内容使用Spring Cloud的版本为Edgware.SR6,Spring Boot的版本为1.5.x,因为在Edgware之后的版本,Spring Boot也升级为2.x版本的了。为了保证能够顺利进行书上的练习,因为选用了Edgware最新的版本。
创建一个父项目
首先创建一个父项目micro-service-spring-cloud-study,用于管理其他子项目依赖的版本,注意父项目的打包方式为pom,其中标签内的是即将创建的生产者和消费者子项目,子项目从父项目中获取依赖的jar包及属性信息。
dependencies和dependencyManagement的说明
- 如果dependencies里的dependency自己没有声明version元素,那么maven就会到dependencyManagement里面去找有没有对该artifactId和groupId进行过版本声明,如果有,就继承它,如果没有就会报错,告诉你必须为dependency声明一个version。
- 如果dependencies中的dependency声明了version,那么无论dependencyManagement中有无对该jar的version声明,都以dependency里的version为准。
以下为父pom文件pom.xml
<?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.fanfan.cloud</groupId>
<artifactId>micro-service-spring-cloud-study</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>micro-service-provider-user</module>
<module>micro-service-consumer-movie</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<!--子项目再次引入此依赖jar包时则无需显式的列出版本号-->
<!--dependencyManagement中定义的只是依赖的声明,并不实现引入,
因此子项目需要显式的声明需要用的依赖。-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!--所有声明在dependencies里的依赖都会自动引入,并默认被所有的子项目继承。-->
<dependencies>
<!--配置处理器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--单元测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--用于监控和管理端点-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--logback日志-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<scope>compile</scope>
<version>1.1.11</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>compile</scope>
<version>1.1.11</version>
<