Eureka服务注册与实现

作者: gona | 来源:发表于2020-10-19 21:40 被阅读0次

    Eureka服务注册与实现

    什么是Eureka

    • Netflix在设计Eureka时遵循的就是AP原则
    • Eureka是Netflix的一个子模块,也是核心模块之一。Eureka是一个基于REST的服务,用于定位服务,以实现云端中间层服务发现和故障转移,服务注册与发现对于微服务来设是非常重要的,有了服务发现于注册,只需要使用服务的标识符,就可以访问到服务,而不需要修改服务调用的配置文件了,功能类似于Dubbo的注册中心,比如Zookeeper;

    原理讲解

    4.png
    • Eureka的基本架构
      • springCould封装NetFlix公司开发的Eureka模块来实现服务注册和发现
      • Eureka采用了C-S的架构设计,EurekaServer作为服务注册功能的服务器,他是服务注册中心
      • 而系统中的其他微服务。使用Eureka的客户端连接到EurekaServer并维持心跳连接。这样系统的维护人员就可以通过EurekaServer来监控系统中各个微服务是否正常运行,SpringCloud的一些其他模块(比如Zuul)就可以通过EurekaServer来发现系统中的其他微服务,并执行相关的逻辑;
      • Eureka包含两个组件:Eureka Server 和Eureka Client
      • EurekaServer提供服务注册服务,各个节点启动后,会再EurekaServer中进行注册,这样EurekaServer中的服务注册表中将会存所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到。
      • Eureka Client是一个Java客户端,用于简化EurekaServer的交互,客户端同时也具备一个内置的,使用轮询负载算法的负载均衡器。在应用启动后,将会向EurekaServer发送心跳(默认周期为30秒)。如果EurekaServer在多个心跳周期内没有接收到某个节点的心跳,EurekaServer将会从服务注册表中把这个服务节点移除掉(默认为90秒)
    • 三大角色
      • Eureka Server:提供服务的注册与发现。
      • Service Provider:将自身服务注册到Eureka中,从而使消费方能够找到。
      • Service Consumer:服务消费方从Eureka中获取注册服务列表,从而找到消费服务。

    eureka使用

    导入依赖

    <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>jgnspringcloud</artifactId>
            <groupId>com.jgn</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>springcloud-eureka-7001</artifactId>
        <!-- 导包 -->
        <dependencies>
            <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka-server</artifactId>
                <version>1.3.6.RELEASE</version>
            </dependency>
            <!-- 热部署工具 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
            </dependency>
    
        </dependencies>
    
    </project>
    

    对eureka进行配置

    server:
      port: 7001
    #Eureka配置
    eureka:
      instance:
        hostname: localhost #Eureka服务端的实例名称
      client:
        register-with-eureka: false #表示是否向eureka注册中心注册自己
        fetch-registry: false #fetch-registry如果为false,则表示自己为注册中心
        service-url: #监控页面 不配置默认访问端口8761
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #动态配置
    

    编写启动类

    package com.jgn.springcloud;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    // 启动之后,访问http://localhost:7001/
    @SpringBootApplication
    @EnableEurekaServer // EnableEurekaServer 服务端的启动类,可以接受别人注册进来
    public class EurekaServer_7001 {
        public static void main(String[] args) {
            SpringApplication.run(EurekaServer_7001.class,args);
        }
    }
    

    注册

    在要注册的module的依赖中加入eureka相关依赖

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
        <version>1.4.6.RELEASE</version>
    </dependency>
    

    在该module的applction.yml文件中配置,让defaultZone与注册中心保存一致。

    #Eureka的配置,服务注册到哪里
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:7001/eureka/
    

    在该module的启动类添加注解来启动注册

    package com.jgn.springcloud;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    @SpringBootApplication
    @EnableEurekaClient
    public class DeptProvider_8001 {
        public static void main(String[] args) {
            SpringApplication.run(DeptProvider_8001.class,args);
        }
    }
    

    测试,先启动服务端,再启动客户端

    5.png

    Eureka集群

    分别配置三个eurekaServer,使用不同的端口号,分别为7001,7002,7003

    6.png

    在各自的配置文件进行配置

    server:
      port: 7001
    #Eureka配置
    eureka:
      instance:
        hostname: eureka7001.com #Eureka服务端的实例名称
      client:
        register-with-eureka: false #表示是否向eureka注册中心注册自己
        fetch-registry: false #fetch-registry如果为false,则表示自己为注册中心
        service-url: #监控页面 不配置默认访问端口8761
          defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ #动态配置
          # 单机 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #动态配置
          # 集群(管连) defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #动态配置
    
    server:
      port: 7002
    #Eureka配置
    eureka:
      instance:
        hostname: eureka7002.com #Eureka服务端的实例名称
      client:
        register-with-eureka: false #表示是否向eureka注册中心注册自己
        fetch-registry: false #fetch-registry如果为false,则表示自己为注册中心
        service-url: #监控页面 不配置默认访问端口8761
          defaultZone: http://eureka7003.com:7003/eureka/,http://eureka7001.com:7001/eureka/ #动态配置
    
    server:
      port: 7003
    #Eureka配置
    eureka:
      instance:
        hostname: eureka7003.com #Eureka服务端的实例名称
      client:
        register-with-eureka: false #表示是否向eureka注册中心注册自己
        fetch-registry: false #fetch-registry如果为false,则表示自己为注册中心
        service-url: #监控页面 不配置默认访问端口8761
          defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/ #动态配置
    

    将提供者provider在集群中完成注册,修改application.yml

    #Eureka的配置,服务注册到哪里
    eureka:
      client:
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
      instance:
        instance-id: springcloud-provider-dept8001 #修改eureka上的默认描述信息
    

    CAP原则

    ACID:RDBMS(MySQL、Oracle、sqlServer)

    • A(Atomicity)原子性
    • C(Consistency)一致性
    • I(Isolation)隔离性
    • D(durability)持久性

    CAP:NoSQL(redis、mongdb)

    • C(Consistency)强一致性
    • A(Availability)可用性
    • P(Partition tolerance)分区容错性

    CAP的三进二:CA、AP、CP

    CAP的核心

    一个分布式系统不可能同时很好的满足一致性、可用性和分区容错性这三个需求

    根据CAP原理,将NoSQL数据库分成了满足CA原则、满足CP原则和满足AP原则三大类:

    • CA:单点集群,满足一致性、可用性的系统,通常可扩展性较差
    • CP:满足一致性,分区容错性的系统,通常性能不是特别高
    • AP:满足可用性,分区容错性的系统,通常可能对一致性要求低一些

    zookeeper保证的是CP

    Eureka保证的是AP

    相关文章

      网友评论

        本文标题:Eureka服务注册与实现

        本文链接:https://www.haomeiwen.com/subject/zxilmktx.html