Spring Cloud开发实践(一): 简介和根模块

目录

简介

使用Spring Boot的提升主要在于jar的打包形式给运维带来了很大的便利, 而Spring Cloud本身的优点不是那么明显, 相对于Dubbo而言, 可能体现在跨语言的交互性上(例如可以配合PHP作为前端模块), 还有现成的服务跟踪治理工具上. 对于熟悉Dubbo项目结构的开发来说, Spring Cloud的结构会相对松散, 上游的服务接口与下游的服务调用之间没有强依赖关系. 这样在上游接口调整后, 并不能及时被IDE发现而提醒下游, 会给团队配合带来一些不便. 这个系列通过代码实例介绍Spring Cloud开发中的一些有益的实践.

项目结构

项目的最简化结构如下

├── scot
│   ├── pom.xml
├── scot-commons-api
│   ├── pom.xml
│   └── src
├── scot-commons-impl
│   ├── pom.xml
│   └── src
├── scot-commons-lib
│   ├── pom.xml
│   └── src
├── scot-eureka
│   ├── pom.xml
│   └── src
├── scot-web
    ├── pom.xml
    └── src

各模块的功能:

  • scot: 项目的根pom, 用于管理依赖Dependency 和 构建工具Plugin的版本
  • scot-eureka: EurekaServer 服务
  • scot-commons-lib: 项目的公用工具类
  • scot-commons-api: 服务的接口, dto和api定义, 上游服务需要实现这些api, 下游调用需要引入这个包的dto和api定义
  • scot-commons-impl: 服务api的接口实现.
  • scot-web: 对外提供访问的模块, 依赖于上游commons提供的接口

根模块 Scot: 版本控制

Scot是一个简单的pom模块, 内容如下

<?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.rockbb</groupId>
    <artifactId>scot</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <name>Scot: Root</name>
    <modules>
        <module>../scot-eureka</module>
        <module>../scot-commons-lib</module>
        <module>../scot-commons-api</module>
        <module>../scot-commons-impl</module>
        <module>../scot-web</module>
    </modules>

    <prerequisites>
        <maven>3.3.9</maven>
    </prerequisites>

    <properties>
        <!-- Global encoding -->
        <project.jdk.version>1.8</project.jdk.version>
        <project.source.encoding>UTF-8</project.source.encoding>

        <!-- Global dependency versions -->
        <springframework.boot.version>2.0.6.RELEASE</springframework.boot.version>
        <springframework.cloud.version>Finchley.RELEASE</springframework.cloud.version>

        <mode.development>1</mode.development>
        <timestamp>${maven.build.timestamp}</timestamp>
        <maven.build.timestamp.format>yyyyMMdd_HHmm</maven.build.timestamp.format>
        <logback.appender>stdout</logback.appender>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- Spring Dependencies Start -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${springframework.cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${springframework.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <version>${springframework.boot.version}</version>
                <scope>test</scope>
            </dependency>
            <!-- Spring Dependencies End -->
        </dependencies>
    </dependencyManagement>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                    <configuration>
                        <source>${project.jdk.version}</source>
                        <target>${project.jdk.version}</target>
                        <encoding>${project.source.encoding}</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.1.0</version>
                    <configuration>
                        <encoding>${project.source.encoding}</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>${springframework.boot.version}</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

在Maven中使用 <parent> 来整体控制项目中的依赖版本是最常见的方式, 大部分中小型项目都可以使用. 因为这边不使用spring的parent而是项目自己的parent, 所以在<dependencyManagement>中增加了spring-cloud-dependencies 和 spring-boot-dependencies, 其 scope=import.

对于一些模块间互相并无层级关系, 结构较松散的项目, 可以在各模块的pom中通过添加<dependencyManagement> + scope=import 来实现同样的效果.

<dependencyManagement>
    <dependencies>
        <dependency>
            <!-- Import dependency management -->
            <groupId>com.rockbb</groupId>
            <artifactId>version-control-pom</artifactId>
            <version>1.2-SNAPSHOT</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

posted on 2018-10-25 22:25  Milton  阅读(581)  评论(0编辑  收藏  举报

导航