当前位置:首页 > 技术 > 正文内容

Spring Cloud微服务:搭建与配置Eureka服务注册中心

访客 技术 2026年6月18日 1

1. 微服务架构中的服务发现

在复杂的微服务架构中,服务实例的数量可能非常庞大,且它们的网络位置(IP地址和端口)并非固定不变。手动管理这些服务的调用关系不仅效率低下,而且极易出错。服务发现机制正是为了解决这一挑战而生,它允许服务实例自动注册自己的信息,并让客户端能够动态查询所需服务的位置。

Spring Cloud生态系统为服务发现提供了多种可选方案,其中包括Netflix Eureka、HashiCorp Consul和Apache ZooKeeper。其中,Eureka因与Spring Cloud的深度集成和易用性,常被视为构建高可用服务注册中心的推荐方案。

2. 构建Eureka注册中心项目

首先,我们创建一个基于Maven的Spring Boot项目,用于承载Eureka服务注册中心。该项目需要引入Spring Cloud Eureka Server的相关依赖。

2.1. 创建Maven项目并配置依赖

pom.xml文件中,确保项目继承自Spring Boot的父项目,并添加spring-cloud-starter-netflix-eureka-serverspring-boot-starter-security依赖。后者用于为Eureka控制台提供基本的安全认证。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!-- 继承Spring Boot父项目 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.18</version> <!-- 根据实际情况调整版本 -->
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.example.microservice</groupId>
    <artifactId>eureka-registry-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>2021.0.8</spring-cloud.version> <!-- 根据实际情况调整版本 -->
    </properties>

    <dependencies>
        <!-- Eureka Server 依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId<
        </dependency>

        <!-- Spring Boot Web 依赖 (可选,但通常需要) -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Spring Security 依赖,用于保护Eureka控制台 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    </dependencies>

    <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>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3. 编写Eureka注册中心启动类

在项目的主包下,创建一个Spring Boot启动类。通过添加@EnableEurekaServer注解,即可将一个普通的Spring Boot应用转变为Eureka服务注册中心。

package com.example.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * Eureka注册中心启动类
 */
@SpringBootApplication
@EnableEurekaServer // 启用Eureka服务注册中心功能
public class EurekaRegistryApp {
    public static void main(String[] args) {
        SpringApplication.run(EurekaRegistryApp.class, args);
    }
}

4. 配置Eureka服务中心

application.yml(或application.properties)配置文件中,我们需要为Eureka服务器指定端口、安全认证信息,以及Eureka客户端的行为。由于当前项目本身就是Eureka服务器,它不需要向其他Eureka服务器注册自己,也不需要获取注册列表。

server:
  port: 8761 # 指定Eureka服务器的监听端口

spring:
  security:
    user:
      name: admin # Eureka控制台的访问用户名
      password: securepass # Eureka控制台的访问密码

eureka:
  instance:
    hostname: eureka-server-host # Eureka服务器实例的主机名,可以是IP地址或域名
  client:
    registerWithEureka: false # 告知Eureka服务器:本应用无需注册到自身
    fetchRegistry: false # 告知Eureka服务器:本应用无需获取其他服务注册信息
    serviceUrl:
      defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/ # Eureka客户端与其他Eureka服务器交互的地址。对于单节点服务器,指向自身。

配置说明:

  • server.port: Eureka服务器运行的端口。
  • spring.security.user.namespring.security.user.password: 配置访问Eureka仪表盘所需的用户名和密码。
  • eureka.instance.hostname: 定义此Eureka实例的主机名。其他服务在注册时会使用此名称来识别Eureka服务器。
  • eureka.client.registerWithEureka: false: 由于这是一个Eureka服务器,它不需要向其他Eureka服务器注册自己,因此设为false
  • eureka.client.fetchRegistry: false: Eureka服务器不需要从其他Eureka服务器获取服务注册列表,因此设为false
  • eureka.client.serviceUrl.defaultZone: Eureka客户端(这里是Eureka服务器本身作为客户端)用于与Eureka服务器交互的地址。对于单节点部署,它指向自身。

5. 启动与验证

完成上述配置后,运行EurekaRegistryApp类的main方法启动应用程序。当服务成功启动后,您可以通过浏览器访问http://localhost:8761/。系统会提示您输入在application.yml中配置的用户名(admin)和密码(securepass)。成功登录后,您将看到Eureka的管理界面,其中显示当前没有任何服务注册。

相关文章

Linux crontab 详解

1) crontab 是什么cron 是 Linux 的定时任务守护进程;crontab 是用来编辑/查看“按时间周期执行命令”的表(cron table)。常见两类:用户 crontab:每个用户一份(crontab -e 编辑)系统级 crontab / cron.d:可指定执行用户(/etc/crontab、/etc/cron.d/*)2) crontab 时间...

富文本里可以允许的 HTML 属性

一、所有标签默认允许的安全属性(极少)class        (可选)id           (通常建议禁用)title️ 注意:id 容易被滥用做锚点注入,很多系统直接禁用class 允许的话最好只允许固定前缀(如 editor-*)二、a 标签允许属性<a href="" t...

Mac 安装 Node.js 指南

方法一:通过官网安装包(最简单,适合初学者)如果你只是想快速安装并开始使用,这是最直接的方法。访问 Node.js 官网。页面会显示两个版本:LTS (Recommended For Most Users):长期支持版,最稳定。建议选这个。Current:最新特性版,包含最新功能但可能不够稳定。下载 .pkg 安装包并运行。按照安装向导点击“下一步”即可完成。方法二:使用 Homebrew 安装(...

Dom\HTML_NO_DEFAULT_NS 的副作用:自动加闭合标签

在使用Dom\HTMLDocument时,Dom\HTML_NO_DEFAULT_NS 将禁止在解析过程中设置元素的命名空间, 此设置是为了与DOMDocument向后兼容而存在的。当使用它时,已知的一个副作用就是:自动加闭合标签例如 </img> 为什么会这样?当你使用:Dom\HTML_NO_DEFAULT_NS文档会变成 无命名空间模式,此时内部更接近 XML...

Laravel 事件和监听器创建

在 Laravel 中,使用 Artisan 命令创建 Events(事件) 和 Listeners(监听器) 是非常高效的。你可以通过以下几种方式来实现:1. 手动创建单个 Event如果你只想创建一个事件类,可以使用 make:event 命令:Bashphp artisan make:event UserRegistered执行后,文件将生成在 app/Even...

自定义域名解析神器 dnsmasq

什么是 dnsmasq?dnsmasq 是一个轻量级、功能强大的网络服务工具,专为小型和中等规模网络设计。它是一个综合的网络基础设施解决方案[1]。dnsmasq 能做什么?功能说明应用场景DNS 转发与缓存将 DNS 查询转发到上游服务器(ISP、Google DNS 等),并在本地缓存结果加快 DNS 查询速度,减少外部 DNS 流量本地 DNS解析本地网络设备的主机名,无需编辑&n...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。