Eureka初识以及搭建

一、服务发现概述

1、服务发现由来

服务发现及注册中心(统一称为服务发现),是跟随软件开发的架构方式而逐步演变而来的

早期的单体架构

对于外部的依赖,通常是采取调用域名接口的形式,例如短信发送接口,就会通过appId和appKey去调用相应的域名,完成短信的发送。

SOA架构发展

单体架构被拆分成粒度较粗的服务化架构,此时内部的依赖就比较多,那么是如何进行服务间的调用呢?以http暴露服务的形式来说,将服务A部署在多台虚拟机上,服务B通过调用服务A的ip地址来进行调用:1、服务A的ip由服务B来维护,通过配置nginx的upstream将服务A的所有实例ip写进去,但这样也暴露了服务A一些实现的细节且A和B不能解耦;2、服务B通过调用服务A的内部域名,而服务A的所有实例ip由自己的nginx维护。

微服务时代

由于docker的崛起,导致服务A的ip并不是固定的,因此上面的做法就比较麻烦了,有两个方案:1、在部署的时候更新nginx的文件,使用ngx_http_dyups_module通过rest api去更新nginx的upstream而不需要reload;2、将服务注册中心作为一个标配组件,网关等组件通过服务注册中心获取实例的相关信息,实现动态路由。

2、Eureka简介

什么是Eureka呢

官网中给出的解释:

Eureka is a REST (Representational State Transfer) based service that
is primarily used in the AWS cloud for locating services for the
purpose of load balancing and failover of middle-tier servers. We call
this service, the Eureka Server. Eureka also comes with a Java-based
client component,the Eureka Client, which makes interactions with the
service much easier. The client also has a built-in load balancer that
does basic round-robin load balancing. At Netflix, a much more
sophisticated load balancer wraps Eureka to provide weighted load
balancing based on several factors like traffic, resource usage, error
conditions etc to provide superior resiliency.

其实可以用一句话概括:Eureka是基于REST的服务,用于定位服务,以实现云端中间层服务发现和故障转移

二、分布式系统CAP原则

1、什么是CAP原则

CAP原则是分布式系统的设计原则
C:Consistency强一致性
A:Availability可用性
P:Partition tolerance分区容错性
CAP 原则指的是,这三个要素最多只能同时实现两点,不可能三者兼顾

2、Eureka和Zookeeper的比较

同样作为服务注册中心,我们来比较一下Eureka和Zookeeper之间的区别(这里并不是说哪种较优越,只能说不同的场景应当选择适合自己的组件):

Zookeeper选用的是CP

Zookeeper采用的选举机制,当master不可用时,必须要选举出一个follower来担任master的职责,但是选举的时间比较久,而在这期间整个集群都处于不可用的状态,所以达不到Availability高可用性。

Eureka选用的是AP

Eureka所有的节点都是平等的,只要不是所有的节点都down掉,那个就会将请求转移到正常的节点服务,但可能获取的实例信息不是最新的(自我保护机制),所以不能保证Consistency强一致性。

三、Eureka架构概述

Eureka是基于C-S架构:Eureka server和Eureka client
架构图

四、Eureka的自我保护机制

总的来说,在某一时刻的某个微服务不可用,Eureka不会立即清除,而是依然会对该微服务的实例信息进行保存。
在默认情况下,如果Eureka在一定时间内没有收到某个微服务的实例心跳,那么就会将该实例清除掉,这种行为是非常危险的,因为该微服务实例可能由于网络波动的原因而导致无法与Eureka进行通讯,但此时服务本身是健康的。而自我保护机制就是为了解决这个问题,当Eureka在短时间内丢失过多的微服务实例时,就会进入自我保护模式,不再删除注册表中的信息,直到网络恢复退出自我保护模式(收到的心跳数达到阈值)。
设计哲学:宁可保留错误的服务注册信息,也不可盲目地删除任何一个可能健康的服务实例。

五、Eureka工程搭建

工程搭建相当简单

项目结构

项目结构

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>eureka01</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka01</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

spring-cloud-starter-netflix-eureka-server引入Eureka Server

主启动类Eureka01Application
package com.example.eureka01;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class Eureka01Application {

    public static void main(String[] args) {
        SpringApplication.run(Eureka01Application.class, args);
    }

}

在启动类中添加注解@EnableEurekaServer,开启Eureka Server

ymal配置文件
server:
  servlet:
    context-path: /eureka01
  port: 8080

spring:
  application:
    name: eureka

eureka:
  instance:
    prefer-ip-address: true #是否使用ip而不是hostname注册
    instance-id: eureka01
  client:
    #是否向注册中心注册自己,如果不使用defaultZone就必须使用注册参数和检索参数作为高可用配置
    register-with-eureka: true
    #检索服务,eureka的职责是维护服务
    fetch-registry: true
    #是否偏好使用处于相同zone的服务提供者
    prefer-same-zone-eureka: true
    # 可以理解成地区
    region: guangzhou
    #可以理解成可用的机房
    availability-zones:
      #region和zone的对应关系
      guangzhou: zone-1,zone-2
    service-url:
      #eureka server对外暴露的服务注册和服务发现地址
      #defaultZone就是一个可用区域
      #defaultZone: http://localhost:8080/eureka01/eureka/,http://localhost:8081/eureka02/eureka/
      # 分机房
      zone-1: http://localhost:8080/eureka01/eureka/
      zone-2: http://localhost:8081/eureka02/eureka/
  server:
    #是否开启自我保护机制,默认开启
    enable-self-preservation: true

各个配置项都有注释,不多说,主要说一下zone区域以及region,region可以理解为地区,而zone可以理解为机房,这个在后面的篇幅会说,当然可以直接使用defaultZone来暴露注册中心的地址,由于我是使用register-with-eureka和fetch-registry来做Eureka之间的通讯,所以两个注册中心会相互注册。

项目启动

Eureka注册中心
项目地址:https://github.com/IvanChiu0/Eureka

后续

后面会有两章篇幅来分别谈一下:Eureka的配置详解和Eureka的源码解读。

发布了6 篇原创文章 · 获赞 1 · 访问量 7238

猜你喜欢

转载自blog.csdn.net/qq_34864092/article/details/103863758