1、Spring Boot 3.x 集成 Eureka Server/Client

一、前言

复制代码
基于 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
xml 复制代码
    <!-- 依赖版本管理,用于管理子模块的依赖版本 -->
    <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 文件
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
java 复制代码
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApp {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApp.class);
    }
}
复制代码
application.yml
yaml 复制代码
server:
  # 监听端口
  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 看一下,是否有配置问题,要是在控制台出现以下内容,就代表 eureka-server 配置完毕了
服务
复制代码
到这里,可以打开浏览器访问 eureka-server 管理页面看看,http://localhost:10001 
复制代码
到此为止,eureka 的服务端就已经搭建完毕

子服务 system-server

复制代码
pom.xml
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
java 复制代码
@SpringBootApplication
@EnableDiscoveryClient
public class SystemServerApp {

    public static void main(String[] args) {
        SpringApplication.run(SystemServerApp.class);
    }
}
复制代码
application.yml
yaml 复制代码
server:
  # 监听端口
  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
    # 单节点无所谓,集群必须设置为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 的配置项,还是老旧的那一套,没有太大的变化
相关推荐
uzong37 分钟前
面试官:Redis中的 16 库同时发送命令,服务端是串行执行还是并行执行
后端·面试·架构
追逐时光者2 小时前
.NET 使用 MethodTimer 进行运行耗时统计提升代码的整洁性与可维护性!
后端·.net
练习时长一年2 小时前
AopAutoConfiguration源码阅读
java·spring boot·intellij-idea
你的人类朋友3 小时前
【Node.js】什么是Node.js
javascript·后端·node.js
David爱编程4 小时前
面试必问!线程生命周期与状态转换详解
java·后端
LKAI.5 小时前
传统方式部署(RuoYi-Cloud)微服务
java·linux·前端·后端·微服务·node.js·ruoyi
Victor3565 小时前
Redis(11)如何通过命令行操作Redis?
后端
Victor3565 小时前
Redis(10)如何连接到Redis服务器?
后端
他日若遂凌云志7 小时前
深入剖析 Fantasy 框架的消息设计与序列化机制:协同架构下的高效转换与场景适配
后端
快手技术7 小时前
快手Klear-Reasoner登顶8B模型榜首,GPPO算法双效强化稳定性与探索能力!
后端