Eureka入门

Eureka是一种服务发现工具,广泛应用于微服务架构中。它主要由Netflix开源,帮助服务在分布式系统中自动注册和发现。以下是Eureka的基本入门指南。

前提条件

在开始之前,确保你已经安装了以下软件:

  • JDK 8或更高版本
  • Maven或Gradle

步骤 1:创建Eureka服务器

  1. 创建一个Spring Boot项目,可以使用Spring Initializr(https://start.spring.io/)来生成项目。

    • 选择Spring Boot版本。
    • 添加依赖项:Eureka Server
  2. pom.xml中添加Eureka Server依赖项(如果没有使用Spring Initializr生成项目):

    复制代码
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
  3. 在主应用程序类中启用Eureka Server:

    复制代码
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    @SpringBootApplication
    @EnableEurekaServer
    public class EurekaServerApplication {
        public static void main(String[] args) {
            SpringApplication.run(EurekaServerApplication.class, args);
        }
    }
  4. application.ymlapplication.properties中进行基本配置:

    复制代码
    server:
      port: 8761
    
    eureka:
      client:
        register-with-eureka: false
        fetch-registry: false
      server:
        enable-self-preservation: false

    步骤 2:创建Eureka客户端

  1. 创建另一个Spring Boot项目作为Eureka客户端。

    • 选择Spring Boot版本。
    • 添加依赖项:Eureka Discovery Client
  2. pom.xml中添加Eureka客户端依赖项:

    复制代码
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
  3. 在主应用程序类中启用Eureka客户端:

    复制代码
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    @SpringBootApplication
    @EnableDiscoveryClient
    public class EurekaClientApplication {
        public static void main(String[] args) {
            SpringApplication.run(EurekaClientApplication.class, args);
        }
    }
  4. application.ymlapplication.properties中进行配置,指定Eureka服务器的URL:

    复制代码
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka/

步骤 3:启动和验证

  1. 启动Eureka服务器应用程序。
  2. 启动Eureka客户端应用程序。
  3. 访问Eureka服务器的控制台(默认URL为:http://localhost:8761/),可以看到注册的客户端服务。

总结

通过以上步骤,你已经成功设置了一个简单的Eureka服务注册和发现系统。Eureka服务器管理服务实例,Eureka客户端注册自身并能够发现其他服务。这是微服务架构中实现服务发现和负载均衡的基础。

相关推荐
Joemt2 天前
ubuntu22.04离线一键安装gpu版docker
docker·容器·eureka
慕y2742 天前
Java学习第九十六部分——Eureka
java·学习·eureka
YuforiaCode3 天前
24黑马SpringCloud的Docker本地目录挂载出现相关问题解决
spring cloud·docker·eureka
bluebonnet273 天前
【python】转移本地安装的python包
java·python·eureka
cici158748 天前
Docker搭建Hadoop集群
hadoop·docker·eureka
小醉你真好8 天前
7、Docker 常用命令大全
docker·容器·eureka
土豆丶杨9 天前
centos 配置docker
docker·eureka·centos
hzulwy9 天前
docker与k8s的容器数据卷
docker·eureka·kubernetes
刘一说9 天前
梳理一些 Docker 常用命令
docker·容器·eureka
Reggie_L10 天前
Eureka-服务注册,服务发现
云原生·eureka·服务发现