使用Gradle7.6.1 + SpringBoot3.0.2 + java17创建微服务项目(学习)

这是一个大胆的决定

技术栈

技术 版本
spring-boot 3.0.2
spring-cloud 2022.0.2
spring-cloud-alibaba 2022.0.0.0-RC2
mybatis-plus-boot-starter 3.5.3.1
mysql-connector-java 8.0.32

开发工具

技术 版本
java 17
gradle 7.6.1
IDEA 2022.2.4
Nvcat 15
MySQL 8.0.32

一、创建gradle父子项目(deity

1.0 简单流程示意

  • 打开IDEA创建 SpringBoot项目
  • 删除父项目中的src模块
  • 新建两个子项目(新建时会重新创建父项目src文件,删除即可)
    • 子项目:mall-Ares
    • 子项目:mall-angel
  • deity build.gradle 配置全局配置
  • mall-Ares 和 mall-angelbuild.gradle 负责引入依赖的jar

1.1、IDEA中主要图示

1.1.1 项目结构图

在这里插入图片描述

1.1.2 IDEA中 Gradle配置

在这里插入图片描述

1.2、deity父项目build.gradle文件

//所有buildscript{}块都必须出现在脚本中任何插件{}块之前
buildscript{
    
     //构建gradle脚本自身需要的资源,可以声明的资源包括依赖项、第三方插件、maven仓库等等

    //统一依赖版本管理
    ext{
    
    
        mysqlVersion     = "8.0.32"  //mysql
        fastjsonVersion  = "2.0.29" //fastjson
        lombokVersion    = '1.18.26' //lombok
        springBootVersion= '3.0.2' //springBoot
        springCloudVersion= '2022.0.2' //springCloud
        springCloudAlibabaVersion='2022.0.0.0-RC2'//cloudAlibaba 2022.0.0.0-RC2
        mybatisPlusVersion   = '3.5.3.1' //mybatisPlus

    }

    //buildscript脚本构建时需要去寻找的仓库
    repositories {
    
     //从前往后顺序执行,找不到就向后查找
//		mavenLocal() //从maven本地仓库下载jar到gradle仓库
        maven {
    
     url 'https://maven.aliyun.com/repository/public/' }
        maven {
    
     url 'https://maven.aliyun.com/repository/grails-core'}
        maven {
    
     url 'https://maven.aliyun.com/repository/google/' }
        maven {
    
     url 'https://maven.aliyun.com/repository/jcenter/' }
        maven {
    
     url "https://mvn.getui.com/nexus/content/repositories/releases/" }
        maven {
    
    
            url 'http://mirrors.huaweicloud.com/repository/maven/'
            allowInsecureProtocol = true
        }
        mavenCentral()
    }

    //构建用到的插件
    dependencies {
    
    
        classpath 'io.spring.gradle:dependency-management-plugin:1.1.0'//maven方式插件
    }

}

//插件要在最上边初始化
plugins{
    
    
    id 'org.springframework.boot' version '3.0.2'
    id 'io.spring.dependency-management' version '1.1.0'//创建于 2022 年 10 月 18 日。   2023.05.08目前为最新版本
    id 'idea'
}


//针对所有project的配置,包含根项目,除此之外还有subprojects 和 project,感兴趣的童鞋可以自行百度查阅
allprojects{
    
    
    apply plugin: 'java'	//项目是java项目
    apply plugin: 'idea'	//项目是idea项目
    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management' //类似于maven的版本管理方式
    group = 'com.dage'
    version = '0.0.1-SNAPSHOT'
    //jdk版本
    sourceCompatibility = '17'
    targetCompatibility = '17'

    //必须在插入 java插件后 执行该命令
    configurations {
    
    
        compileJava{
    
    
            extendsFrom annotationProcessor
        }
    }

    //所有project项目的 jar获取方式
    tasks.withType(JavaCompile) {
    
    
        options.encoding = "UTF-8"
        options.deprecation = true
    }

    //全局配置时查找依赖的使用
    repositories{
    
     //从前往后顺序执行,找不到就向后查找
        maven {
    
     url 'https://maven.aliyun.com/repository/public/' }
        maven {
    
     url 'https://maven.aliyun.com/repository/grails-core'}
        maven {
    
     url 'https://maven.aliyun.com/repository/google/' }
        maven {
    
     url 'https://maven.aliyun.com/repository/jcenter/' }
        maven {
    
     url "https://mvn.getui.com/nexus/content/repositories/releases/" }
        maven {
    
    
            url 'http://mirrors.huaweicloud.com/repository/maven/'
            allowInsecureProtocol = true
        }
        mavenCentral()
    }
}

subprojects{
    
    // 子项目配置(可以理解为子模块的全局配置)
    sourceCompatibility = '17'
    targetCompatibility = '17'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management' //类似于maven的版本管理方式

    //子model统一jar引入
    dependencies {
    
    
        annotationProcessor 'org.projectlombok:lombok' //注释处理器
        implementation 'org.projectlombok:lombok'//引入lombok依赖
    }

    //提供类似 Maven 的依赖管理和排除的 Gradle 插件
    //使用类似于maven的方式(io.spring.dependency-management) 统一版本管理
    //https://docs.spring.io/dependency-management-plugin/docs/current/reference/html/#pom-generation  学习网址
    //DSL 允许您使用:分隔的字符串来声明依赖管理,以配置托管依赖的坐标
    dependencyManagement {
    
    
        dependencies{
    
    //统一版本管理
            //DSL 允许您使用:分隔的字符串来声明依赖管理,以配置托管依赖的坐标
            dependency "mysql:mysql-connector-java:${mysqlVersion}"
            dependency "org.projectlombok:lombok:${lombokVersion}"
            dependency "com.baomidou:mybatis-plus-boot-starter:${mybatisPlusVersion}"
            dependency "com.baomidou:mybatis-plus-generator:${mybatisPlusVersion}"
            dependency "org.springframework.boot:spring-boot-starter-web:${springBootVersion}"
            dependency "org.apache.velocity:velocity-engine-core:${velocityVersion}"

        }

        imports {
    
    
            //引入 spring-cloud统一包管理
            mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
            //引入 spring-cloud-alibaba统一包管理
            mavenBom "com.alibaba.cloud:spring-cloud-alibaba-dependencies:${springCloudAlibabaVersion}"
            //引入 spring-boot统一包管理
            mavenBom "org.springframework.boot:spring-boot-dependencies:${springBootVersion}"
        }
    }

}


//禁止根项目一切行为(不影响模块)
//禁用以后执行build的时候不会构建parent项目
tasks.forEach {
    
    
    it.enabled = false
}


/**
 * Gradle打标准jar包
 * 根项目需禁用springboot插件,否则会构建失败
 * 光禁用BootJar是不行的,还要启用Jar。
 * GroovyDSL的写法,对KotlinDSL无效
 */
bootJar{
    
    enabled = false}

jar{
    
    enabled=true}
/**    KotlinDSL写法
 * tasks.bootJar {enabled = false}
 * tasks.jar {enabled = true}
 */


tasks.named('test') {
    
    
    useJUnitPlatform()
}

1.3、deity父项目settings.gradle文件

注意这里,子项目是没有settings.gradle文件的。全局只有一个settings.gradle文件

pluginManagement {
    
    
    repositories {
    
    
        //        mavenLocal() //从maven本地仓库下载jar到gradle仓库 【两者】
        maven {
    
     url 'https://maven.aliyun.com/repository/public/' }
        // 新增
        maven {
    
    url 'https://maven.aliyun.com/repository/grails-core'}
        maven {
    
     url 'https://maven.aliyun.com/repository/google/' }
        maven {
    
     url 'https://maven.aliyun.com/repository/jcenter/' }
        maven {
    
     url "https://mvn.getui.com/nexus/content/repositories/releases/" }
        maven {
    
    
            url 'http://mirrors.huaweicloud.com/repository/maven/'
            allowInsecureProtocol = true
        }
        maven {
    
     url 'https://repo.spring.io/milestone' }
        maven {
    
     url 'https://repo.spring.io/snapshot' }
        mavenCentral() //maven中央仓库
        gradlePluginPortal()
    }
}


rootProject.name = 'deity'
include 'mall-Ares'
include 'mall-angel'


此时我们在看 settings.gradle 文件,就会发现,已经自动生成了子模块引入目录

1.4、子项目build.gradle

mall-Ares 和 mall-angel两个子项目

这里注意,子项目只需要这么多配置参数,如果需要其他jar继续在dependencies {}中引入即可。(所有子项目配置均一致)


//子项目需要的jar包
dependencies {
    
    
    //SpringBoot启动项引入
    implementation 'org.springframework.boot:spring-boot-starter-web'

}

// 动态移除对kotlin构建插件的权限,避免子模块构建报错
tasks.register("prepareKotlinBuildScriptModel") {
    
    }

1.5、子项目中的 application.yml 配置

1.5.1、mall-Ares application.yml

server:
  port: 8082

1.5.2、mall-angelapplication.yml

server:
  port: 8083

1.6、测试Controller

1.6.1、mall-Ares测试代码

package com.dage.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("test/")
public class TestController {
    
    

    @RequestMapping("one")
    public String one(){
    
    
        return "欢迎来到java17";
    }
}

1.6.2、mall-angel测试代码

package com.dage.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("test/")
public class TestController {
    
    
    @RequestMapping("one")
    public String one(){
    
    
        return "欢迎来到gradle7.6.1";
    }
}

1.7、测试是否启动

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

二、创建gitee仓库,推送到远端

2.1、登录gitee新建仓库(deity

仓库名称要项目名称一致,这里仓库名称为:deity

在这里插入图片描述

2.1.1. Get the following command (to create a new warehouse, 创建git仓库just use )

简易的命令行入门教程:
Git 全局设置:

git config --global user.name "xxx"
git config --global user.email "[email protected]"
创建 git 仓库:

mkdir gods
cd gods
git init 
touch README.md
git add.
git commit -m "first commit"
git remote add origin https://gitee.com/xxxxxxxx/gods.git
git push -u origin "master"
已有仓库?

cd existing_git_repo
git remote add origin https://gitee.com/xxxxxxx/gods.git
git push -u origin "master"

Here you need to use git, you need to install it yourself, and I won’t elaborate here
. Use git add here to add all files to the temporary storage area

2.2, git execution command

Execute the command in the root directory of the project
Remember xxxxxto replace it with your own address

  1. Open git and enter the project file
  2. git init initialize git
  3. touch README.mdCreate version file
  4. git statusView current workspace file status
  5. git add . Upload all files to the staging area
  6. git commit -m " 提交注释"Submit files to local repository
  7. git remote add origin https://gitee.com/xxxxxxxx/gods.gitAdd a remote warehouse origin and establish an association
  8. git push -u origin "master"Push the current branch masterto the remote warehouseorigin

2.2.1. In IDEA, you can verify whether the code is successfully uploaded to the warehouse

在这里插入图片描述

2.2.2. Check whether the code is uploaded successfully in the gitee warehouse

在这里插入图片描述

2.3. Brief introduction to git common knowledge

在这里插入图片描述
PS: Image source

workspace: work area, where code is usually stored staging
area: temporary storage area/cache area, used to temporarily store your changes, in fact it is just a file, save the information about to be submitted to the file list local
repository: local warehouse, it is safe The location where the data is stored, which contains the data you have submitted to all versions. Where HEAD points to the latest version put into the warehouse remote
repository: remote warehouse, server hosting code

From modification to submission to the remote warehouse, the code generally needs to go through the following five states: unmodified, modified, temporarily stored, submitted, and pushed. As follows:

未修改
       原始内容
已修改    ↓   
       工 作 区
已暂存    ↓    git add
       暂 存 区
已提交    ↓    git commit
       本地仓库
已推送    ↓    git push
       远程仓库

Precautions

git init will initialize a .git directory under the root directory of the project. The git warehouse must be
remote and only needs to be executed once. yourRepository.git is a warehouse you already have in github.
gitignore, this file is written to all files that do not need to be added to version management , especially like node_modules

三、Nacos 服务注册 SpringCloud Alibaba

3.1、启动win下的单机nacos

我这里是安装的: NACOS 2.2.2

  1. 第一步:下载nacos到本地之后,打开文件至bin目录,输入cmd进入命令窗口

  2. 第二步:输入启动命令: startup.cmd -m standalone 可以看到启动成功

Linux/Unix/Mac:命令

startup.sh -m standalone

​ Windows:命令

startup.cmd -m standalone

  1. 第三步:启动成功后,浏览器打开网址:http://localhost:8848/nacos/
  2. 默认nacos/nacos,成功查看(单机不需要登录)

在这里插入图片描述

在这里插入图片描述

3.2、给项目创建一个命名空间

在这里插入图片描述

PS: 这里 命名空间ID 要着重记忆

3.3、引入新的需要的依赖

这里要在两个子项目中的dependencies{}中添加如下依赖
这是一种 基于 maven方式的 gradle插件在管理依赖,如果需要,请跳转目录:1.2

    //    SpringCloud Alibaba nacos服务发现
    implementation('com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery') {
    
    
        exclude group: 'org.springframework.cloud', module: 'spring-cloud-starter-netflix-ribbon'
    }
    //用于简化配置管理和外部化配置
    //可以从远程配置服务器(如 Git 存储库)中获取应用程序的配置信息,然后将这些配置信息注入到应用程序中
    implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap'

3.3.1 子项目依赖更新图

在这里插入图片描述

3.4、 启动类添加 @EnableDiscoveryClient 注解

@EnableDiscoveryClient 是 Spring Cloud 的一个注解,用于启用服务发现功能

package com.dage;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class AresSpringBootApplication {
    
    

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

3.5 、创建bootstrap.yml进行配置

这里因为我有两个子项目,便于理解,我贴出了两个配置
这里的命名空间ID 就是 目录 3.2中需要注意的内容
这里要注意的一点是:application.yml中的server.port 配置我拿到了 bootstrap.yml中
所以现在 application.yml是空的。

server:
  port: 8082
spring:
  application:
    name: mall-Ares
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
        enabled: true
        namespace: deity88481314 #命名空间ID

server:
  port: 8083
spring:
  application:
    name: mall-angel
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
        enabled: true
        namespace: deity88481314 #命名空间ID

3.6、在nacos管理平台中查看注册进入的服务

在这里插入图片描述

测试完成

Guess you like

Origin blog.csdn.net/aaxzsuj/article/details/130816105