原创

Eureka的使用以及配置

温馨提示:
本文最后更新于 2018年06月01日,已超过 2,147 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

1.pom.xml依赖

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!--parent是SpringBoot的父依赖-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.lzhpo</groupId>
    <artifactId>eureka_server_01</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka_server_01</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <!--使用Java8版本-->
        <java.version>1.8</java.version>
        <!--SpringCloud的版本-->
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <!--eureka-server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!--SpringBoot的测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>

    <!--SpringBoot的Maven插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2.配置文件application.yml和启动类注解@EnableEurekaServer

单机版

启动类加上注解@EnableEurekaServer

server:
  # Eureka的启动端口
  port: 8761

spring:
  application:
    # 服务名称
    name: eureka_server

eureka:
  instance:
    # Eureka-Server的实例名称
    hostname: localhost
    # 当应用程序向eureka注册时,它将使用其IP地址而不是其主机名
    prefer-ip-address: true
  client:
    # 是否注册自己到Eureka中作为服务,默认为true( registerWithEureka 和 fetchRegistry 都为false说明明自己是个服务端 )
    registerWithEureka: false
    # 指示此客户端是否应从Eureka服务器获取Eureka注册表信息,默认为true
    fetchRegistry: false
    serviceUrl:
      # 要注册到Eureka的服务注册的地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

  ################################# 以下建议开发环境配置 #################################
  server:
    # 禁用自我保护模式(生产模式不建议关闭,开发模式中随意)
    enable-self-preservation: false
    # 等待2秒没心跳自动剔除
    eviction-interval-timer-in-ms: 2000

集群版

Windows的hosts文件添加:

127.0.0.1        cloud.eureka01.com
127.0.0.1        cloud.eureka02.com

eureka_server_ha_01

启动类加上注解@EnableEurekaServer

server:
  # Eureka的启动端口
  port: 8861

spring:
  application:
    # 服务名称
    name: eureka_server_ha

eureka:
  instance:
    # Eureka-Server的实例名称
    hostname: cloud.eureka01.com
    # 当应用程序向eureka注册时,它将使用其IP地址而不是其主机名
    prefer-ip-address: true
  client:
    # 是否注册自己到Eureka中作为服务,默认为true( registerWithEureka 和 fetchRegistry 都为false说明明自己是个服务端 )
    registerWithEureka: true
    # 指示此客户端是否应从Eureka服务器获取Eureka注册表信息,默认为true
    fetchRegistry: true
    serviceUrl:
      # 要注册到Eureka的服务注册的地址(集群版,你中有我,我中有你)
      defaultZone: http://cloud.eureka02.com:8862/eureka/

  ################################# 以下建议开发环境配置 #################################
  server:
    # 禁用自我保护模式(生产模式不建议关闭,开发模式中随意)
    enable-self-preservation: false
    # 等待2秒没心跳自动剔除
    eviction-interval-timer-in-ms: 2000

eureka_server_ha_02

启动类加上注解@EnableEurekaServer

server:
  # Eureka的启动端口
  port: 8862

spring:
  application:
    # 服务名称
    name: eureka_server_ha

eureka:
  instance:
    # Eureka-Server的实例名称
    hostname: cloud.eureka02.com
    # 当应用程序向eureka注册时,它将使用其IP地址而不是其主机名
    prefer-ip-address: true
  client:
    # 是否注册自己到Eureka中作为服务,默认为true( registerWithEureka 和 fetchRegistry 都为false说明明自己是个服务端 )
    registerWithEureka: true
    # 指示此客户端是否应从Eureka服务器获取Eureka注册表信息,默认为true
    fetchRegistry: true
    serviceUrl:
      # 要注册到Eureka的服务注册的地址(集群版,你中有我,我中有你)
      defaultZone: http://cloud.eureka01.com:8861/eureka/

  ################################# 以下建议开发环境配置 #################################
  server:
    # 禁用自我保护模式(生产模式不建议关闭,开发模式中随意)
    enable-self-preservation: false
    # 等待2秒没心跳自动剔除
    eviction-interval-timer-in-ms: 2000

注意事项

集群版关闭了自我保护模式enable-self-preservation和自动剔除指定时间没有心跳的服务eviction-interval-timer-in-ms
先启动的可能会报错com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
这个是不影响的,发现注册中心之后就会恢复正常。

简单了解一下@EnableEurekaServer注解

加上@EnableEurekaServer注解表示启动一个服务注册中心。

package com.lzhpo.eureka_server_01;

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

@SpringBootApplication
@EnableEurekaServer     //启动一个服务注册中心
public class EurekaServer01Application {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServer01Application.class, args);
    }

}

@EnableEurekaServer注解:
@Import(EurekaServerMarkerConfiguration.class)激活@EnableEurekaServer注解

package org.springframework.cloud.netflix.eureka.server;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.context.annotation.Import;

/**
 * Annotation to activate Eureka Server related configuration.
 * {@link EurekaServerAutoConfiguration}
 *
 * @author Dave Syer
 * @author Biju Kunjummen
 *
 */

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(EurekaServerMarkerConfiguration.class)
public @interface EnableEurekaServer {

}

EurekaServerMarkerConfiguration类:
负责添加一个标记eurekaServerMarkerBean的bean来激活@EnableEurekaServer注解。

package org.springframework.cloud.netflix.eureka.server;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Responsible for adding in a marker bean to activate
 * {@link EurekaServerAutoConfiguration}.
 *
 * @author Biju Kunjummen
 */
@Configuration
public class EurekaServerMarkerConfiguration {

    @Bean
    public Marker eurekaServerMarkerBean() {
        return new Marker();
    }

    class Marker {

    }

}

Eureka的自我保护机制

eureka管理后台出现一串红色字体:是警告,说明有服务上线率低
EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.

  • 如果开启了Eureka的自我保护机制(默认是开启的),当一个注册在Eureka注册中心的服务挂掉之后,Eureka注册中心不会剔除这个挂掉的服务,而是会将数据保存下来,当这个挂掉的服务恢复正常之后,还可以自动恢复之前的数据。
  • 如果关闭了Eureka的自我保护机制,那么这个服务挂了之后就直接被Eureka注册中心剔除了,就算是此服务恢复正常了,之前的数据也没了。
本文目录