依赖
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
implementation 'org.springframework.boot:spring-boot-starter-freemarker'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
- 新版的需要引入freemarker否则eureka页面出不来
配置文件
server:
port: 2000
eureka:
instance:
hostname: localhost
client:
#是否向注册中心注册本身服务,
#由于本身是注册中心单例模式设置false
#集群模式设置为true将自己注册到注册中心
registerWithEureka: true
fetchRegistry: true
serviceUrl:
#集群模式下注册中心地址 用,分开
defaultZone: http://localhost:2000/eureka/,http://localhost:2001/eureka/
spring:
freemarker:
prefer-file-system-access: false
application:
name: eureka-server-2000
- 其他的节点配置类似,只要改个ip地址或者端口号即可
- 集群模式下注册中心会相互注册,即使挂了一个其他的也还能用
- 注册中心启动之后,会相互复制信息,所以即使客户端之注册到其中一个注册中心,经过少许时间也最终会注册到集群中所有的注册中心
开启eureka
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerProjectApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerProjectApplication.class, args);
}
}
客户端依赖和配置
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
@EnableEurekaClient
server:
port: 3001
spring:
application:
name: hystrix-project
eureka:
client:
service-url:
defaultZone: http://localhost:2000/eureka/
网友评论