第4章 创建工程
4.1 新建工程
在IDEA中新建工程,并使用SpringInitializr来创建:
点击下一步,输入项目的GAV坐标及工程构造方式后点击下一步:
点击下一步,选择主版本和模块,这里以Spring2.x版本为例,引入JDBC和对接Redis及Kafka的Starter。
之后点击下一步后确认项目存放位置,点击finish完成。
4.2 引入其他依赖
由于工程需要从多个数据源读取数据,这里引入baomidou组织的动态数据源切换依赖。
同时引入mysql,clickhouse,phoenix,spring-data-elasticsearch的驱动及相关依赖。pom.xml中引入的依赖如下:
<dependencies> <!-- springmvc模块 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 整合Mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.3.0</version> </dependency> <!-- springdata 访问 es --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> <version>4.0.5.RELEASE</version> </dependency> <!-- springdata 访问 redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- spring 集成 kafka --> <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> </dependency> <!-- clickhosue驱动 --> <dependency> <groupId>ru.yandex.clickhouse</groupId> <artifactId>clickhouse-jdbc</artifactId> <version>0.1.55</version> </dependency> <!-- mysql8 驱动 --> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!-- 动态数据源切换 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>3.3.2</version> <scope>compile</scope> </dependency> <!-- fastjson 构造数据格式 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.69</version> </dependency> <!-- phoenix 读取hbase --> <dependency> <groupId>org.apache.phoenix</groupId> <artifactId>phoenix-core</artifactId> <version>5.0.0-HBase-2.0</version> </dependency> <!-- phoenix 读取hbase 相关依赖 --> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>3.1.3</version> </dependency> <!-- phoenix 读取hbase 相关依赖 --> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>27.1-jre</version> <scope>compile</scope> </dependency> </dependencies>
4.3 添加配置
添加项目中所需要的数据源连接信息及框架配置如下,其中红色部分需要替换为自己的配置。
dest: logUrl: http://localhost:8080/app redisDataKey: hotphone param: logStr topic: userLog mybatis: configuration: mapUnderscoreToCamelCase: true spring: kafka: bootstrap-servers: hadoop102:9092 elasticsearch: uris: http://hadoop102:9200 redis: host: hadoop102 port: 6379 datasource: dynamic: primary: mysql strict: false datasource: mysql: url: jdbc:mysql://hadoop102:3306/gmall_report?serverTimezone=Asia/Shanghai&useSSL=false&useUnicode=true&characterEncoding=UTF-8 username: root password: “000000” driver-class-name: com.mysql.cj.jdbc.Driver ck: url: jdbc:clickhouse://hadoop102:8123/default username: password: driver-class-name: ru.yandex.clickhouse.ClickHouseDriver hbase: url: jdbc:phoenix:hadoop102,hadoop103,hadoop104:2181 username: password: driver-class-name: org.apache.phoenix.jdbc.PhoenixDriver