一、前言
基于 Spring Boot 3.x 版本开发,因为 Spring Boot 3.x 暂时没有正式发布,所以很少有 Spring Boot 3.x 开发的项目,自己也很想了踩踩坑,看看 Spring Boot 3.x 与 2.x 有什么区别。自己与记录一下在 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>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>17</java.version>
<chain.version>1.0.0</chain.version>
<spring-cloud.version>2023.0.3</spring-cloud.version>
<spring.boot.version>3.3.4</spring.boot.version>
<spring.framework.version>6.1.13</spring.framework.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.framework.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<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:
port: 10001
spring:
application:
name: eureka-server
eureka:
instance:
hostname: ${spring.application.name}
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
上面三个文件配置完毕之后,可以启动一下 EurekaServerApp 看一下,是否有配置问题,要是在控制台出现以下内容,就代表 eureka-server 配置完毕了
服务

到这里,可以打开浏览器访问 eureka-server 管理页面看看,http://localhost:10001

到此为止,eureka 的服务端就已经搭建完毕
子服务 system-server
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<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:
port: 10010
servlet:
context-path: /
spring:
application:
name: system-service
eureka:
instance:
hostname: ${spring.application.name}
client:
register-with-eureka: true
fetch-registry: true
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 的配置项,还是老旧的那一套,没有太大的变化