Spring Boot 3.x 集成 Eureka Server/Client的详细过程

 更新时间:2024年09月30日 10:38:50   作者:Kenny.志  
随着SpringBoot 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

    <!-- 依赖版本管理,用于管理子模块的依赖版本 -->
    <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:
  # 监听端口
  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

<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:
  # 监听端口
  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 的配置项,还是老旧的那一套,没有太大的变化

到此这篇关于Spring Boot 3.x 集成 Eureka Server/Client的文章就介绍到这了,更多相关Spring Boot 集成 Eureka Server/Client内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java中的SynchronousQueue阻塞队列使用代码实例

    Java中的SynchronousQueue阻塞队列使用代码实例

    这篇文章主要介绍了Java中的SynchronousQueue阻塞队列使用代码实例,SynchronousQueue是无缓冲区的阻塞队列,即不能直接向队列中添加数据,会报队列满异常,需要的朋友可以参考下
    2023-12-12
  • Java使用多线程异步执行批量更新操作方法

    Java使用多线程异步执行批量更新操作方法

    这篇文章主要介绍了Java使用多线程异步执行批量更新操作,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-01-01
  • IDEA GIT 忽略文件的最佳方式推荐

    IDEA GIT 忽略文件的最佳方式推荐

    这篇文章主要介绍了IDEA GIT 忽略文件的最佳方式推荐,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • Mybatis之Mapper动态代理实例解析

    Mybatis之Mapper动态代理实例解析

    这篇文章主要介绍了Mybatis之Mapper动态代理实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • 深入了解Java8中的时区日期时间

    深入了解Java8中的时区日期时间

    Java 在 java.time 包中也提供了几个类用于处理需要关注时区的日期时间 API,本文将通过简单的示例讲讲它们的用法,需要的可以参考一下
    2023-04-04
  • spring boot 常见http请求url参数获取方法

    spring boot 常见http请求url参数获取方法

    这篇文章主要介绍了spring boot 常见http请求url参数获取,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-03-03
  • mybatis的ParamNameResolver参数名称解析

    mybatis的ParamNameResolver参数名称解析

    这篇文章主要为大家介绍了mybatis的ParamNameResolver参数名称解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • 详解MybatisPlus3.4版本之后分页插件的使用

    详解MybatisPlus3.4版本之后分页插件的使用

    从Mybatis Plus 3.4.0版本开始,不再使用旧版本的PaginationInterceptor ,而是使用MybatisPlusInterceptor。本文就详细的介绍一下两者的区别,感兴趣的可以了解一下
    2021-11-11
  • 用dom4j生成xml,去掉xml头的方法

    用dom4j生成xml,去掉xml头的方法

    今天小编就为大家分享一篇用dom4j生成xml,去掉xml头的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • SpringBoot项目加入冲突动态监测算法的实现

    SpringBoot项目加入冲突动态监测算法的实现

    冲突动态监测算法是一种网络通信中的冲突检测方法,适用于无线网络或其他共享传输介质的环境,本文主要介绍了SpringBoot项目加入冲突动态监测算法的实现,感兴趣的可以了解一下
    2023-09-09

最新评论