Eureka 的使用

单机版 Eureka 注册中心

注册中心

  1. 新建一个 spring-cloud-eureka 项目,用作注册中心

  2. 引入依赖

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
      <properties>
    <java.version>11</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <spring-boot.version>2.3.0.RELEASE</spring-boot.version>
    <spring-cloud.version>Hoxton.SR4</spring-cloud.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>
    </dependencyManagement>

    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>

    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    </dependencies>

  3. 配置属性

    1
    2
    3
    spring.application.name=spring-cloud-eureka
    server.port=8090
    eureka.client.service-url.defaultZone = http://localhost:8090/eureka

    自己注册自己

  4. 启动 eureka-server,在 application 中增加注解@EnableEurekaServer

  5. 启动服务,由于自己注册自己的服务,在启动的时候会报错,可以忽略,也可以在配置文件中增加

    1
    2
    eureka.client.register-with-eureka=false
    eureka.client.fetch-registry=false
  6. 访问 http://localhost:8090/

服务提供者

  1. 增加依赖

    1
    2
    3
    4
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
  2. 启动 eureka-client,在 application 中增加注解@EnableEurekaClient

  3. 增加配置文件

    1
    2
    3
    spring.application.name=order-service
    server.port=8188
    eureka.client.service-url.defaultZone = http://localhost:8090/eureka
  4. 访问注册中心 http://localhost:8090,

    Application AMIs Availability Zones Status
    ORDER-SERVICE n/a (2) (2) UP (2) - 10.64.12.118:order-service:8188 , 10.64.12.118:order-service:8999

高可用版注册中心

Eureka 的高可用是启动多个 eureka 服务节点,每个节点之间相互注册服务,互为主备,一旦某个节点挂掉之后,其他节点照常提供服务。

  1. 创建 2 个 springboot 项目

  2. 添加依赖

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    <properties>
    <java.version>11</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>

    <spring-boot.version>2.3.0.RELEASE</spring-boot.version>
    <spring-cloud.version>Hoxton.SR4</spring-cloud.version>
    </properties>


    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>

    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</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>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>${spring-boot.version}</version>
    <type>pom</type>
    <scope>import</scope>
    </dependency>
    </dependencies>
    </dependencyManagement>

  3. 修改配置文件

    • 配置文件 1

      1
      2
      3
      4
      5
      6
      spring.application.name=spring-cloud-eureka2
      server.port=8091

      eureka.client.service-url.defaultZone=http://localhost:8090/eureka
      eureka.client.register-with-eureka=false
      eureka.client.fetch-registry=false
    • 配置文件 2

      1
      2
      3
      4
      5
      spring.application.name=spring-cloud-eureka
      server.port=8090
      eureka.client.service-url.defaultZone=http://localhost:8091/eureka
      eureka.client.register-with-eureka=false
      eureka.client.fetch-registry=false

    2 个配置文件中的注册中心地址,分别为交换地址,如果存在 2 个以上的注册中心,可以用逗号继续添加

  4. 启动 eureka-server,在 appplication 中添加@EnableEurekaServer 注解

  5. 分别访问 http://localhost:8091/,http://localhost:8090/,对应服务都注册到上面了。