【尚筹网IDEA版】11-前台-环境配置(gradle项目)

代码下载

【我的尚筹网项目代码】

-----------------------------------------------------------------------------以⬆️均是基于Maven构建--------------------------------------------------------------
-----------------------------------------------------------------------------以⬇️均是基于Gradle构建-----------------------------------------------------------

基本环境

  • 系统:Ubuntu2020

  • jdk: 1.8

  • idea: 2020.2.3

  • gradle: 6.7.1

代码下载

尚筹网会员系统gradle项目框架及环境配置

前言

基于先前Gradle构建的SpringBoot聚合项目基础:Gradle-SpringCloud聚合项目配置,在尚筹网项目会员系统练习中均采用Gradle统一管理jar包。博客不包含所有文件信息,需要可自行下载,获取所需文件。

尚筹网会员系统总目标

👣环境搭建 --> 会员登录注册 --> 发起众筹项目 --> 展示众筹项目 --> 支持众筹项目 --> 订单、支付

架构图

模块列表(🐘Gradle项目)

  1. 父工程、聚合工程:atcrowdfunding07-member-parent

  2. 注册中心:atcrowdfunding08-member-eureka

  3. 实体类模块:atcrowdfunding09-member-entity

  4. MySQL数据服务:atcrowdfunding10-member-mysql-provider

  5. Redis数据服务:atcrowdfunding11-member-redis-provider

  6. 会员中心:atcrowdfunding12-member-authentication-consumer

  7. 项目维护:atcrowdfunding13-member-project-consumer

  8. 订单维护:atcrowdfunding14-member-order-consumer

  9. 支付功能:atcrowdfunding15-member-pay-consumer

  10. 网关:atcrowdfunding16-member-zuul

  11. API接口模块:atcrowdfunding17-member-api

atcrowdfunding07-member-parent模块

配置整个工程的依赖环境版本。

创建dependencyDefine.gradle

参考:Gradle在Java多module项目的应用

由于部分jar包在SpringBoot、SpringCloud的依赖管理中没有指定,例如MyBatis Spring Boot StarterDruid,所以需要自己在子模块中手动添加。为了方便,可以在父项目根目录下创建dependencyDefine.gradle文件,统一的规定这些jar包的版本。在父工程build.gradle文件的合适位置处用apply from: 'dependencyDefine.gradle'引入该文件。注意:${springCloudVersion}${myBatisSpringBootStarterVersion}…等语句的使用需要在引入该文件之后。

/** * 依赖包的定义。 * 这种定义方式的优点是在顶级项目目录下引入,在子项目中也可以直接用了。 * */// jar包的版本号ext{//定义一个变量,统一规定springboot的版本    springCloudVersion = 'Greenwich.SR2'    // Mybatis与SpringBoot整合的版本    myBatisSpringBootStarterVersion = '2.1.0'    // 数据库连接池版本    druidVersion = '1.0.5'}

父类build.gradle

// 在这里声明pluginplugins {// 引入 api 传递依赖的方法 用来代替compile    //  apply plugin: 'java-library'之后才生效    id 'java-library'    // Spring Boot’s dependency management can be used in a project without applying Spring Boot’s plugin to that project.    // First, configure the project to depend on the Spring Boot plugin but do not apply it:    id 'org.springframework.boot' version '2.1.6.RELEASE' apply false    id 'io.spring.dependency-management' version '1.0.10.RELEASE'    // mybatis逆向工程需要的插件    id "com.thinkimi.gradle.MybatisGenerator" version "2.2"}//ext { // 自定义扩展 字段 这里定义版本信息//    //定义一个变量,统一规定springboot的版本//    springCloudVersion = 'Greenwich.SR2'//    // Mybatis与SpringBoot整合的版本//    myBatisSpringBootStarterVersion = '2.1.0'//    // 数据库连接池版本//    druidVersion = '1.0.5'////    // To customize a managed version you set its corresponding property.//    // For example, to customize the version of SLF4J which is controlled by the slf4j.version property://    // ext['slf4j.version'] = '1.7.20'//    // 可以自己修改jar包的版本,但是不建议,都交给Spring管理即可,自己设定版本可能会出错//}repositories {// 配置资源地址//    maven { url 'file:/opt/maven/maven_localRepository' } // 这是我的maven仓库地址    maven { url "http://maven.aliyun.com/nexus/content/groups/public/" } // 阿里仓库    jcenter()    mavenCentral()}// 引入jar包定义// 这个文件中定义的jar包可以直接在子项目中使用,不需要在子模块中指定jar包版本apply from: 'dependencyDefine.gradle'// 子项目的通用配置subprojects {// 在这里使用plugin    apply plugin: 'java' // java是Gradle的核心插件,是内置的,内置插件不需要配置依赖路径    apply plugin: 'idea' // 同上  让Gradle自动生成Intellij的项目文件     apply plugin: 'org.springframework.boot' // 注意gradle插件不再自动应用,所以这里需要指定     /* 依赖管理插件仍然是一个spring-boot-gradle-plugin传递依赖,所以无需在build.gradle配置中明确列出此依赖。 */    apply plugin: 'io.spring.dependency-management' /* 依赖管理,用来传递spring的依赖 */    apply plugin: 'java-library' // 可以使用api编译    // A project can be configured to build both an executable archive and a normal archive at the same time by enabling the jar or war task:    // 可以参考:https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/htmlsingle/    // 默认bootJar和bootWar is true,jar和war 是false    // springboot打出的bootJar包  一般是 不能被依赖的    // 可以参考:https://www.cnblogs.com/karlMa/p/11304524.html    // 打出来的jar包可以子模块间相互依赖,但经过个人测试后发现不可以被执行    // 如果不开启的话,后续是会出现模块间找不到依赖的问题!    jar.enabled = true    group = 'com.yuanbaoqiang'    version = '0.0.1-SNAPSHOT' /* 项目版本 */    /* 指定jdk版本 */    sourceCompatibility = '1.8'    /* java编译的时候缺省状态下会因为中文字符而失败 */    [compileJava, compileTestJava, javadoc]*.options*.encoding = 'UTF-8'    repositories {// 配置资源地址//        maven { url 'file:/opt/maven/maven_localRepository' } // 这是我的maven仓库地址        maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }        jcenter()        mavenCentral()    }    // 此时只是一个依赖管理,并不会导入任何的jar包    // 子项目需要的时候才会真正导入    dependencyManagement {imports {//spring bom helps us to declare dependencies without specifying version numbers.            ///**            // * The coordinates {@code (group:name:version)} of the            // * {@code spring-boot-dependencies} bom.            // */            //public static final String BOM_COORDINATES = "org.springframework.boot:spring-boot-dependencies:"            //  SPRING_BOOT_VERSION;            mavenBom (org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)            mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"            mavenBom "org.mybatis.spring.boot:mybatis-spring-boot-starter:${myBatisSpringBootStarterVersion}"        }    }}

父类setting.gradle

rootProject.name = 'atcrowdfunding07-member-parent'include 'atcrowdfunding08-member-eureka'include 'atcrowdfunding09-member-entity'include 'atcrowdfunding10-member-mysql-provider'include 'atcrowdfunding11-member-redis-provider'include 'atcrowdfunding12-member-authentication-consumer'include 'atcrowdfunding13-member-project-consumer'include 'atcrowdfunding14-member-order-consumer'include 'atcrowdfunding15-member-pay-consumer'include 'atcrowdfunding16-member-zuul'include 'atcrowdfunding17-member-api'// 将其他项目中的模块添加至该项目中,方便其子模块进行依赖include 'atcrowdfunding05-common-util'project(':atcrowdfunding05-common-util').projectDir =file('/home/yuanbaoqiang/Documents/ideaWorkSpace/atcrowdfunding01-admin-parent/atcrowdfunding05-common-util')

相关约定

包名约定

com.yuanbaoqiang.crowd为父级package

主启动类类名约定

  • atcrowdfunding08-member-eureka: EurekaCrowdMainClass

  • atcrowdfunding10-member-mysql-provider: MySQLProviderCrowdMainClass

  • atcrowdfunding11-member-redis-provider: RedisProviderCrowdMainClass

  • atcrowdfunding12-member-authentication-consumer: AuthenticationConsumerCrowdMainClass

  • atcrowdfunding16-member-zuul: ZuulCrowdMainClass

端口号约定(ubuntu环境)

1024以下的端口号,非root用户无法访问!

The TCP/IP port numbers below 1024 are special in that normal users are not allowed to run servers on them. This is a security feaure, in that if you connect to a service on one of these ports you can be fairly sure that you have the real thing, and not a fake which some hacker has put up for you. The normal port number for W3 servers is port 80. This number has been assigned to WWW by the Internet Assigned Numbers Authority, IANA. When you run a server as a test from a non-priviliged account, you will normally test it on other ports, such as 2784, 5000, 8001 or 8080.

  • 1025 --> atcrowdfunding08-member-eureka

  • 1026 --> atcrowdfunding10-member-mysql-provider

  • 1027 --> atcrowdfunding11-member-redis-provider

  • 1028 --> atcrowdfunding12-member-authentication-consumer

  • 1029 --> atcrowdfunding13-member-project-consumer

  • 1030 --> atcrowdfunding14-member-order-consumer

  • 1031 --> atcrowdfunding15-member-pay-consumer

  • 1032 --> atcrowdfunding16-member-zuul

atcrowdfunding08-member-eureka模块

子模块build.gradle

archivesBaseName = 'atcrowdfunding08-member-eureka'bootJar {archiveClassifier = 'boot'}dependencies {implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'}

子模块主启动类EurekaCrowdMainClass

注意此时的主启动类需要加:@EnableEurekaServer

@EnableEurekaServer@SpringBootApplicationpublic class EurekaCrowdMainClass {public static void main(String[] args) {SpringApplication.run(EurekaCrowdMainClass.class, args);    }}

子模块配置文件application.yml

server:  port: 1025spring:  application:    name: atyuanbaoqiang-com.yuanbaoqiang.crowd-eurekaeureka:  instance:    hostname: localhost            # 配置当前Eureka服务的主机地址  client:    register-with-eureka: false    # 自己就是注册中心,所以自己不注册自己    fetch-registry: false          # 自己就是注册中心,所以不需要从注册中取回信息    service-url:                   # 客户端(指consumer和provider)访问Eureka所使用的地址      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

atcrowdfunding09-member-entity模块

该模块主要存放项目中的实体类信息,主要分为以下实体类对象

VO

View Object:视图对象

用途1:接收浏览器发送过来的数据

用途2:把数据发送给浏览器去显示

PO

Persist Object:持久化对象

用途1:将数据封装到PO对象存入数据库

用途2:将数据查询出来存入到PO对象

所以PO对象是数据库表对应,一个数据库表对应一个PO对象

DO

Data Object:数据对象

用途1:从Redis拆线呢得到数据封装到DO对象

用途2:从ElasticSearch查询得到数据封装到DO对象

用途3:从Solr查询得到数据封装到DO对象

从中间件或者其他第三方接口查询到的数据封装为DO对象

DTO

Data Transfer Object数据传输对象

用途1:从Consumer发送数据到Provider

用途2:Provider返回数据给Consumer

gradle逆向工程

参考先前的博客记录:gradle逆向工程

最后生成的四个文件最后放置对应的模块内部:

  • com.yuanbaoqiang.crowd.entity.po.MemberPO

  • com.yuanbaoqiang.crowd.entity.po.MemberPOExample

  • com.yuanbaoqiang.crowd.mapper.MemberPOMapper

  • com.yuanbaoqiang.crowd.mapper.MemberPOMapper

子模块build.gradle

/*******************gradle逆向工程所需*******************/// 详细信息可见:https://blog.csdn.net/qyb19970829/article/details/111055162//引入 mybatis-generator 插件apply plugin: "com.thinkimi.gradle.MybatisGenerator"mybatisGenerator {configFile = 'src/main/resources/generatorConfig.xml'}/*****************************************************//*****************************************************/archivesBaseName = 'atcrowdfunding09-member-entity'bootJar.enabled = falsedependencies{// 此时lombok需要传递    compileOnly 'org.projectlombok:lombok'}/*****************************************************/

atcrowdfunding10-member-mysql-provider模块

注意: 依赖中使用了先前maven项目中模块,需要先将之前的maven项目转换成gradle项目,gradle与maven项目互转可以参考:idea中Maven项目和Gradle项目互转

子模块build.gradle

archivesBaseName = 'atcrowdfunding10-member-mysql-provider'bootJar {archiveClassifier = 'boot'}dependencies {// springboot中的mybatis    implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter'    // mysql驱动    implementation 'mysql:mysql-connector-java'    // 数据库连接池    implementation "com.alibaba:druid:${druidVersion}"    // springboot测试    testImplementation 'org.springframework.boot:spring-boot-starter-test'    // 对外暴露服务    implementation 'org.springframework.boot:spring-boot-starter-web'    // 作为客户端访问Eureka注册中心    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'    // 为了可以使用实体类    implementation project(':atcrowdfunding09-member-entity')    // 为了可以使用工具类    implementation project(":atcrowdfunding05-common-util")}

子模块主启动类MySQLProviderCrowdMainClass

注意需要开启包扫描

@MapperScan("com.yuanbaoqiang.crowd.mapper")@SpringBootApplicationpublic class MySQLProviderCrowdMainClass {public static void main(String[] args) {SpringApplication.run(MySQLProviderCrowdMainClass.class, args);    }}

子模块配置文件application.yml

server:  port: 1026spring:  application:    name: atyuanbaoqiang-com.yuanbaoqiang.crowd-mysql  datasource:    name: mydb    type: com.alibaba.druid.pool.DruidDataSource    url: jdbc:mysql://127.0.0.1:3306/project_crowd?serverTimezone=UTC    username: root    password: qyb19970829    driver-class-name: com.mysql.cj.jdbc.Drivereureka:  client:    service-url:      defaultZone: http://localhost:1025/eurekalogging:  level:    com.yuanbaoqiang.crowd.mapper: debug    com.yuanbaoqiang.crowd.test: debugmybatis:  mapper-locations: classpath*:/mybatis/mapper/*Mapper.xml

atcrowdfunding11-member-redis-provider模块

子模块build.gradle

archivesBaseName = 'atcrowdfunding11-member-redis-provider'bootJar {archiveClassifier = 'boot'}dependencies {// 整合redis    implementation 'org.springframework.boot:spring-boot-starter-data-redis'    // 为了可以使用工具类    implementation project(":atcrowdfunding05-common-util")    // 为了可以使用实体类    implementation project(':atcrowdfunding09-member-entity')    // 作为客户端访问Eureka注册中心    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'    // 对外暴露    implementation 'org.springframework.boot:spring-boot-starter-web'    // 测试    testImplementation 'org.springframework.boot:spring-boot-starter-test'}

子模块启动类RedisProviderCrowdMainClass

@SpringBootApplicationpublic class RedisProviderCrowdMainClass {public static void main(String[] args) {SpringApplication.run(RedisProviderCrowdMainClass.class, args);    }}

子模块配置文件application.yml

server:  port: 1027spring:  application:    name: atyuanbaoqiang-crowd-redis  redis:    host: 192.168.205.128eureka:  client:    service-url:      defaultZone: http://localhost:1025/eureka

atcrowdfunding12-member-authentication-consumer模块

子模块build.gradle

archivesBaseName = 'atcrowdfunding12-member-authentication-consumer'bootJar {archiveClassifier = 'boot'}dependencies {implementation 'org.springframework.boot:spring-boot-starter-web'    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'    implementation project(":atcrowdfunding17-member-api")}

子模块启动类AuthenticationConsumerCrowdMainClass

// @EnableDiscoveryClient // 这个注解看具体的版本,当前版本可以不写@SpringBootApplicationpublic class AuthenticationConsumerCrowdMainClass {public static void main(String[] args) {SpringApplication.run(AuthenticationConsumerCrowdMainClass.class, args);    }}

子模块配置文件application.yml

server:  port: 1028spring:  application:    name: atyuanbaoqiang-crowd-auth  thymeleaf:    prefix: classpath:/templates/    suffix: .htmleureka:  client:    service-url:      defaultZone: http://localhost:1025/eureka

atcrowdfunding16-member-zuul模块

子模块build.gradle

archivesBaseName = 'atcrowdfunding16-member-zuul'bootJar {archiveClassifier = 'boot'}dependencies {implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-zuul'}

主启动类ZuulCrowdMainClass

注意开启代理

@EnableZuulProxy@SpringBootApplicationpublic class ZuulCrowdMainClass {public static void main(String[] args) {SpringApplication.run(ZuulCrowdMainClass.class, args);    }}

子模块配置文件application.yml

server:  port: 1032spring:  application:    name: atyuanbaoqiang-crowd-zuuleureka:  client:    service-url:      defaultZone: http://localhost:1025/eurekazuul:  ignored-services: "*"  sensitive-headers: "*"  routes:    crowd-portal:      service-id: atyuanbaoqiang-crowd-auth      path: /**

atcrowdfunding17-member-api模块

子模块build.gradle

archivesBaseName = 'atcrowdfunding17-member-api'bootJar.enabled = falsedependencies {implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'    // 依赖需要被传递,所以使用api    api project(":atcrowdfunding05-common-util")    api project(":atcrowdfunding09-member-entity")}client:    service-url:      defaultZone: http://localhost:1025/eurekazuul:  ignored-services: "*"  sensitive-headers: "*"  routes:    crowd-portal:      service-id: atyuanbaoqiang-crowd-auth      path: /**

atcrowdfunding17-member-api模块

子模块build.gradle

archivesBaseName = 'atcrowdfunding17-member-api'bootJar.enabled = falsedependencies {implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'    // 依赖需要被传递,所以使用api    api project(":atcrowdfunding05-common-util")    api project(":atcrowdfunding09-member-entity")}

来源:https://www.icode9.com/content-4-783201.html

(0)

相关推荐