1、Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。
SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。
2、Eureka包含两个组件:Eureka Server和Eureka Client。
在应用启动后,将会向Eureka Server发送心跳,默认周期为30秒,如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,Eureka Server将会从服务注册表中把这个服务节点移除(默认90秒)。
Eureka通过心跳检查、客户端缓存等机制,确保了系统的高可用性、灵活性和可伸缩性。
二、Eureka搭建 2.1、Eureka Server搭建新建springboot项目neil-eureka-server。
在pom.xml里面添加Eureka的依赖
org.springframework.cloud spring-cloud-starter-netflix-eureka-server
配置文件application.yml
server:
port: 8989
spring:
application:
name: neil-eureka-server
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8989/eureka/
fetch-registry: true
register-with-eureka: true
instance:
prefer-ip-address: true
springboot启动类里面添加@EnableEurekaServer注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class NeilEurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(NeilEurekaServerApplication.class, args);
}
}
启动neil-eureka-server
在浏览器地址栏输入机器ip加端口访问Eureka页面
http://127.0.0.1:8989/
新建springboot项目neil-eureka-client。
在pom.xml里面添加Eureka的依赖。
org.springframework.cloud spring-cloud-starter-netflix-eureka-client
配置文件application.yml
server:
port: 8990
servlet:
context-path: /neil-eureka-client
spring:
application:
name: neil-eureka-client
eureka:
client:
service-url:
defaultZone: http://localhost:8989/eureka/ #Eureka Server地址
instance:
prefer-ip-address: true #Hostname使用主机ip
springboot启动类里面添加@EnableEurekaClient注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class ProductApplication {
public static void main(String[] args) {
SpringApplication.run(ProductApplication.class, args);
}
}
启动neil-eureka-client
在浏览器地址栏输入机器ip加端口访问Eureka页面,查看是否注册上Eureka了。
http://127.0.0.1:8989/
至此,Eureka的服务端和客户端就搭建完成了!



