一、Eureka架构介绍

Eureka是Netflix开发的服务发现组件,本身是一个基于REST的服务。
Spring Cloud将它集成在其子项目spring-cloud-netflix中,以实现Spring Cloud的服务发现功能服务发现组件对比(CAP)Zookeeper - CPEureka - APEureka
他的源码在github上面:https://github.com/Netflix/eureka/
他的文档:https://github.com/Netflix/eureka/wiki
他的架构介绍:High level architecture:https://github.com/Netflix/eureka/wiki/Eureka-at-a-glance

image.png

二、服务器端:

加入spring cloud父POM及spring-cloud-starter-eureka-server

  1. <dependencyManagement>
  2. <dependencies>
  3. <dependency>
  4. <groupId>org.springframework.cloud</groupId>
  5. <artifactId>spring-cloud-dependencies</artifactId>
  6. <version>Edgware.SR3</version>
  7. <type>pom</type>
  8. <scope>import</scope>
  9. </dependency>
  10. </dependencies>
  11. </dependencyManagement>
  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-eureka-server</artifactId>
  4. </dependency>

Application.properties:

  1. server.port=8761
  2. #取消向eureka server(注册中心)注册
  3. eureka.client.register-with-eureka=false
  4. #取消向eureka server(注册中心)获取注册信息
  5. eureka.client.fetch-registry=false
  6. #eureka 提供服务发现的地址
  7. eureka.client.service-url.defaultZone=http://localhost:8761/eureka
  1. 启动类增加注解@EnableEurekaServer

三、微服务端

加入spring cloud父POM及spring-cloud-starter-eureka

  1. <dependencyManagement>
  2. <dependencies>
  3. <dependency>
  4. <groupId>org.springframework.cloud</groupId>
  5. <artifactId>spring-cloud-dependencies</artifactId>
  6. <version>Edgware.SR3</version>
  7. <type>pom</type>
  8. <scope>import</scope>
  9. </dependency>
  10. </dependencies>
  11. </dependencyManagement>
  1. <!-- eureka -->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-eureka</artifactId>
  5. </dependency>

Application.properties:

  1. eureka.client.serviceUrl.defaultZone=http://zhangfei:777@localhost:8761/eureka
  2. eureka.instance.prefer-ip-address=true
  3. eureka.instance.instance-id=${spring.application.name}:${spring.application.instance_id:${server.port}}
  1. 启动类增加注解@EnableEurekaClient