eureka主要包括了三个角色
Register Service :服务注册中心,它是一个 rureka Server ,提供服务注册和发现的功能。
Provider Service :服务提供者,它是 Eureka Client ,提供服务。
Consumer Service :服务消费者,它是 Eureka Cient ,消费服务。
编写eureka server
创建maven 多 module模块结构:如下图,eureka-server为服务注册中心,eureka-client为服务提供者或服务消费者

加入依赖
在主maven pom.xml文件添加:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
在 eureka server pom.xml文件添加:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
配置文件
添加配置文件application.yml配置:
server: port: 8761 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
Eureka Server 默认会向自己 注册registerWithEureka ,fetchRegistry 设置为false。
添加启动类注解
在启动类添加注解@EnableEurekaServer
编写eureka client
加入依赖
在 eureka client pom.xml文件添加:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
配置文件
在application.yml配置文件添加:
server: port: 8762 spring: application: name: eureka-client eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
添加启动类注解
在启动类添加注解@EnableEurekaClient
测试
分别启动eureka server, eureka client ,然后在浏览器上打开http://localhost:8761/ ,在Instances currently registered with Eureka一栏会发现,eureka client已注册到eureka server上

eureka集群
修改eureka server application.yml文件
--- spring: profiles: peer1 server: port: 8761 eureka: instance: hostname: peer1 client: serviceUrl: defaultZone: http://peer2:8762/eureka/ --- spring: profiles: peer2 server: port: 8762 eureka: instance: hostname: peer2 client: serviceUrl: defaultZone: http://peer1:8761/eureka/
修改hosts文件 C:\Windows\System32\drivers\etc\hosts
127.0.0.1 peer1 127.0.0.1 peer2
启动两个不同的实例,eclipse 工具如下:

测试:DSReplicas出现peer2说明配置成功

浙公网安备 33010602011771号