Spring Cloud(二):服务注册与发现Eureka

1、介绍

对于微服务的治理而言,其核心就是服务的注册和发现。在SpringCloud 中提供了多种服务注册与发现组件:Eureka,Consul,Zookeeper。官方推荐使用Eureka。
说明:Eureka是Netflix开源的一款提供服务注册和发现的产品,它提供了完整的Service Registry和Service Discovery实现。也是springcloud体系中最重要最核心的组件之一。管理的服务包含了Spring Cloud的其他服务组件如:熔断、负载、降级等

2、理解

以往服务间资源的获取都是通过相互调用,比如A获取B相关资源,A调用B提供的API获取相关资源 。加入Eureka之后,则B提供服务资源需要在Eureka服务中心注册一遍,A获取服务资源也需要在Eureka服务中心注册一遍,从而获取B服务的资源。监控Eureka服务中心可以监控AB服务调用的使用情况。

3、核心内容

上文说过,对于服务治理而言,其核心部分就是服务的发现与注册,而Spring Cloud推荐使用的是Eureka。因为在分布式系统中,有一个CAP定理,即 Consistency(一致性)、 Availability(可用性)、Partition tolerance(分区容错性),三者不可兼得。Zookeeper是Hadoop的子项目,它的作用也多作为服务的发现与注册。 Zookeeper在CAP定理中满足的CP,也就是一致性和分区容错性,但是它不能保证服务的可用性。比如,服务消费者通过注册列表获取数据时,倘若,Zookeeper正在选主导致服务不可用,亦或者大多数服务宕机。在一般分布式系统的数据存储场景,数据一致性应该是首先被保证的。然而在服务发现的场景中,服务消费者能够消费才是首先保证的,即服务的可用性才是首先保证。

3.1、Eureka组件

Eureka由多个instance(服务实例)组成,这些服务实例可以分为两种:Eureka Server和Eureka Client。为了便于理解,我们将Eureka client再分为Service Provider(服务提供者)和Service Consumer(服务消费者)。
Eureka Server:服务的注册中心,负责维护注册的服务列表。
Service Provider:服务提供方,作为一个Eureka Client,向Eureka Server做服务注册、续约和下线等操作,注册的主要数据包括服务名、机器ip、端口号、域名等等。
Service Consumer:服务消费方,作为一个Eureka Client,向Eureka Server获取Service Provider的注册信息,并通过远程调用与Service Provider进行通信。
这里写图片描述

3.2、Eureka Server

Eureka Server作为一个独立的部署单元,以REST API的形式为服务实例提供了注册、管理和查询等操作。同时,Eureka Server也为我们提供了可视化的监控页面,可以直观地看到各个Eureka Server当前的运行状态和所有已注册服务的情况。

Eureka Server节点启动后,会首先尝试从邻近节点获取所有实例注册表信息,完成初始化。Eureka Server通过getEurekaServiceUrls()方法获取所有的节点,并且会通过心跳续约的方式定期更新。 默认配置下,如果Eureka Server在一定时间内没有接收到某个服务实例的心跳,Eureka Server将会注销该实例(默认为90秒,通过eureka.instance.lease-expiration-duration-in-seconds配置)。当Eureka Server节点在短时间内丢失过多的心跳时(比如发生了网络分区故障),那么这个节点就会进入自我保护模式。

自我保护模式: 默认配置下,如果Eureka Server每分钟收到心跳续约的数量低于一个阈值(instance的数量(60/每个instance的心跳间隔秒数)自我保护系数),并且持续15分钟,就会触发自我保护。在自我保护模式中,Eureka Server会保护服务注册表中的信息,不再注销任何服务实例。当它收到的心跳数重新恢复到阈值以上时,该Eureka Server节点就会自动退出自我保护模式。它的设计理念就是宁可保留错误的服务注册信息,也不盲目注销任何可能健康的服务实例。该模式可以通过eureka.server.enable-self-preservation = false来禁用,同时eureka.instance.lease-renewal-interval-in-seconds可以用来更改心跳间隔,eureka.server.renewal-percent-threshold可以用来修改自我保护系数(默认0.85)。

3.3、Eureka Client

服务注册 :启动时,会调用服务注册方法,向Eureka Server注册自己的信息。Eureka Server会维护一个已注册服务的列表。当实例状态发生变化时(如自身检测认为Down的时候),也会向Eureka Server更新自己的服务状态,同时用replicateToPeers()向其它Eureka Server节点做状态同步。

续约与剔除 :服务实例启动后,会周期性地向Eureka Server发送心跳以续约自己的信息,避免自己的注册信息被剔除。续约的方式与服务注册基本一致,首先更新自身状态,再同步到其它Peer。如果Eureka Server在一段时间内没有接收到某个微服务节点的心跳,Eureka Server将会注销该微服务节点(自我保护模式除外)。

服务消费 :Service Consumer本质上也是一个Eureka Client。它启动后,会从Eureka Server上获取所有实例的注册信息,包括IP地址、端口等,并缓存到本地。这些信息默认每30秒更新一次。前文提到过,如果与Eureka Server通信中断,Service Consumer仍然可以通过本地缓存与Service Provider通信。

三处缓存:
Eureka Server对注册列表进行缓存,默认时间为30s。
Eureka Client对获取到的注册信息进行缓存,默认时间为30s。
Ribbon会从上面提到的Eureka Client获取服务列表,将负载均衡后的结果缓存30s。

4、代码实现

我们做三个角色
Eureka Server :提供服务注册和发现;
Service Provider :服务提供方,将自身服务注册到Eureka,从而使服务消费方能够找到;
Service Consumer: 服务消费方,从Eureka获取注册服务列表,从而能够消费服务。

5、Eureka Server

maven项目的pom.xml文件

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR2</spring-cloud.version>
</properties>

<dependencies>
<!--Eureka服务端-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</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>

项目的启动配置文件:application.properties

spring.application.name=eureka-server
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

配置说明:其中serviceUrl代表注册中心,集群多个,逗号分隔,fetch-registry(默认开启)表示从注册中中心获取服务;register-with-eureka(默认开启):是否将自身注册到服务中心。

开启Eureka服务:

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

浏览器访问Eureka后台:http://localhost:8761/
eureka无服务注册

箭头方向可以看出目前服务注册中心没有实例

6、Eureka Provider

maven项目的pom.xml文件:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR2</spring-cloud.version>
</properties>

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

<!-- spring boot实现Java Web服务-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</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>

项目启动配置文件application.properties:

#应用(服务)名称
spring.application.name=eureka-provider
server.port=8762                                                    eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

注解@EnableDiscoveryClient服务化:

@EnableDiscoveryClient
@SpringBootApplication
public class EurekaProviderApplication {

public static void main(String[] args) {
new SpringApplicationBuilder(EurekaProviderApplication.class).web(true).run(args);

}}

访问控制器:

@RestController
public class ProviderNode1Controller {

@GetMapping({"","/"})
public String index(){
return "Hi,dy_bom! this is  provider-node1 of peer!";
 }

查看注册中心后台 :
服务注册
可以看到服务提供者已经成功的注册到了服务中心。

说明:图中红色警告是因为Eureka开启了自我保护机制,Eureka Server在运行期间,会统计心跳失败的比例在15分钟之内是否低于85%,我这里是因为之前没有开启BUS服务的rabbit造成的,你也可以设置自我保护enable-self-preservation为false

7、Eureka与Spring Cloud Security 结合做权限认证

在pom.xml中加入Security的依赖

 <!--权限认证-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

properties定义权限的用户名密码

#权限认证(默认就是开启的)
security:
  basic:
    enabled: true
  user:
    name: eureka
    password: 123456

访问Eureka的后台,需要输入对应的用户名密码
这里写图片描述

8、Eureka集群

properties配置文件

#对等意识,集群Eureka服务器配置
---
spring:
  application:
    name: spring-cloud-eureka
  profiles: peer1
server:
  port: 8001
    eureka:
  instance:
    hostname: peer1
  client:
serviceUrl:defaultZone: http://peer2:8002/eureka/,http://peer3:8003/eureka/
---
spring:
  application:
    name: spring-cloud-eureka
  profiles: peer2
server:
  port: 8002
eureka:
  instance:
    hostname: peer2
  client:
serviceUrl:
  defaultZone: http://peer1:8001/eureka/,http://peer3:8003/eureka/
---
spring:
  application:
    name: spring-cloud-eureka
  profiles: peer3
server:
  port: 8003
eureka:
  instance:
    hostname: peer3
  client:
serviceUrl:
  defaultZone: http://peer1:8001/eureka/,http://peer2:8001/eureka/

启动后,我们可以看到三个节点是两两互相注册的。各个节点没有主次之分,对等。

代码地址:https://github.com/rubenYuan/Spring-Cloud-Samples

PPT:http://download.csdn.net/download/ruben95001/9974839

上一篇:SpringCloud前言概述
下一篇:服务消费ribbon

猜你喜欢

转载自blog.csdn.net/ruben95001/article/details/77198419