马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
Eureka先容与使用教程
你好,我是悦创。
Eureka 是 Netflix 开辟的一款服务发现(Service Discovery)工具,它紧张用于云中基于微服务架构的应用步伐。Eureka使服务实例可以大概动态地注册自己,而其他服务实例可以通过 Eureka 发现并毗连到它们。这种动态服务注册与发现机制,是构建高度可扩展的微服务架构的关键构成部分。
Eureka的紧张构成部分
- Eureka Server: 服务注册中心。服务实例注册其元数据到Eureka Server,别的服务实例通过Eureka Server来发现服务。
- Eureka Client: 通常嵌入在须要举行服务注册与发现的应用步伐中。Eureka Client负责维护心跳(心跳机制包管服务实例的存活状态)、服务注册、服务发现等功能。
Eureka的工作原理
- 服务注册: 启动后,Eureka客户端向注册中心注册自己的服务,并定期发送心跳来更新其状态。
- 服务发现: 服务斲丧者使用Eureka客户端从Eureka服务器查询可用服务,并获取服务相干信息来举行远程调用。
- 服务下线: 当服务实例关闭时,它会向Eureka服务器发送下线哀求,注册中心将该实例从其列表中移除。
- 故障监测: Eureka Server在没有收到某个服务实例的心跳后,会将该实例从服务列表中移除。
怎样使用Eureka
以下是使用Spring Cloud与Eureka举行服务注册与发现的步调:
步调 1: 设置Eureka Server
- 添加依靠: 在Spring Boot项目标pom.xml中添加Eureka Server的依靠。
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
- </dependency>
- </dependencies>
复制代码 在application.yml中设置Eureka Server。- server:
- port: 8761
- eureka:
- client:
- registerWithEureka: false
- fetchRegistry: false
- server:
- enableSelfPreservation: false
复制代码 在Spring Boot的启动类中,通过@EnableEurekaServer注解激活Eureka Server。- @SpringBootApplication
- @EnableEurekaServer
- public class EurekaServerApplication {
- public static void main(String[] args) {
- SpringApplication.run(EurekaServerApplication.class, args);
- }
- }
复制代码 步调 2: 设置Eureka Client
在须要注册的微服务的 pom.xml 中添加 Eureka Client 的依靠。- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
复制代码
- 设置客户端:
在application.yml中设置 Eureka Client。
- eureka:
- client:
- serviceUrl:
- defaultZone: http://localhost:8761/eureka/
复制代码 在微服务的启动类中,通过 @EnableEurekaClient 注解激活 Eureka Client。- @SpringBootApplication
- @EnableEurekaClient
- public class ProductServiceApplication {
- public static void main(String[] args) {
- SpringApplication.run(ProductServiceApplication.class, args);
- }
- }
复制代码 通过这些步调,你可以设置一个根本的 Eureka Server 和 Client,用于微服务的注册和发现。使用 Eureka 可以极大地增强微服务架构的动态性和结实性。
|