基于gradle构建spring cloud项目
構(gòu)建環(huán)境
idea:2021.1.2
gradle:4.10.3
項目介紹
gradle-spring-cloud 根項目,用于統(tǒng)一一些公共配置
gradle-eurakeserver 模塊使用eurake提供服務(wù)注冊功能
gradle-getway 提供網(wǎng)關(guān)服務(wù)
gradle-serviceA和gradle-serviceB用來提供接口服務(wù),服務(wù)名相同,用于測試getway 負載均衡
構(gòu)建開始
一、創(chuàng)建根項目(gradle-spring-cloud)
1.idea創(chuàng)建新的gradle項目
2.配置項目gradle
3.修改buid.gradle文件
buildscript {ext {springBootVersion = '2.0.7.RELEASE'springCloudVersion = 'Finchley.SR2'}repositories {maven { url "http://nexus.xxxxx.com/repository/maven-public" }}dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")} } allprojects {group 'org.example'version '1.0-SNAPSHOT'apply plugin: 'java'// 指定JDK版本sourceCompatibility = 1.8targetCompatibility = 1.8//指定編碼格式tasks.withType(JavaCompile) {options.encoding = "UTF-8"}repositories {maven { url "http://nexus.xxxxx.com/repository/maven-public" }} }subprojects {//dependency-management 插件apply plugin: 'io.spring.dependency-management'dependencyManagement {imports {mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"mavenBom "org.springframework.boot:spring-boot-dependencies:${springBootVersion}"}}jar {manifest.attributes provider: 'gradle'}}說明:?maven { ?url "http://nexus.xxxxx.com/repository/maven-public" } 配置的是 maven私服地址,大家可以配置成阿里云的鏡像地址。
4.build下項目,無報錯,根項目構(gòu)建成功。
二、構(gòu)建gradle-eurakeserver服務(wù)注冊中心
1.在根項目下,new module,取名為gradle-eurakeserver
2.修改build.gradle配置文件,加入相關(guān)依賴
apply plugin: 'org.springframework.boot'dependencies { //dependencies閉包implementation 'org.springframework.boot:spring-boot-starter-web'implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'testImplementation 'org.springframework.boot:spring-boot-starter-test' }3.build項目
4.創(chuàng)建spring boot啟動類
package org.example.eurake.server;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication @EnableEurekaServer public class Application {public static void main(String[] args) {SpringApplication.run(Application.class,args);} }5.配置application.properties
server.port=7101 spring.application.name=gradle-eureka #不向注冊中心注冊自己 eureka.client.register-with-eureka=false #健康檢測 eureka.client.fetch-registry=false #開發(fā)環(huán)境關(guān)閉自我保護機制,保證不可用的服務(wù)及時剔除 eureka.server.enable-self-preservation=false #間隔2秒鐘剔除一次 eureka.server.eviction-interval-timer-in-ms=2000 #服務(wù)注冊地址 eureka.client.service-url.defaultZone=http://127.0.0.1:7101/eureka6.啟動項目,訪問 http://127.0.0.1:7101/? 注冊中心搭建完畢
三、搭建gradle-getway網(wǎng)關(guān)服務(wù)
1.在根項目下,new module,取名為gradle-getway
2.修改build.gradle配置文件,加入相關(guān)依賴
dependencies {implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'implementation 'org.springframework.cloud:spring-cloud-starter-gateway'implementation 'org.springframework.cloud:spring-boot-starter-data-redis-reactive'implementation 'org.springframework.cloud:spring-cloud-starter-netflix-hystrix'implementation 'com.alibaba:fastjson:1.2.47' }3.build項目
4.創(chuàng)建spring boot啟動類
@SpringBootApplication @EnableDiscoveryClient public class GetWayApplication {public static void main(String[] args) {SpringApplication.run(GetWayApplication.class,args);} }5.配置application.yml
# 端口 server:port: 8000spring:application:# 應(yīng)用名稱name: gradle-getwaycloud:gateway:discovery:locator:# 是否和服務(wù)注冊與發(fā)現(xiàn)組件結(jié)合,設(shè)置為 true 后可以直接使用應(yīng)用名稱調(diào)用服務(wù)enabled: true# 路由(routes:路由,它由唯一標(biāo)識(ID)、目標(biāo)服務(wù)地址(uri)、一組斷言(predicates)和一組過濾器組成(filters)。filters 不是必需參數(shù)。)routes:# 路由標(biāo)識(id:標(biāo)識,具有唯一性) 轉(zhuǎn)發(fā)指定服務(wù)并傳入?yún)?shù)- id: route_addRequestParameter# 目標(biāo)服務(wù)地址(uri:地址,請求轉(zhuǎn)發(fā)后的地址)uri: lb://gradle-service# 路由條件(predicates:斷言,匹配 HTTP 請求內(nèi)容)predicates:## 匹配 GET 請求- Method=GET# 過濾器(filters:過濾器,過濾規(guī)則)filters:## 添加指定參數(shù)- AddRequestParameter=age, threeeureka:client:serviceUrl:# 注冊中心地址defaultZone: http://127.0.0.1:7101/eurekalogging:level:# log 級別org.springframework.cloud.gateway: debug6.啟動getway,成功注冊到eureka
四、創(chuàng)建gradle-service 服務(wù),AB服務(wù)只是端口不同,所以只以A為例
1.在根項目下,new module,取名為gradle-serviceA
2.修改build.gradle配置文件,加入相關(guān)依賴
dependencies {implementation 'org.springframework.boot:spring-boot-starter-web'implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'implementation 'org.springframework.boot:spring-boot-starter-data-redis'implementation 'org.projectlombok:lombok:1.18.18'implementation 'redis.clients:jedis:2.9.0'implementation 'org.apache.commons:commons-pool2'}3.build項目
4.創(chuàng)建spring boot啟動類
@SpringBootApplication @EnableDiscoveryClient public class ServiceApplicationA {public static void main(String[] args) {SpringApplication.run(ServiceApplicationA.class,args);} }5.配置application.yml
# 端口 server:port: 9000# 應(yīng)用名稱 spring:application:name: gradle-serviceeureka:client:serviceUrl:# 注冊中心地址defaultZone: http://127.0.0.1:7101/eurekafetch-registry: trueregister-with-eureka: true6.創(chuàng)建controller接口類
@RestController public class ASayHelloController {/** @ClassName ASayHelloController* @Desc TODO 讀取配置文件中的端口* @Date 2019/5/20 23:24* @Version 1.0*/@Value("${server.port}")private String port;/** @ClassName ASayHelloController* @Desc TODO Say Hello* @Date 2019/5/20 23:24* @Version 1.0*/@RequestMapping("/hello")public String hello(){return "Hello!I'm a. port:" + port;}}7.相同的創(chuàng)建B服務(wù),端口改為9001.
8.啟動 AB服務(wù),注冊到注冊中心,搭建完成
五、測試getway網(wǎng)關(guān)功能
調(diào)用網(wǎng)關(guān)地址如下?http://localhost:8000/hello
第一次返回?Hello!I'm a. port:9000?
第二次返回?Hello!I'm a. port:9001
基于gradle 構(gòu)建簡單的cloud 項目,完畢。
總結(jié)
以上是生活随笔為你收集整理的基于gradle构建spring cloud项目的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux安装git并配置GitHub账
- 下一篇: 创世纪观后感