Spring Boot 3.x 集成 Eureka Server/Client的詳細過程
一、前言
基于 Spring Boot 3.x 版本開發(fā),因為 Spring Boot 3.x 暫時沒有正式發(fā)布,所以很少有 Spring Boot 3.x 開發(fā)的項目,自己也很想了踩踩坑,看看 Spring Boot 3.x 與 2.x 有什么區(qū)別。自己與記錄一下在 Spring Boot 3.x 過程中遇到一下問題
二、搭建服務
chain 服務
pom.xml 文件,我這里使用的是 Spring Boot 版本 3.3.4,Spring Cloud 版本是 2023.0.3
<!-- 依賴版本管理,用于管理子模塊的依賴版本 --> <properties> <!-- 項目編碼 --> <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>
子服務 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)聽端口 port: 10001 spring: application: # 服務名稱 name: eureka-server eureka: instance: # eureka 服務實例的主機名稱 hostname: ${spring.application.name} client: # 表示是否將自己注冊進EurekaServer默認為true register-with-eureka: false # 表示是否從EurekaServer抓取已有的注冊信息,默認為true fetch-registry: false # EurekaServer服務提供地址 service-url: # 單機版 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
上面三個文件配置完畢之后,可以啟動一下 EurekaServerApp 看一下,是否有配置問題,要是在控制臺出現(xiàn)以下內(nèi)容,就代表 eureka-server 配置完畢了
服務
到這里,可以打開瀏覽器訪問 eureka-server 管理頁面看看,http://localhost:10001
到此為止,eureka 的服務端就已經(jīng)搭建完畢
子服務 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)聽端口 port: 10010 servlet: # 應用的訪問路徑 context-path: / spring: application: # 服務名稱 name: system-service eureka: instance: # eureka 服務實例的主機名稱 hostname: ${spring.application.name} # 服務實例的注冊ID #lease-instance-id: ${spring.application.name}:${server.port} # 服務實例的注冊時間間隔,單位為秒 #lease-renewal-interval-in-seconds: 5 # 是否開啟安全認證 #security: #basic: #enabled: false client: # 表示是否將自己注冊進EurekaServer默認為true register-with-eureka: true # 表示是否從EurekaServer抓取已有的注冊信息,默認為true # 單節(jié)點無所謂,集群必須設置為true才能配合ribbon使用負載均衡 fetch-registry: true # EurekaServer服務提供地址 service-url: # 單機版 defaultZone: http://localhost:10001/eureka/
同樣啟動一下 system-server 服務測試
也可以看一下在 eureka-server 服務中是否有 system-server 注冊信息
也可以去到 eureka-server 管理頁面,看看 system-server 是否注冊成功
搭建 eureka server/client 相對比較簡單,在這個過程中主要是要找對 Spring Boot 與 Spring Cloud 的版本即可,eureka 的配置項,還是老舊的那一套,沒有太大的變化
到此這篇關于Spring Boot 3.x 集成 Eureka Server/Client的文章就介紹到這了,更多相關Spring Boot 集成 Eureka Server/Client內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springsecurity記住我登錄時訪問無權限接口跳轉(zhuǎn)登錄界面的處理方案
這篇文章主要介紹了springsecurity記住我登錄時訪問無權限接口跳轉(zhuǎn)登錄界面的處理方案,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-02-02java.lang.IllegalStateException:方法有太多主體參數(shù)問題
這篇文章主要介紹了java.lang.IllegalStateException:方法有太多主體參數(shù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07mybatis中string和date的轉(zhuǎn)換方式
這篇文章主要介紹了mybatis中string和date的轉(zhuǎn)換方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08如何利用postman完成JSON串的發(fā)送功能(springboot)
這篇文章主要介紹了如何利用postman完成JSON串的發(fā)送功能(springboot),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07