单节点搭建
注意:在使用Eureka之前应将本机防火墙关闭。可在 控制面板\系统和安全\Windows 防火墙 中关闭。
在spring cloud中Eureka也是一个微服务,搭建过程和普通微服务开发过程相比更加简洁。
pom依赖
<!--Eureka服务端端依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
yml配置
hostname: 对应hosts文件中添加的值
server:
port: 7001
eureka:
instance:
hostname: localhost
client:
#false表示不向注册中心注册自己
register-with-eureka: false
#false表示自己端就是注册中心
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
server:
#关闭自我保护机制
enable-self-preservation: false
#检测时间改为2000毫秒
eviction-interval-timer-in-ms: 2000
主启动类
主启动类上添加@EnableEurekaServer注解
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
@EnableEurekaServer
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class,args);
}
}
运行结果
补充:控制台参数说明:https://www.cnblogs.com/linjiqin/p/10087462.html
集群搭建
在Windows系统下需要修改hosts文件添加如下信息
修改yml配置文件
对hostname和defauktZone进行修改
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com
#hostname: localhost
client:
#false表示不向注册中心注册自己
register-with-eureka: false
#false表示自己端就是注册中心
fetch-registry: false
service-url:
#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
defaultZone: http://eureka7002.com:7002/eureka
server:
#关闭自我保护机制
enable-self-preservation: false
#检测时间改为2000毫秒
eviction-interval-timer-in-ms: 2000
服务启动后:
微服务进驻Eureka
在需要进驻Eureka的微服务的pom依赖中添加Eureka客户端依赖
<!--Eureka客户端依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
yml配置
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka
#defaultZone: http://localhost:7001/eureka,http://eureka7002.com:7002/eureka
主启动类
添加@EnableEurekaClient注解
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
@EnableEurekaClient
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class);
}
}