• 新建项目

    新建maven项目

    创建子模块

    添加pom依赖

    父级pom

    这里添加一些公用的pom属性,类似私有仓库等等。

    <!--加parent-->
    <parent>
    <groupId>com.hand.hap.cloud</groupId>
    <artifactId>hap-cloud-parent</artifactId>
    <version>1.0.0-Release</version>
    </parent>
    <!--添加公司私有仓库-->
    <repositories>
    <repository>
        <id>hand-snapshot-repository</id>
        <name>Hand Snapshot Repository</name>
        <url>http://nexus.saas.hand-china.com/content/repositories/rdcsnapshot/</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>hand-release-repository</id>
        <name>Hand Release Repository</name>
        <url>http://nexus.saas.hand-china.com/content/repositories/rdc/</url>
    </repository>
    </repositories>
    <!--添加dependencyManagement-->
    <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
    </dependencyManagement>
    <build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
    </build>
    

    子级pom

    根据子级模块所需jar包添加需要的依赖。以下介绍一些hap-cloud特有的依赖:

    <dependencies>
        <dependency>
            <groupId>com.hand.hap.cloud</groupId>
            <artifactId>hap-resource-server-starter</artifactId>
            <version>1.0.0-Release</version>
        </dependency>
        <dependency>
            <groupId>com.hand.hap.cloud</groupId>
            <artifactId>hap-swagger-starter</artifactId>
            <version>1.0.0-Release</version>
        </dependency>
        <dependency>
            <groupId>com.hand.hap.cloud</groupId>
            <artifactId>hap-mybatis-mapper-starter</artifactId>
            <version>1.0.0-Release</version>
        </dependency>
        <dependency>
            <groupId>com.hand.hap.cloud</groupId>
            <artifactId>hap-feign-replay-starter</artifactId>
            <version>1.0.0-Release</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- config server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-sleuth-stream</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
        </dependency>
        <!-- test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    

    项目结构

    在子模块resource文件夹中配置

    server:
      port: 8378
    mybatis:
      mapperLocations: classpath*:/mapper/*.xml
      configuration:
        mapUnderscoreToCamelCase: true
    spring:
      application:
        name: hap-todo-service
    eureka:
      instance:
        preferIpAddress: true
        leaseRenewalIntervalInSeconds: 1
        leaseExpirationDurationInSeconds: 3
      client:
        serviceUrl:
          defaultZone: ${EUREKA_DEFAULT_ZONE:http://localhost:8000/eureka/}
    ---
    spring:
      profiles: default
      cloud:
        config:
          enabled: false
    ---
    spring:
      profiles: docker,sit
      cloud:
        config:
          discovery:
            serviceId: hap-config-server
            enabled: true
        failFast: true
        retry:
          maxAttempts: 32
          multiplier: 1.5
          maxInterval: 10000
    
    spring:
        datasource:
            url: jdbc:mysql://localhost/hap_demo_service_todo?useUnicode=true&characterEncoding=utf-8&useSSL=false
            username: hapdemo
            password: handhand
        eureka:
          client:
            serviceUrl:
              defaultZone: http://localhost:8000/eureka
    swagger:
        oauthUrl: http://localhost:8080/oauth/oauth/authorize
    

    结构图