springcloud构架Eureka服务注册与发现(二)

Spring Cloud 封装了 Netflix 公司开发的 Eureka 模块来 实现服务注册和发现(请对比Zookeeper)。   Eureka 采用了 C-S 的设计架构。Eureka Server 作为服务注册功能的服务器,它是服务注册中心。 

  而系统中的其他微服务,使用 Eureka 的客户端连接到 Eureka Server并维持心跳连接。这样系统的维护人员就可以通过 Eureka Server 来监控系统中各个微服务是否正常运行。SpringCloud 的一些其他模块(比如Zuul)就可以通过 Eureka Server 来发现系统中的其他微服务,并执行相关的逻辑。 

请注意和Dubbo的架构对比 

  Eureka包含两个组件:Eureka Server和Eureka Client 

Eureka Server提供服务注册服务 

各个节点启动后,会在EurekaServer中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到 

EurekaClient是一个Java客户端,用于简化Eureka Server的交互,客户端同时也具备一个内置的、使用轮询(round-robin)负载算法的负载均衡器。在应用启动后,将会向Eureka Server发送心跳(默认周期为30秒)。如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,EurekaServer将会从服务注册表中把这个服务节点移除(默认90秒) 

好了现在我们开始动手实现

构建步骤

1.新建servicecloud-eureka-7001 Module

pom 注意springboot2.X开始eureka包名字有所改变(原:spring-cloud-starter-eureka-server) 现:spring-cloud-starter-netflix-eureka-server

<?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>com.goolecloud.springcloud</groupId>
        <artifactId>servicecloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>servicecloud-config-eureka-7001</artifactId>

    <properties>
        <java.version>1.8</java.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-devtools</artifactId>
        </dependency>
    </dependencies>
</project>

yml

server:
  port: 7001
eureka:
  instance:
    hostname: localhost #eureka服务端的实例名称 
  client:
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/  #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。 

    register-with-eureka: false #false表示不向注册中心注册自己
    fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 

启动类增加@EnableEurekaServer //EurekaServer服务器端启动类,接受其它微服务注册进来 注解

package com.goolecloud.springcloud;

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

@SpringBootApplication
@EnableEurekaServer
public class ServicecloudConfigEureka7001Application {

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

}

测试:http://localhost:7001/

 

2.servicecloud-provider-dept-8001将已有的部门微服务注册进eureka服务中心

pom 

<!—springboot2.0—>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency> 
    < dependency > 
      < groupId > org.springframework.cloud </ groupId > 
      < artifactId > spring-cloud-starter- config </ artifactId > 
    </ dependency > 

修改yml

eureka:
  client:
    service-url: #客户端注册进eureka服务列表内 
      defaultZone: http://localhost:7001/eureka
  instance:
    instance-id: servicecloud-dept8001
    prefer-ip-address: true
ServicecloudProvider8001Application 主启动类 添加注解 @EnableEurekaClient 说明是Eureka客户端注入到Eureka的服务中
package com.goolecloud.springcloud;

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

@SpringBootApplication
@EnableEurekaClient
public class ServicecloudProvider8001Application {

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

}

测试:先启动 servicecloud-eureka-7001服务 在启动ServicecloudProvider8001Application http://localhost:7001/

3.actuator与注册微服务信息完善

主机名称:服务名称修改servicecloud-provider-dept-8001

yml添加

eureka: 
   client:   #客户端注册进eureka服务列表内 
     service-url:   
       defaultZone:  http://localhost:7001/eureka 
  instance: 
    instance-id: microservicecloud-dept8001 

访问信息有IP信息提示 yml

prefer-ip-address:  true      #访问路径可以显示IP地址 与instance-id: 同一节点

微服务info内容详细信息

servicecloud-provider-dept-8001:pom

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

修改父工程 pom

< build > 
    < finalName >servicecloud </ finalName > 
    < resources > 
      < resource > 
        < directory > src /main/resources </ directory > 
        < filtering > true </ filtering > 
      </ resource > 
    </ resources > 
    < plugins > 
      < plugin > 
        < groupId > org.apache.maven.plugins </ groupId > 
        < artifactId > maven -resources- plugin </ artifactId > 
        < configuration > 
          < delimiters > 
           < delimit > $ </ delimit > 
          </ delimiters > 
        </ configuration > 
      </ plugin > 
    </ plugins > 
   </ build > 

yml 

springboot2.0以下配置

info: 
   app.name:  goolecloud-servicecloud 
   company.name:  www.goolecloud.com 
   build.artifactId:  $project.artifactId$ 
   build.version:  $project.version$ 
 

springboot2.X配置

Springboot2.X
#actuator端口
management:
  server:
    port: 9001
  endpoints:
    web:
      base-path: /actuator
      exposure:
        include: health,info,env,metrics
  endpoint:
    health:
      show-details: always

4.eureka自我保护

导致原因

什么是自我保护模式? 

   

默认情况下,如果EurekaServer在一定时间内没有接收到某个微服务实例的心跳,EurekaServer将会注销该实例(默认90秒)。但是当网络分区故障发生时,微服务与EurekaServer之间无法正常通信,以上行为可能变得非常危险了——因为微服务本身其实是健康的, 此时本不应该注销这个微服务 。Eureka通过“自我保护模式”来解决这个问题——当EurekaServer节点在短时间内丢失过多客户端时(可能发生了网络分区故障),那么这个节点就会进入自我保护模式。一旦进入该模式,EurekaServer就会保护服务注册表中的信息,不再删除服务注册表中的数据(也就是不会注销任何微服务)。当网络故障恢复后,该Eureka Server节点会自动退出自我保护模式。 

  

在自我保护模式中,Eureka Server会保护服务注册表中的信息,不再注销任何服务实例。当它收到的心跳数重新恢复到阈值以上时,该Eureka Server节点就会自动退出自我保护模式。它的设计哲学就是宁可保留错误的服务注册信息,也不盲目注销任何可能健康的服务实例。一句话讲解:好死不如赖活着 

  

综上,自我保护模式是一种应对网络异常的安全保护措施。它的架构哲学是宁可同时保留所有微服务(健康的微服务和不健康的微服务都会保留),也不盲目注销任何健康的微服务。使用自我保护模式,可以让Eureka集群更加的健壮、稳定。 

  

在Spring Cloud中,可以使用eureka.server.enable-self-preservation = false 禁用自我保护模式。 

5.servicecloud-provider-dept-8001服务发现Discovery

servicecloud-provider-dept-8001工程的DeptController

  @Autowired 

   private  DiscoveryClient client; 


  @RequestMapping(value = "/dept/discovery", method = RequestMethod. GET ) 
   public  Object discovery() 
  { 
    List<String> list = client.getServices(); 
    System. out .println("**********" + list); 


    List<ServiceInstance> srvList = client.getInstances("SERVICECLOUD-DEPT"); 
     for  (ServiceInstance element : srvList) { 
     System. out .println(element.getServiceId() + "\t" + element.getHost() + "\t" + element.getPort() + "\t" 
         + element.getUri()); 
    } 
     return   this .client; 
  } 

启动类添加@EnableDiscoveryClient  //服务发现 注解

自测:1.先要启动EurekaSer,2.再启动ServicecloudProvider8001Application主启动类,需要稍等一会,

修改servicecloud-consumer-dept-80工程的DeptController_Consumer

package com.goolecloud.springcoud.controller;

import com.goolecloud.springcloud.entities.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class DeptCoumsuer_Controller {
    private static  final String REST_URL_PREFIX =  "http://localhost:8001" ;
  
    @Autowired
    RestTemplate template;
    @RequestMapping(value = "/consumer/add", method = RequestMethod.POST)
    public boolean add(@RequestBody Dept dept)
    {
        return template.postForObject(REST_URL_PREFIX+"/dept/add",dept,boolean.class);
    }

    @RequestMapping(value = "/consumer/get/{id}", method = RequestMethod.GET)
    public Dept get(@PathVariable("id") Long id)
    {
        return template.getForObject(REST_URL_PREFIX+"/dept/get/"+id,Dept.class);
    }

    @RequestMapping(value = "/consumer/list", method = RequestMethod.GET)
    public List<Dept> list()
    {
        return template.getForObject(REST_URL_PREFIX+"/dept/list",List.class);
    }

}

6.Eureka集群配置

原理说明:

基本原理

上图是来自eureka的官方架构图,这是基于集群配置的eureka; 

- 处于不同节点的eureka通过Replicate进行数据同步 

- Application Service为服务提供者 

- Application Client为服务消费者 

- Make Remote Call完成一次服务调用

 

服务启动后向Eureka注册,Eureka Server会将注册信息向其他Eureka Server进行同步,当服务消费者要调用服务提供者,则向服务注册中心获取服务提供者地址,然后会将服务提供者地址缓存在本地,下次再调用时,则直接从本地缓存中取,完成一次调用。

 

当服务注册中心Eureka Server检测到服务提供者因为宕机、网络原因不可用时,则在服务注册中心将服务置为DOWN状态,并把当前服务提供者状态向订阅者发布,订阅过的服务消费者更新本地缓存。

 

服务提供者在启动后,周期性(默认30秒)向Eureka Server发送心跳,以证明当前服务是可用状态。Eureka Server在一定的时间(默认90秒)未收到客户端的心跳,则认为服务宕机,注销该实例。

(1).新建servicecloud-eureka-7002,servicecloud-eureka-7003 按照7001为模板粘贴POM

(2).修改映射配置  找到C:\Windows\System32\drivers\etc路径下的hosts文件 mac/Linux 系统修改/etc/hosts

修改映射配置添加进hosts文件
127.0.0.1  eureka7001.com
127.0.0.1  eureka7002.com
127.0.0.1  eureka7003.com

7001端口 yml

server:   
   port:  7001 


eureka:   
   instance: 
     hostname:  eureka7001.com  #eureka服务端的实例名称 
   client:   
     register-with-eureka:   false       #false表示不向注册中心注册自己。 
     fetch-registry:   false       #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 
     service-url:   
       #单机 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。 
       defaultZone:  http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ 
       

7002 端口yml

server:   
   port:  7002 


eureka:   
   instance: 
     hostname:  eureka7002.com  #eureka服务端的实例名称 
   client:   
     register-with-eureka:   false       #false表示不向注册中心注册自己。 
     fetch-registry:   false       #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 
     service-url:   
       #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。 
       defaultZone:  http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/ 

7003端口yml

server:   
   port:  7003 


eureka:   
   instance: 
     hostname:  eureka7003.com  #eureka服务端的实例名称 
   client:   
     register-with-eureka:   false       #false表示不向注册中心注册自己。 
     fetch-registry:   false       #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 
     service-url:   
       #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。 
       defaultZone:  http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/ 

(3).servicecloud-provider-dept-8001 微服务发布到上面3台eureka集群配置中

server: 
   port:  8001 
   
mybatis: 
   config-location:  classpath:mybatis/mybatis.cfg.xml   #mybatis所在路径 
   type-aliases-package:  com.goolecloud.springcloud.entities  #entity别名类 
   mapper-locations: 
  - classpath:mybatis/mapper/ ** / * .xml  #mapper映射文件 
     
spring: 
    application: 
     name: servicecloud-dept  
    datasource: 
     type:  com.alibaba.druid.pool.DruidDataSource 
     driver-class-name:  org.gjt.mm.mysql.Driver 
     url:  jdbc:mysql://localhost:3306/cloudDB01 
     username:  root 
     password:  123456 
     dbcp2: 
       min-idle:  5 
       initial-size:  5 
       max-total:  5 
       max-wait-millis:  200 
       
eureka: 
   client:   #客户端注册进eureka服务列表内 
     service-url:   
       defaultZone:   http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ 
   instance: 
     instance-id: servicecloud-dept8001    #自定义服务名称信息 
     prefer-ip-address:   true       #访问路径可以显示IP地址 
       
       
 Springboot2.X
#actuator端口
management:
  server:
    port: 9001
  endpoints:
    web:
      base-path: /actuator
      exposure:
        include: health,info,env,metrics
  endpoint:
    health:
      show-details: always

7.作为服务注册中心,EurekaZookeeper好在哪里

  

 作为服务注册中心,Eureka比Zookeeper好在哪里 

著名的CAP理论指出,一个分布式系统不可能同时满足C(一致性)、A(可用性)和P(分区容错性)。由于分区容错性P在是分布式系统中必须要保证的,因此我们只能在A和C之间进行权衡。 

因此 

Zookeeper保证的是CP, 

Eureka则是AP。 

  

4.1 Zookeeper保证CP 

当向注册中心查询服务列表时,我们可以容忍注册中心返回的是几分钟以前的注册信息,但不能接受服务直接down掉不可用。也就是说,服务注册功能对可用性的要求要高于一致性。但是zk会出现这样一种情况,当master节点因为网络故障与其他节点失去联系时,剩余节点会重新进行leader选举。问题在于,选举leader的时间太长,30 ~ 120s, 且选举期间整个zk集群都是不可用的,这就导致在选举期间注册服务瘫痪。在云部署的环境下,因网络问题使得zk集群失去master节点是较大概率会发生的事,虽然服务能够最终恢复,但是漫长的选举时间导致的注册长期不可用是不能容忍的。 

  

4.2 Eureka保证AP 

Eureka看明白了这一点,因此在设计时就优先保证可用性。 Eureka各个节点都是平等的 ,几个节点挂掉不会影响正常节点的工作,剩余的节点依然可以提供注册和查询服务。而Eureka的客户端在向某个Eureka注册或时如果发现连接失败,则会自动切换至其它节点,只要有一台Eureka还在,就能保证注册服务可用(保证可用性),只不过查到的信息可能不是最新的(不保证强一致性)。除此之外,Eureka还有一种自我保护机制,如果在15分钟内超过85%的节点都没有正常的心跳,那么Eureka就认为客户端与注册中心出现了网络故障,此时会出现以下几种情况:  

1. Eureka不再从注册列表中移除因为长时间没收到心跳而应该过期的服务  

2. Eureka仍然能够接受新服务的注册和查询请求,但是不会被同步到其它节点上(即保证当前节点依然可用)  

3. 当网络稳定时,当前实例新的注册信息会被同步到其它节点中 

  

因此, Eureka可以很好的应对因网络故障导致部分节点失去联系的情况,而不会像zookeeper那样使整个注册服务瘫痪。 

  

  

  

  

  

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值