其他分享
首页 > 其他分享> > 微服务架构 | 3.1 Netflix Eureka 注册中心

微服务架构 | 3.1 Netflix Eureka 注册中心

作者:互联网

目录


前言

参考资料
《Spring Microservices in Action》
《Spring Cloud Alibaba 微服务原理与实战》
《B站 尚硅谷 SpringCloud 框架开发教程 周阳》
《Spring Cloud Netflix 官方资料》

Eureka 是 Netflix 开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在 AWS 域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的;

Spring Cloud 将它集成在其子项目 spring-cloud-netflix 中,以实现 Spring Cloud 的服务发现功能;


1. Eureka 基础知识

1.1 Eureka 模型中的服务器与客户端

1.2 Eureka 的 30s 启动机制

1.3 Eureka 为什么注册服务的 IP ,而不是主机名

1.4 Eureka 服务器的高可用(集群)


2. 构建 Eureka 服务器

2.1 引入 pom.xml 依赖

<!-- 1.x老版本的依赖 -->
<dependency>  
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!-- 2.x新版本的依赖 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

2.2 修改 application.yaml 配置文件

server:
  port: 8761 #Eureka 监听的端口
spring:
  application:
    name: eureka-server #Eureka 服务器的名称
eureka:
  client:
    fetch-registry: false #不要在本地缓存注册表信息
    register-with-eureka: false #不使用 Eureka 服务进行注册
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  instance:
    hostname: localhost
  server:  #配置属性,但由于 Eureka 自我保护模式以及心跳周期长的原因,经常会遇到 Eureka Server 不剔除已关停的节点的问题
    enable-self-preservation: false
    eviction-interval-timer-in-ms: 5000
    waitTimelnMsWhenSyncEmpty: 5 #在服务器接收请求之前等待的初始时间,在实际生产中应注释掉此属性

2.3 在主程序类上标注注解

@EnableEurekaServer:表示该服务为 Eureka 服务器;


3. 将客户端注册进 Eureka 服务器

3.1 引入 pom.xml 依赖

<!--注册中心-->
<dependency>  
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

3.2 修改 boostrap.yml 配置文件

spring:
  application:
    name: xxx-client  #应用程序ID,常写在 boostrap.yml 下
  profiles:
    active: dev
  cloud:
    config:
      enabled: true
eureka:
  instance:
    preferIpAddress: true  #注册服务的IP,而不是服务名称
  client:
    registerWithEureka: true  #向 Eureka 注册服务
    fetchRegistry: true  #拉取注册表的本地副本
    serviceUrl: 
      defaultZone: http://localhost:8761/eureka/  #Eureka 服务模块

3.3 在主程序类上标注服务发现的注解


4. Eureka Server 的自我保护

4.1 故障现象

Eureka 的自我保护

4.2 自我保护概述

4.3 自我保护原理

自我保护原理

4.4 关闭自我保护机制



最后

新人制作,如有错误,欢迎指出,感激不尽!
欢迎关注公众号,会分享一些更日常的东西!
如需转载,请标注出处!

标签:服务,Netflix,Server,eureka,3.1,服务器,Eureka,客户端
来源: https://www.cnblogs.com/dlhjw/p/15806074.html