简介
用作注册中心的,类似zk。zk实现的是CP,eureka实现的是AP。因为注册中心数据变化不大,而数据可靠性要求较高,所以eureka更适合做注册中心。
使用方式
1.pom文件中增加eureka依赖
因为它是一个纯java的项目,所以需要容器启动,目前基本上都是基于springboot项目启动
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2.spring application启动类中增加注解
@EnableEurekaServer
3.application.yml中增加配置
server:
port: 8761
spring:
application:
name: eureka-master
eureka:
instance:
hostname: localhost
prefer-ip-address: true
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
4.集群配置
如果是集群版的,不能用ip,且application.name必须是同一个,register-with-eureka、fetch-registry必须设置成true,参考配置,底部有配置好的附件
spring:
profiles:
- node1
application:
name: eureka-master
eureka:
instance:
hostname: eureka-master1
prefer-ip-address: false
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka-master2:8762/eureka/,http://eureka-master3:8763/eureka/
server:
port: 8761
---
spring:
profiles:
- node2
application:
name: eureka-master
eureka:
instance:
hostname: eureka-master2
prefer-ip-address: false
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka-master1:8761/eureka/,http://eureka-master3:8763/eureka/
server:
port: 8762
---
spring:
profiles:
- node3
application:
name: eureka-master
eureka:
instance:
hostname: eureka-master3
prefer-ip-address: false
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka-master1:8761/eureka/,http://eureka-master2:8762/eureka/
server:
port: 8763
网友评论