Spring Boot 3.x 集成 Eureka Server/Client的詳細(xì)過(guò)程
一、前言
基于 Spring Boot 3.x 版本開(kāi)發(fā),因?yàn)?Spring Boot 3.x 暫時(shí)沒(méi)有正式發(fā)布,所以很少有 Spring Boot 3.x 開(kāi)發(fā)的項(xiàng)目,自己也很想了踩踩坑,看看 Spring Boot 3.x 與 2.x 有什么區(qū)別。自己與記錄一下在 Spring Boot 3.x 過(guò)程中遇到一下問(wèn)題
二、搭建服務(wù)
chain 服務(wù)
pom.xml 文件,我這里使用的是 Spring Boot 版本 3.3.4,Spring Cloud 版本是 2023.0.3
<!-- 依賴版本管理,用于管理子模塊的依賴版本 -->
<properties>
<!-- 項(xiàng)目編碼 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- java編譯版本 -->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!-- java版本 -->
<java.version>17</java.version>
<!-- chain 版本 -->
<chain.version>1.0.0</chain.version>
<!--SpringCloud版本-->
<spring-cloud.version>2023.0.3</spring-cloud.version>
<!-- spring-boot版本 -->
<spring.boot.version>3.3.4</spring.boot.version>
<!-- spring framework版本 -->
<spring.framework.version>6.1.13</spring.framework.version>
</properties>
<!-- 依賴聲明 -->
<dependencyManagement>
<dependencies>
<!--依賴管理,用于管理spring-cloud的依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring framework版本 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.framework.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring-boot版本2.5.15更換為3.2.4 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.3.4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>子服務(wù) eureka-server
pom.xml 文件
<dependencies>
<!-- eureka server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- spring boot starter test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>EurekaServerAPP
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApp {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApp.class);
}
}application.yml
server:
# 監(jiān)聽(tīng)端口
port: 10001
spring:
application:
# 服務(wù)名稱
name: eureka-server
eureka:
instance:
# eureka 服務(wù)實(shí)例的主機(jī)名稱
hostname: ${spring.application.name}
client:
# 表示是否將自己注冊(cè)進(jìn)EurekaServer默認(rèn)為true
register-with-eureka: false
# 表示是否從EurekaServer抓取已有的注冊(cè)信息,默認(rèn)為true
fetch-registry: false
# EurekaServer服務(wù)提供地址
service-url:
# 單機(jī)版
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/上面三個(gè)文件配置完畢之后,可以啟動(dòng)一下 EurekaServerApp 看一下,是否有配置問(wèn)題,要是在控制臺(tái)出現(xiàn)以下內(nèi)容,就代表 eureka-server 配置完畢了
服務(wù)

到這里,可以打開(kāi)瀏覽器訪問(wèn) eureka-server 管理頁(yè)面看看,http://localhost:10001

到此為止,eureka 的服務(wù)端就已經(jīng)搭建完畢
子服務(wù) system-server
pom.xml
<dependencies>
<!-- eureka client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- spring boot web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring boot starter test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- spring boot devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
</dependencies>SystemServerApp
@SpringBootApplication
@EnableDiscoveryClient
public class SystemServerApp {
public static void main(String[] args) {
SpringApplication.run(SystemServerApp.class);
}
}application.yml
server:
# 監(jiān)聽(tīng)端口
port: 10010
servlet:
# 應(yīng)用的訪問(wèn)路徑
context-path: /
spring:
application:
# 服務(wù)名稱
name: system-service
eureka:
instance:
# eureka 服務(wù)實(shí)例的主機(jī)名稱
hostname: ${spring.application.name}
# 服務(wù)實(shí)例的注冊(cè)ID
#lease-instance-id: ${spring.application.name}:${server.port}
# 服務(wù)實(shí)例的注冊(cè)時(shí)間間隔,單位為秒
#lease-renewal-interval-in-seconds: 5
# 是否開(kāi)啟安全認(rèn)證
#security:
#basic:
#enabled: false
client:
# 表示是否將自己注冊(cè)進(jìn)EurekaServer默認(rèn)為true
register-with-eureka: true
# 表示是否從EurekaServer抓取已有的注冊(cè)信息,默認(rèn)為true
# 單節(jié)點(diǎn)無(wú)所謂,集群必須設(shè)置為true才能配合ribbon使用負(fù)載均衡
fetch-registry: true
# EurekaServer服務(wù)提供地址
service-url:
# 單機(jī)版
defaultZone: http://localhost:10001/eureka/同樣啟動(dòng)一下 system-server 服務(wù)測(cè)試

也可以看一下在 eureka-server 服務(wù)中是否有 system-server 注冊(cè)信息

也可以去到 eureka-server 管理頁(yè)面,看看 system-server 是否注冊(cè)成功

搭建 eureka server/client 相對(duì)比較簡(jiǎn)單,在這個(gè)過(guò)程中主要是要找對(duì) Spring Boot 與 Spring Cloud 的版本即可,eureka 的配置項(xiàng),還是老舊的那一套,沒(méi)有太大的變化
到此這篇關(guān)于Spring Boot 3.x 集成 Eureka Server/Client的文章就介紹到這了,更多相關(guān)Spring Boot 集成 Eureka Server/Client內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
linux部署出現(xiàn)java文件操作報(bào)錯(cuò):java.io.FileNotFoundException解決辦法
這篇文章主要g介紹了linux部署出現(xiàn)java文件操作報(bào)錯(cuò):java.io.FileNotFoundException解決的相關(guān)資料,這個(gè)錯(cuò)誤通常表示你的Spring Boot應(yīng)用程序無(wú)法找到指定的文本文件,需要的朋友可以參考下2023-12-12
Spring中獲取HttpServletRequest的三種方式小結(jié)
spring框架web環(huán)境中,獲取HttpServletRequest是常見(jiàn)的操作,本文將為大家詳細(xì)介紹一下Spring中獲取HttpServletRequest的三種方式,有需要的小伙伴可以了解下2026-04-04
myBatis實(shí)現(xiàn)三級(jí)嵌套復(fù)雜對(duì)象的賦值問(wèn)題
這篇文章主要介紹了myBatis實(shí)現(xiàn)三級(jí)嵌套復(fù)雜對(duì)象的賦值問(wèn)題,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
JavaMe開(kāi)發(fā)繪制文本框TextEdit
在JavaMe連載(3)-也說(shuō)MVC設(shè)計(jì)模式 一文中提到了一個(gè)TextEdit類,但沒(méi)有給出具體實(shí)現(xiàn),TextEdit是采用GameCanvas繪制的文本編輯器。本文結(jié)合實(shí)例給出實(shí)現(xiàn)的方法。2015-09-09
使用Java實(shí)現(xiàn)創(chuàng)建Excel表單控件
在數(shù)據(jù)填報(bào)時(shí),創(chuàng)建Excel表單控件是一項(xiàng)常見(jiàn)的任務(wù),它可以極大地簡(jiǎn)化數(shù)據(jù)收集和處理的過(guò)程,本文主要介紹了如何使用Java實(shí)現(xiàn)創(chuàng)建Excel表單控件,感興趣的可以了解下2024-03-03
詳解Java七大阻塞隊(duì)列之SynchronousQueue
SynchronousQueue不需要存儲(chǔ)線程間交換的數(shù)據(jù),它的作用像是一個(gè)匹配器,使生產(chǎn)者和消費(fèi)者一一匹配。本文詳細(xì)講解了Java七大阻塞隊(duì)列之一SynchronousQueue,需要了解的小伙伴可以參考一下這篇文章2021-09-09

