Gradle下如何搭建SpringCloud分布式環(huán)境
Gradle下搭建SpringCloud分布式環(huán)境
1.idea配置好gradle
這一步不必多說, 常規(guī)操作
2.創(chuàng)建一個(gè)空的gradle項(xiàng)目

3.創(chuàng)建好后, 注意版本號(hào)

4.在本地配置好gradle
將idea的gradle配置改為本地(可選項(xiàng))

5.修改build.gradle
注意springcloud的版本需要和springboot版本對(duì)應(yīng), 不然出大問題(不可盲目追新)

推薦

buildscript {
ext {
springBootVersion = '2.2.5.RELEASE'
springCloudVersion = 'Hoxton.SR1'
}
repositories {
mavenLocal()
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
allprojects {
group 'com.qpf'
version '1.0-SNAPSHOT'
apply plugin: 'java'
// 指定JDK版本
sourceCompatibility = 1.8
targetCompatibility = 1.8
//指定編碼格式
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
repositories {
mavenLocal()
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
mavenCentral()
}
}
subprojects {
//dependency-management 插件
apply plugin: 'io.spring.dependency-management'
dependencyManagement {
imports {
//spring bom helps us to declare dependencies without specifying version numbers.
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
mavenBom "org.springframework.boot:spring-boot-dependencies:${springBootVersion}"
}
}
jar {
manifest.attributes provider: 'gradle'
}
}
5.添加.gitignore文件
根據(jù)需要自己修改內(nèi)容即可,若idea中沒有該插件,自己添加一個(gè),不往git上提交的可以忽略此步


6.創(chuàng)建子模塊

點(diǎn)gradle

7.檢查子模塊有沒有添加到settings.gradle中
若沒有手動(dòng)添加

8.處理
將子模塊下的build.gradle文件中除了dependencies中的內(nèi)容之外全部刪除,添加
apply plugin: 'org.springframework.boot'

例如:
apply plugin: 'org.springframework.boot'
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.cloud:spring-cloud-starter-consul-discovery'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}9.刪除子模塊下多余文件(可選,自己決定)

10.點(diǎn)開右上角的gradle
將與父模塊并列的子模塊刪除

點(diǎn)開父模塊, 可以看到其下的子模塊

說明:刪除模塊時(shí)不要直接刪文件,需要從項(xiàng)目中移除然后刪除File->Project Structure…

然后刪除根項(xiàng)目settings.gradle中的include(‘eureka’),最后從項(xiàng)目中刪除子模塊文件即可
提示:若不想修改gradle本地文件編譯,則不用刪除子模塊中g(shù)radle文件夾就可以(推薦此方法)
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot配置Undertow服務(wù)器全過程
Spring?Boot默認(rèn)使用內(nèi)嵌Tomcat容器簡化部署,但部分開發(fā)者選擇性能更優(yōu)的Undertow替代,集成Undertow需排除Tomcat依賴,直接引入相關(guān)模塊即可實(shí)現(xiàn)高并發(fā)、低內(nèi)存的Web服務(wù)運(yùn)行2025-07-07
快速解決jdk初始化運(yùn)行,防火墻通信選錯(cuò)專業(yè)網(wǎng)絡(luò)的問題
新項(xiàng)目添加不同JDK版本時(shí)因防火墻通信策略導(dǎo)致外部連接失敗,解決方法為調(diào)整防火墻設(shè)置,允許應(yīng)用通過并將其設(shè)為公用網(wǎng)絡(luò),適用于多JDK環(huán)境2025-08-08
mybatis-plus判斷參數(shù)是否為空并作為查詢條件詳解
文章介紹了MyBatis-Plus中判斷參數(shù)是否為空并作為查詢條件的兩種方式:全局配置和單獨(dú)判斷,作者分享了自己的經(jīng)驗(yàn),希望能對(duì)大家有所幫助2026-03-03
IDEA創(chuàng)建SpringBoot項(xiàng)目整合mybatis時(shí)mysql-connector-java報(bào)錯(cuò)異常的詳細(xì)分析
最近工作中發(fā)現(xiàn)了個(gè)錯(cuò)誤,分享給同樣遇到這個(gè)問題的朋友,這篇文章主要給大家介紹了關(guān)于IDEA創(chuàng)建SpringBoot項(xiàng)目整合mybatis時(shí)mysql-connector-j報(bào)錯(cuò)異常的詳細(xì)分析,需要的朋友可以參考下2023-02-02
在lambda的foreach遍歷中break退出操作(lambda foreach break)
這篇文章主要介紹了在lambda的foreach遍歷中break退出操作(lambda foreach break),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
SpringBoot項(xiàng)目使用yml文件鏈接數(shù)據(jù)庫異常問題解決方案
在使用SpringBoot時(shí),利用yml進(jìn)行數(shù)據(jù)庫連接配置需小心數(shù)據(jù)類型區(qū)分,如果用戶名或密碼是數(shù)字,必須用雙引號(hào)包裹以識(shí)別為字符串,避免連接錯(cuò)誤,特殊字符密碼也應(yīng)用引號(hào)包裹2024-10-10

