Spring Cloud-Eureka 服务注册中心

Eureka 是 Netflix 开发的,一个基于 REST 服务的,服务注册与发现的组件git

它主要包括两个组件:Eureka Server 和 Eureka Clientgithub

Eureka Client:一个Java客户端,用于简化与 Eureka Server 的交互(一般就是微服务中的客户端和服务端)
Eureka Server:提供服务注册和发现的能力(一般就是微服务中的注册中心)spring

各个微服务启动时,会经过 Eureka Client 向 Eureka Server 注册本身,Eureka Server 会存储该服务的信息缓存

也就是说,每一个微服务的客户端和服务端,都会注册到 Eureka Server,这就衍生出了微服务相互识别的话题spring-boot

同步:每一个 Eureka Server 同时也是 Eureka Client(逻辑上的)
   多个 Eureka Server 之间经过复制的方式完成服务注册表的同步,造成 Eureka 的高可用
识别:Eureka Client 会缓存 Eureka Server 中的信息
   即便全部 Eureka Server 节点都宕掉,服务消费者仍可以使用缓存中的信息找到服务提供者(笔者已亲测)
续约:微服务会周期性(默认30s)地向 Eureka Server 发送心跳以Renew(续约)信息(相似于heartbeat)
续期:Eureka Server 会按期(默认60s)执行一次失效服务检测功能
   它会检查超过必定时间(默认90s)没有Renew的微服务,发现则会注销该微服务节点
Spring Cloud 已经把 Eureka 集成在其子项目 Spring Cloud Netflix 里面微服务

关于 Eureka 配置的最佳实践,可参考:https://github.com/spring-cloud/spring-cloud-netflix/issues/203fetch

更多介绍,可参考:http://cloud.spring.io/spring-cloud-static/Camden.SR4/#spring-cloud-eureka-serverspa

单个Eureka Server配置部署实例

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </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>Finchley.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

注册中心,启动类code

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
}

注册中心配置文件server

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://localhost:${server.port}/eureka/

spring.cloud.config.discovery.enabled: true
相关文章
相关标签/搜索