您的位置:首页 > 编程语言 > Java开发

Spring boot服务的注册与发现demo(一)

2017-03-23 11:27 543 查看
原文地址:http://www.maoyupeng.com/springboot-cloud-register-and-found.html

简单演示使用idea搭建Spring boot模块化项目结构搭建及Spring boot服务发现与注册 (该文章为作者笔记,只供参考,不要学习)

开发环境

操作系统Mac OS
JDK版本1.8
Maven3.0.5
IDEIntelliJ IDEA

项目结构规划



项目结构规划

假设我们这个项目有客户模块与管理员模块. 则项目结构划分为如上图:

名称描述
mpicloud项目名称
mpi-admin-provide例: mpi项目下,管理员模块的提供方
mpi-admin-service例: mpi项目下,管理员模块的消费者
mpi-client-providempi项目下,客户模块的提供方
mpi-client-servicempi项目下,客户模块的消费者
mpi-cloud注册中心 (通俗的说法)

新建父模块

步骤截图











配置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.maoyupeng.test</groupId>
<artifactId>mpicloud</artifactId>
<version>1.0-SNAPSHOT</version>

</project>


修改后

<?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.maoyupeng.test</groupId>
<artifactId>mpicloud</artifactId>
<version>${mpicloid.version}</version>

<!--修改打包方式为pom-->
<packaging>pom</packaging>
<!--项目名称-->
<name>mpicloud</name>
<!--项目描述-->
<description>mpicloud for spring-boot</description>

<!--
子模块引入
-->
<modules>

</modules>

<!--
定义属性变量
使用方法例如: <version>${mpicloid.version}</version>
这样version里面的值为0.0.1-SNAPSHOT
-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>

<!--main version-->
<mpicloid.version>0.0.1-SNAPSHOT</mpicloid.version>
<!--cloud服务jar版本-->
<mpi-cloud.version>0.0.1-SNAPSHOT</mpi-cloud.version>
<!--管理员服务jar版本-->
<mpi-admin-service.version>0.0.1-SNAPSHOT</mpi-admin-service.version>
<!--客户端服务jar版本-->
<mpi-client-service.version>0.0.1-SNAPSHOT</mpi-client-service.version>
<!--管理员提供方jar版本-->
<mpi-admin-provide.version>0.0.1-SNAPSHOT</mpi-admin-provide.version>
<!--客户端提供方jar版本-->
<mpi-client-provide.version>0.0.1-SNAPSHOT</mpi-client-provide.version>
</properties>

<!--引入spring boot 核心包-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<!--引入依赖包-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-mockmvc</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<!--插件-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<!--指定spring镜像仓库,有的时候依赖包会报错,如果引入了该仓库,就会解决问题-->
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

</project>


新建子模块

步骤截图











修改子模块(mpi-client-service)的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"> <parent>
<artifactId>mpicloud</artifactId>
<groupId>com.maoyupeng.test</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>mpi-client-service</artifactId>

</project>


修改后

<?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>mpicloud</artifactId>
<groupId>com.maoyupeng.test</groupId>
<!--修改: 引用父模块中的properties, 引入成功,则配置生效-->
<version>${mpicloid.version}</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>mpi-client-service</artifactId>

</project>


验证



由上图可知,在mpi-client-service(pom.xml)里面没有引入任何的依赖包,但是在Maven Dependencies 里面,则显示出了与父模块一样的引用依赖.

最后的项目结构

其他子模块的新建步骤雷同,所以省略…



Spring-boot服务的注册与发现demo(二):http://www.maoyupeng.com/springboot-cloud-register-and-found-2.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring