# 087. 基于 spring boot 快速构建缓存服务以及商品服务

本章搭建项目:

  • eshop-cache-ha:缓存服务,端口 7001
  • eshop-product-ha:商品服务,端口 7000

两个服务都是建立在父模块下的子模块,项目源码参考 https://github.com/zq99299/cache-pdp.git

以下配置重新搞过一次,之前的老是依赖有问题,时不时的就编译不了

最终项目目录布局

搭建一个空项目,能连接到数据库的配置,两个项目都是一样的除了端口号不一样

公共配置 build.gradle

allprojects {
    group = 'cn.mrcode.cachepdp'
    version = '0.0.1-SNAPSHOT'

    repositories {
        mavenLocal()
        maven { url 'https://repo.spring.io/libs-snapshot' }
        maven { url "http://maven.aliyun.com/nexus/content/groups/public" }
        maven { url "https://maven.repository.redhat.com/ga/" }
        maven { url "http://maven.nuiton.org/nexus/content/groups/releases/" }
        maven { url "https://repository.cloudera.com/artifactory/cloudera-repos/" }
        mavenCentral()
    }
}


subprojects { p ->
    apply plugin: 'java'
    apply plugin: 'idea'
    sourceCompatibility = '1.8'

    //跳过所有文件的编译测试;不是跳过compileTestJava task 而是在执行该task的时候,跳过所有的测试文件
    test {
        exclude '**/*.class'
    }

    // 这里一定得要。在多模块下,不然编译失败,
    // bootJar 默认会关闭 jar 任务
    jar {
        enabled = true
    }

    //指定编译的编码
    tasks.withType(JavaCompile) {
        options.encoding = "UTF-8"
    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

公共配置 settings.gradle

pluginManagement {
    repositories {
        gradlePluginPortal()
    }
}
rootProject.name = 'cache-pdp'
// 这个写在之前的父项目下了,所以还有其他的子项目
include 'eshop-inventory'
include 'eshop-cache'
include 'storm-helloword'
include 'eshop-storm'
include 'eshop-cache-ha'
include 'eshop-product-ha'

1
2
3
4
5
6
7
8
9
10
11
12
13
14

eshop-product-ha/build.gradle

plugins {
    id 'org.springframework.boot' version '2.1.5.RELEASE'
}

apply plugin: 'io.spring.dependency-management'
dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework.boot:spring-boot-starter-jdbc'
    compile 'org.springframework.boot:spring-boot-starter-actuator'
    compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
    compile 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.1'
    runtimeOnly 'mysql:mysql-connector-java:5.1.34'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    compile 'com.alibaba:fastjson:1.1.43'
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

eshop-product-ha application.yml

server:
  port: 7000
logging:
  level:
    root: info
    # 可以打印 sql
    cn.mrcode.cachepdp.eshop.product.ha: info
    org.springframework.web: TRACE
#  path: ./
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
#    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://192.168.99.173:3306/eshop?useUnicode=yes&characterEncoding=UTF-8&useSSL=false
    username: eshop
    password: eshop
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8

mybatis:
#  type-aliases-package: cn.mrcode.cachepdp.eshop.product.ha.model
  mapper-locations: classpath*:mapper/*.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

项目启动后,可访问到 http://localhost:7000/getUserInfo 即为成功