SpringCloudEureka上篇

SpringCloudEureka上篇

  • 本文学习自<<重新定义SpringCloud>>
Eureka简介
  • Eureka是Netflix公司开源的一款服务发现组件,该组件提供的服务发现可以为负载均衡、failover等提供支持。Eureka包括 Eureka Server 和 Eureka Client。Eureka Server提供REST服务,而Eureka Client则是使用Java编写的客户端,用于简化与Eureka Server的交互。


服务发现技术选型
  • Jason Wilder 在2014年2月的时候写了一篇博客<>,总结了当时市场上的几类服务发现组件
名称 类型 AP或者CP 语言 依赖 集成 一致性算法
Zookeeper General CP Java JVM Client Binding Paxos
Doozer General CP Go Client Binding Paxos
Consul General CP Go HTTP / DNS library Raft
Etcd General CP or Mixed(1) Go Client Binding / HTTP Raft
SmartStack Dedicated AP Ruby Zookeeper / Haproxy Sidekick(never / synapse)
Eureka Dedicated AP Java JVM Java Client
NSQ(lookupd) Dedicated AP Go Client Binding
Serf Dedicated AP Go Local CLI
Spotify Dedicated AP N / A Bind DNS library
SkyDNS Dedicated Mixed(2) Go DNS library / HTTP
  • 从列表上来看,有很多服务发现组件可以选择,针对AP或者CP,主要选Eureka和Consul作为代表。关于Eureka和Conusl的区别,在Consul的官网有一个很好的阐述(https://www.consul.io/docs/intro/vs/eureka),具体如下:
    • Eureka Server 端采用的是P2P的复制模式,但是它不保证复制操作的一定能成功,因此它提供的是一个最终一次性 服务实例视图;Clinet端在Server端的注册信息有一个带期限的租约,一旦Server在指定期间没有收到Client端发送的心跳,则Server端会认为Client端注册的服务是不健康的,定时任务会将其从注册表中删除。Consul和Eureka不同,Consul采用Raft算法,可以提供强一致性的保证,Consul的 agent 相当于 Netflix Ribbon 和NetFlix Eureka Clinet,而且对应于来说相对透明,同时相对于Eureka这种集中式的心跳检测机制,Consul的 agent 可以参与到基于gossip协议的健康检查,分散了Server端的心跳检测压力,除此之外Consul为多数据中心提供了开销即用的原生支持等。那么基于什么样的因素可以考虑Eureka呢?
      • 选择AP不是CP的,详细后文会提起。
      • 如果团队是Java体系的,则偏好Java语言开发的,技术体系行比较统一,出问题也比较好排查修复,对组件的掌控力比较强,方便扩展维护。
      • 更主要的是Eureka是Netflix开源套件的一部分,跟Zuul,Ribbon等整合的比较好。

Eureka入门案例
Eureka Client
  • 启动类

    package com.example.demochenspringcloudeurekaclient;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.web.ConditionalOnEnabledResourceChain;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    @SpringBootApplication
    @EnableDiscoveryClient 
    public class ChenSpringCloudEurekaClientApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ChenSpringCloudEurekaClientApplication.class, args);
        }
    
    }
    
    
  • 配置文件

    server:
      port: 9091
    
    spring:
      application:
        name: chen-eureka-client
    
    eureka:
      client:
        serviceUrl:   ## 服务端地址
          defaultZone: http://localhost:9092/eureka/
    

Eureka Server
  • 启动类

    package com.example.demochenspringcloudeurekacserver;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    @SpringBootApplication
    @EnableEurekaServer
    public class DemochenSpringCloudEurekaServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemochenSpringCloudEurekaServerApplication.class, args);
        }
    
    }
    
    
  • 配置文件

    spring:
      profiles:
        active: local
      jackson:
        serialization:
          FAIL_ON_EMPTY_BEANS: false
    eureka:
      server:
        use-read-only-response-cache: false
        response-cache-auto-expiration-in-seconds: 10
    management:
      endpoints:
        web:
          exposure:
            include: '*'
    ## 这是两个配置文件,依此为分割
    server:
      port: 9092
    
    eureka:
      instance:
        hostname: localhost
      client:
        registerWithEureka: false
        fetchRegistry: false
        serviceUrl:
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      server:
        waitTimeInMsWhenSyncEmpty: 0
        enableSelfPreservation: false
    
代码演示详解
  • 分别启动Eureka Server 和Eureka Client,然后访问Eureka Server的地址(http://localhost:9092/),这便是Eureka的注册中心

Eureka Server的REST API简介
  • 上文已经介绍了最基本的Eureka Server 和Eureka Client,它是SpringCloud生态里面“服务注册和发现”的一个缩影,这里举例的是使用Java语言的Client端的例子,但实际Eureka Server提供了 REST API,允许非Java语言的其他应用服务通过HTTP REST 的方式接入Eureka服务中。

    • REST API 列表

      • SpringCloud Netflix Eureka跟SpringBoot整合后,提供的REST PAI 与原始的REST API有一点点不同,其路径中的{version}值固定为eureka,其他变化不大

    • 访问效果图

posted @ 2022-01-25 18:35  unknown-n2  阅读(38)  评论(0编辑  收藏  举报