• idea gradle spring cloud 微服务项目


    这里是以最简单的spring cloud 微服务项目搭建,具体的内容自行增加。

    整体结构图

    在这里插入图片描述

    gradle版本号控制(可修改)

    在这里插入图片描述

    settings.gradle

    rootProject.name = 'gradlecloud'//主工程项目
    include 'common'//子工程公共类
    include 'gateway'//网关
    include 'services'//子服务
    
    • 1
    • 2
    • 3
    • 4

    gradlecloud的build.gradle

    buildscript {//构建gradle脚本自身需要的资源,可以声明的资源包括依赖项、第三方插件、maven仓库地址等
        ext{
            //统一版本管理
            lombokVersion = '1.18.20'//lombok版本
            springBootVersion = '2.3.12.RELEASE'//spring boot 版本
            springCloudVersion = 'Hoxton.SR12'//spring cloud 版本
            springCloudAlibabaVersion = '2.2.7.RELEASE'//spring cloud alibaba 版本
            springCloudStarterBootstrapVersion = '3.1.2'//bootstrap.yml版本
            commonsLangVersion = '2.5'//StringUtils工具类 commons-lang版本
            mysqlVersion = '8.0.23'//mysql版本
            mybatisSpringBootVersion = '2.1.0'//mybatis版本
            pagehelperVersion = '1.2.13'//分页插件pageHelper版本
            japidocsVersion = '1.4.4'//生成离线接口文档JApiDocs版本
        }
    
        //设置仓库
        repositories {
            //从前到后顺序执行,找不到就往后找。
            mavenLocal()//本地仓库
            maven { url 'https://maven.aliyun.com/repository/public' }//镜像仓库
            mavenCentral()//官方仓库
        }
    
        dependencies {
            //spring-boot-gradle插件,方便版本管理
            classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        }
    }
    
    //全局配置,包括root和其子项目
    allprojects {
        apply plugin: 'java'
        group 'com.fu'//项目组
        version '1'//项目版本
        sourceCompatibility = 1.8//java版本
        targetCompatibility = 1.8//java版本
    
        tasks.withType(JavaCompile){
            options.encoding = "UTF-8"
        }
    
        repositories {
            //从前到后顺序执行,找不到就往后找。
            mavenLocal()//本地仓库
            maven { url 'https://maven.aliyun.com/repository/public' }//镜像仓库
            mavenCentral()//官方仓库
        }
    }
    
    //配置所有子项目
    subprojects {
        apply plugin: 'io.spring.dependency-management'//版本管理插件
    
        //dependencyManagement版本统一管理,类似maven的dependencyManagement
        dependencyManagement{
            dependencies {
                //统一版本管理
                dependency "org.projectlombok:lombok:${lombokVersion}"
                dependency "org.springframework.cloud:spring-cloud-starter-bootstrap:${springCloudStarterBootstrapVersion}"
                dependency "commons-lang:commons-lang:${commonsLangVersion}"
                dependency "mysql:mysql-connector-java:${mysqlVersion}"
                dependency "org.mybatis.spring.boot:mybatis-spring-boot-starter:${mybatisSpringBootVersion}"
                dependency "com.github.pagehelper:pagehelper-spring-boot-starter:${pagehelperVersion}"
                dependency "io.github.yedaxia:japidocs:${japidocsVersion}"
            }
            imports {
                mavenBom "org.springframework.boot:spring-boot-dependencies:${springBootVersion}"//Spring Boot
                mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"//Spring Cloud
                mavenBom "com.alibaba.cloud:spring-cloud-alibaba-dependencies:${springCloudAlibabaVersion}"//Spring Cloud Alibaba
            }
        }
    }
    
    • 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
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72

    common

    公共类可以存放公共entity、dao、mapper、util等
    在这里插入图片描述
    common的build.gradle

    apply plugin: 'java'
    group 'com.fu'
    version '1'
    dependencies {
        annotationProcessor 'org.projectlombok:lombok'//注释处理器
        implementation 'org.projectlombok:lombok'//引入lombok依赖
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    gateway

    网关(filter和redis都不是必须的,注册与服务发现中心我用的是nacos,这里我就不演示了,不然太长了)
    在这里插入图片描述
    gateway的build.gradle

    apply plugin: 'org.springframework.boot'
    dependencies {
        implementation project(':common')
        implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap'
        implementation 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-config'
        implementation 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery'
        implementation 'org.springframework.boot:spring-boot-starter-webflux'
        implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
        implementation 'org.springframework.cloud:spring-cloud-starter-loadbalancer'
        implementation 'org.springframework.boot:spring-boot-starter-data-redis'
    }
    bootJar {//修改打成jar包以后的名字
        baseName('gateway')//自定义的jar包名
        version(null)//版本号
        mainClassName = 'com.fu.gateway.GatewayApplication'
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    services

    在这里插入图片描述
    services的build.gradle(像mysql、mybatis我都注释掉了,开放注解即可使用,这里还是以入门为主)

    apply plugin: 'org.springframework.boot'
    dependencies {
        implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap'
        implementation 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-config'
        implementation 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery'
        implementation 'org.springframework.boot:spring-boot-starter-web'
    //    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    //    implementation 'org.springframework.boot:spring-boot-starter-validation'
    //    implementation 'mysql:mysql-connector-java'
    //    implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter'
    //    implementation 'com.github.pagehelper:pagehelper-spring-boot-starter'
    }
    bootJar {//修改打成jar包以后的名字
        baseName('services')//自定义的jar包名
        version(null)//版本号
        mainClassName = 'com.fu.services.ServicesApplication'
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    logbcak日志输出(建议配置)

    logback-spring.xml

    <configuration scanPeriod="60 seconds">
        <!-- 动态日志级别 -->
        <jmxConfigurator />
        <!-- 定义日志文件 输出位置 -->
        <property name="log_dir" value="./logs/XX项目/" />
    
        <!-- 控制台输出日志 -->
        <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>[%d{yyyy-MM-dd HH:mm:ss}] [%thread] [%magenta(%level)] %green([%logger]) >>> %cyan(%msg) %n</pattern>
            </encoder>
        </appender>
    
        <!-- 日志输出到文件 -->
        <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>INFO</level>
            </filter>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>${log_dir}/log-%d{yyyy-MM-dd}.log</fileNamePattern>
                <!-- 日志保留最大天数 -->
                <maxHistory>15</maxHistory>
                <!-- 重启项目后删除日志 -->
                <cleanHistoryOnStart>true</cleanHistoryOnStart>
            </rollingPolicy>
            <encoder>
                <pattern>[%d{yyyy-MM-dd HH:mm:ss}] [%thread] [%level] [%logger] >>> %msg %n</pattern>
            </encoder>
        </appender>
        <logger name="demo" level="INFO"/>
        <!-- root级别 DEBUG -->
        <root level="INFO">
            <!-- 控制台输出 -->
            <appender-ref ref="console" />
            <!-- 文件输出 -->
            <appender-ref ref="FILE" />
        </root>
    </configuration>
    
    • 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
  • 相关阅读:
    硬件测试(一):温循
    笔试强训48天——day12
    C++类和对象(上)
    大文件上传时如何做到 秒传?
    3A通过PMP考试有多大比例?
    粒子群算法及其Python实现
    使用Apache Flink实现实时数据同步与清洗:MySQL和Oracle到目标MySQL的ETL流程
    论文解读(NWR)《Graph Auto-Encoder via Neighborhood Wasserstein Reconstruction》
    Kotlin(十) 空指针检查、字符串内嵌表达式以及函数默认值
    Go解密之路——GPM
  • 原文地址:https://blog.csdn.net/weixin_43933728/article/details/125635933