日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

基于gradle构建spring cloud项目

發布時間:2023/12/8 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于gradle构建spring cloud项目 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

構建環境

idea:2021.1.2

gradle:4.10.3

項目介紹

gradle-spring-cloud 根項目,用于統一一些公共配置

gradle-eurakeserver 模塊使用eurake提供服務注冊功能

gradle-getway 提供網關服務

gradle-serviceA和gradle-serviceB用來提供接口服務,服務名相同,用于測試getway 負載均衡

構建開始

一、創建根項目(gradle-spring-cloud)

1.idea創建新的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下項目,無報錯,根項目構建成功。

二、構建gradle-eurakeserver服務注冊中心

1.在根項目下,new module,取名為gradle-eurakeserver

2.修改build.gradle配置文件,加入相關依賴

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.創建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 #開發環境關閉自我保護機制,保證不可用的服務及時剔除 eureka.server.enable-self-preservation=false #間隔2秒鐘剔除一次 eureka.server.eviction-interval-timer-in-ms=2000 #服務注冊地址 eureka.client.service-url.defaultZone=http://127.0.0.1:7101/eureka

6.啟動項目,訪問 http://127.0.0.1:7101/? 注冊中心搭建完畢

三、搭建gradle-getway網關服務

1.在根項目下,new module,取名為gradle-getway

2.修改build.gradle配置文件,加入相關依賴

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.創建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:# 應用名稱name: gradle-getwaycloud:gateway:discovery:locator:# 是否和服務注冊與發現組件結合,設置為 true 后可以直接使用應用名稱調用服務enabled: true# 路由(routes:路由,它由唯一標識(ID)、目標服務地址(uri)、一組斷言(predicates)和一組過濾器組成(filters)。filters 不是必需參數。)routes:# 路由標識(id:標識,具有唯一性) 轉發指定服務并傳入參數- id: route_addRequestParameter# 目標服務地址(uri:地址,請求轉發后的地址)uri: lb://gradle-service# 路由條件(predicates:斷言,匹配 HTTP 請求內容)predicates:## 匹配 GET 請求- Method=GET# 過濾器(filters:過濾器,過濾規則)filters:## 添加指定參數- AddRequestParameter=age, threeeureka:client:serviceUrl:# 注冊中心地址defaultZone: http://127.0.0.1:7101/eurekalogging:level:# log 級別org.springframework.cloud.gateway: debug

6.啟動getway,成功注冊到eureka

四、創建gradle-service 服務,AB服務只是端口不同,所以只以A為例

1.在根項目下,new module,取名為gradle-serviceA

2.修改build.gradle配置文件,加入相關依賴

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.創建spring boot啟動類

@SpringBootApplication @EnableDiscoveryClient public class ServiceApplicationA {public static void main(String[] args) {SpringApplication.run(ServiceApplicationA.class,args);} }

5.配置application.yml

# 端口 server:port: 9000# 應用名稱 spring:application:name: gradle-serviceeureka:client:serviceUrl:# 注冊中心地址defaultZone: http://127.0.0.1:7101/eurekafetch-registry: trueregister-with-eureka: true

6.創建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.相同的創建B服務,端口改為9001.

8.啟動 AB服務,注冊到注冊中心,搭建完成

五、測試getway網關功能

調用網關地址如下?http://localhost:8000/hello

第一次返回?Hello!I'm a. port:9000?

第二次返回?Hello!I'm a. port:9001

基于gradle 構建簡單的cloud 項目,完畢。

總結

以上是生活随笔為你收集整理的基于gradle构建spring cloud项目的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。