Spring cloud 注册中心使用简介

Spring cloud 注册中心使用简介

注册中心是服务发现的核心。它保存了各个可用服务实例的网络地址(IP Address和Port)。服务注册中心必须要有高可用性和实时更新功能。 Netflix Eureka 就是一个服务注册中心。它提供了服务注册和查询服务信息的REST API。服务通过使用POST请求注册自己的IP Address和Port。每30秒(默认)发送一个PUT请求刷新注册信息。通过DELETE请求注销服务。客户端通过GET请求获取可用的服务实例信息。

实现注册中心的不同技术

Spring cloud的服务注册及发现支持eureka、Zookeeper和Consul。
这里写图片描述
三种技术实现是根据CAP理论(三种特性:Consistency(一致性) 、Availability(可用性)、Partition tolerance(分区容错性),在分布式设计中只能选其二)的取舍进行设计的,具体特性如下:
# eureka 是按照AP原则设计,作为分布式场景下的服务发现的产品较为合适,服务发现场景的可用性优先级较高,一致性并不是特别致命。各个服务可以单独提供服务,不需要发起选举;
# zookeeper 是按照CP原则设计,牺牲可用性,在服务发现场景并没太大优势,需要选举,在选举过程中服务不可用;
# Consul 是按照CA原则设计,为保证数据一致性,需要发起选举,在选举过程中服务不可用。

注册中心管理原理

这里写图片描述
所有的服务端及访问服务的客户端都需要连接到注册管理器(eureka服务器)。服务在启动时会自动注册自己到eureka服务器,每一个服务都有一个名字,这个名字会被注册到eureka服务器。使用服务的一方只需要使用该名字加上方法名就可以调用到服务。

实现Eureka的核心技术

使用 Jersey 框架实现自身的 RESTful HTTP接口;
使用 HTTP 协议实现服务注册和peer同步;
使用 JDK 自带 Timer 实现定时任务(发送心跳、定时清理过期服务、节点同步等) ;
使用guava (Google)包实现内存缓存。

属性配置与注解

  • 配置

服务注册中心
eg: eureka.server.enable-self-preservation=true/false Eureka的自我保护模式开关
服务实例类
eg: eureka.instance.appname=dal-service-1 服务名,默认取 spring.application.name 配置值,如果没有则为 unknown
服务注册类
eg: eureka.client. service-url. defaultZone=http//:ip:port/eureka 指定服务注册中心地址

  • 注解

@EnableEurekaServer 该注解表明应用为eureka服务,可以联合多个服务作为集群,对外提供服务注册以及发现功能。
@EnableEurekaClient 该注解表明应用既作为eureka实例又为eureka client 可以发现注册的服务(netflix)
@EnableDiscoveryClient 该注解表明应用既作为eureka实例又为eureka client 可以发现注册的服务 (commons)

注册中心实例

  • 构造三个Eureka Server

    向pom.xml里加入下面的依赖

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

应用主类添加注解 @EnableEurekaServer

@SpringBootApplication
@EnableEurekaServer
public class ServerApplication{
    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class, args);
    }
}

在properties 或 yml 文件添加配置属性
peer1 peer2 peer3 是127.0.0.1 的host 映射

server:
  port: 8010
spring:
  profiles: env1
  application:
    name: mmb-eureka-server
eureka:
  instance:
    hostname: peer1
  server:
    enable-self-preservation: true
  client:
    register-with-eureka: true
    fetch-registry: true
    serviceUrl:
      defaultZone: http://peer1:8010/eureka/,http://peer2:8020/eureka/,http://peer3:8030/eureka/
---
server:
  port: 8020
spring:
  profiles: env2
  application:
    name: mmb-eureka-server
eureka:
  instance:
    hostname: peer2
  server:
    enable-self-preservation: true
  client:
    register-with-eureka: true
    fetch-registry: true
    serviceUrl:
      defaultZone: http://peer1:8010/eureka/,http://peer2:8020/eureka/,http://peer3:8030/eureka/
---
server:
  port: 8030
spring:
  profiles: env3
  application:
    name: mmb-eureka-server
eureka:
  instance:
    hostname: peer3
  server:
    enable-self-preservation: true
  client:
    register-with-eureka: true
    fetch-registry: true
    serviceUrl:
      defaultZone: http://peer1:8010/eureka/,http://peer2:8020/eureka/,http://peer3:8030/eureka/

启动三个服务

  • 构造Eureka client(service)

    向pom.xml里加入下面的依赖

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

应用主类添加注解 @EnableEurekaClient

@SpringBootApplication
@EnableEurekaClient
public class ClientServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(ClientServiceApplication.class, args);
    }
}

在properties 或 yml 文件添加配置属性

server: 
  port: 8014
spring: 
  application:
    name: hello-service
eureka:
  client:
    service-url:
      defaultZone: http://peer1:8010/eureka/
  • 构造Eureka client(consumer)

向pom.xml里加入下面的依赖

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

应用主类添加注解 @EnableEurekaClient

@SpringBootApplication
@EnableEurekaClient
public class ClientServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(ClientServiceApplication.class, args);
    }
}

在properties 或 yml 文件添加配置属性

server: 
  port: 8013
spring: 
  application:
    name: hello-consumer
eureka:
  client:
    service-url:
      defaultZone: http://peer1:8020/eureka/

启动service client 和 consumer client ,consumer就可以通过 service 服务名调用 service restful 接口。

在浏览器中打开:http://peer1:8030/ 可查看注册中心信息
这里写图片描述

### 分析遗留问题

  • 注册中心数据同步实现原理

E、F、G 为注册中心Server;
c1、c2、c3、c4是注册在注册中心的client;
E向F注册,F向G注册,G向E注册。
说明:isReplication(是否是复制的标识) R表示注册 C表示复制
1. c1、c2、c3都注册在E上,c3、c4都在注册在F上,没有直接注册到G上的client
2. Eureka 同步过程,E将直接相关的注册信息复制到F,F将同步c1、c2的注册信息;F则将c3、c4发送到G;G没有直接相关的注册信息,没有信息复制给E。
3. 若c2与E的注册发生异常,则c2将从,E的信息列表中cancel,并且在下一次信息同步的时候将该信息同步给F,F中c2 是复制信息,因此,也执行该cancel操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值