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

原文地址:http://www.maoyupeng.com/springboot-cloud-register-and-found.html

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

开发环境

操作系统 Mac OS
JDK版本 1.8
Maven 3.0.5
IDE IntelliJ IDEA

项目结构规划

w400
w400

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

名称 描述
mpicloud 项目名称
mpi-admin-provide 例: mpi项目下,管理员模块的提供方
mpi-admin-service 例: mpi项目下,管理员模块的消费者
mpi-client-provide mpi项目下,客户模块的提供方
mpi-client-service mpi项目下,客户模块的消费者
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 里面,则显示出了与父模块一样的引用依赖.

最后的项目结构

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

w400
w400

Spring-boot服务的注册与发现demo(二):http://www.maoyupeng.com/springboot-cloud-register-and-found-2.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容