一. Eureka服务注册与发现

1.新建子项目springcloud-eureka-7001 pom

<?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>springcloud</artifactId>
        <groupId>com.giao</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-eureka-7001</artifactId>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
        <version>1.4.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
</dependencies>

</project>
  1. 配置文件 application.yaml
server:
  port: 7001

#Eureka配置
eureka:
  instance:
    hostname: localhost #Eureka服务端的实例名称
  client:
    register-with-eureka: false #表示是否向Eureka注册中心注册自己
    fetch-registry: false #fetch-registry如果为false,则表示自己为注册中心
    service-url: #监控页面
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  1. 启动类
package com.giao.springcloud;

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

@SpringBootApplication
@EnableEurekaServer //EnableEurekaServer 服务端的启动类,可以接收别人注册进来
public class EurekaServer_7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer_7001.class,args);
    }
}

4.在springcloud-provider-dept-8001项目 pom中加入eureka依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
    <version>1.4.6.RELEASE</version>
</dependency>
<!--    actuator完善监控信息    -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
  1. 在springcloud-provider-dept-8001项目 application.yaml中加入eureka配置
#Eureka的配置,服务注册到哪里
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/
    instance:
      instance-id: springcloud-privider-dept-8001 #修改eureka上的默认描述信息
#info配置
info:
  app.name: giao-springcloud
  company.name: giao

6.在springcloud-provider-dept-8001项目的主启动类加注解@EnableEurekaClient

@SpringBootApplication
@EnableEurekaClient//在服务启动后自动注册到eureka中
public class DeptProvider_8001 {
    public static void main(String[] args) {

        SpringApplication.run(DeptProvider_8001.class,args);
    }
}
  1. eureka 自我保护机制

    某个时刻一个微服务不可用,rureka不会立刻清理,依旧会对该微服务的信息进行保存,他的架构哲学是宁可同时保留所有微服务(健康或不健康的),也不盲目注销任何健康的微服务,可以让Eureka集群更加健壮和稳定

8.得到一个具体的微服务信息,通过具体的微服务id ,在启动类加注解

@SpringBootApplication
@EnableEurekaClient//在服务启动后自动注册到eureka中
@EnableDiscoveryClient//服务发现
public class DeptProvider_8001 {
    public static void main(String[] args) {

        SpringApplication.run(DeptProvider_8001.class,args);
    }
}
package com.giao.springcloud.controller;

import com.giao.springcloud.pojo.Dept;
import com.giao.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//提供restful服务
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;
    //获取一些配置信息,得到具体的微服务
    @Autowired
    private DiscoveryClient client;
    @PostMapping("/dept/add")
 public boolean addDept(Dept dept){
   return deptService.addDept(dept);
 }
@GetMapping("/dept/get/{id}")
 public Dept get(@PathVariable("id") Long id){
     return deptService.queryById(id)  ;
 }

    @GetMapping("/dept/list")
    public List<Dept> get(){
        return deptService.queryAll() ;
    }
    //注册进来的微服务 获取一些消息
    @RequestMapping("dept/discovery")
public Object discovery(){
//获取微服务列表清单
    List<String> services = client.getServices();
    System.out.println("discovery->services"+services);
    //得到一个具体的微服务信息,通过具体的微服务id applicationName
    List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
    for (ServiceInstance instance : instances) {
        System.out.println(
                instance.getHost()+"\t"+
                instance.getPort()+"\t"+
                  instance.getUri()+"\t"+ instance.getServiceId()
        );

    }
    return this.client;
}



}

二. Eureka集群环境配置

1.新建两个子项目springcloud-eureka-7002、springcloud-eureka-7003

2.将springcloud-eureka-7001的pom中的依赖复制粘贴至springcloud-eureka-7002、springcloud-eureka-7003

3.将springcloud-eureka-7001的配置文件application.yaml复制粘贴至springcloud-eureka-7002、springcloud-eureka-7003,分别修改端口号为7002,7003

4.两个子项目springcloud-eureka-7002、springcloud-eureka-7003分别写启动类

springcloud 前缀 springcloud._spring


5.分别修改7001,7002,7003的配置文件,使之集群关联

server:
  port: 7001

#Eureka配置
eureka:
  instance:
    hostname: localhost #Eureka服务端的实例名称
  client:
    register-with-eureka: false #表示是否向Eureka注册中心注册自己
    fetch-registry: false #fetch-registry如果为false,则表示自己为注册中心
    service-url: #监控页面
      #单机:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      #集群(关联)
      defaultZone: http://localhost:7002/eureka/,http://localhost:7003/eureka/
server:
  port: 7002

#Eureka配置
eureka:
  instance:
    hostname: localhost #Eureka服务端的实例名称
  client:
    register-with-eureka: false #表示是否向Eureka注册中心注册自己
    fetch-registry: false #fetch-registry如果为false,则表示自己为注册中心
    service-url: #监控页面
      #单机:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      #集群(关联)
      defaultZone: http://localhost:7001/eureka/,http://localhost:7003/eureka/
server:
  port: 7002

#Eureka配置
eureka:
  instance:
    hostname: localhost #Eureka服务端的实例名称
  client:
    register-with-eureka: false #表示是否向Eureka注册中心注册自己
    fetch-registry: false #fetch-registry如果为false,则表示自己为注册中心
    service-url: #监控页面
      #单机:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      #集群(关联)
      defaultZone: http://localhost:7001/eureka/,http://localhost:7003/eureka/

6.修改springcloud-provider-dept-e8001配置文件,新增eureka集群配置

server:
  port: 8001
  #mybatis配置
mybatis:
  type-aliases-package: com.giao.springcloud.pojo
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml
  #spring配置
spring:
  application:
    name: springcloud-provider-dept
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource #数据源
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/db01?useUnicode=true&characterEncoding=UTF-8
    username: root
    password: gn941030

#Eureka的配置,服务注册到哪里
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/,http://localhost:7003/eureka/
    instance:
      instance-id: springcloud-privider-dept-8001 #修改eureka上的默认描述信息

#info配置
info:
  app.name: giao-springcloud
  company.name: giao

三.对比Zookeeper

回顾CAP原则

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

CAP的三进二:CA、AP、CP 三个不可能同时满足

ACID

  • 原子性
  • 一致性
  • 隔离性
  • 持久性

CAP理论的核心

  • 一个分布式系统不可能同时很好的满足一致性,可用性和分区容错性三个需求
  • 根据CAP原理,将NoSql数据库分成满足了CA原则,满足CP原则和满足AP原则三大类:
  • CA:单点集群,满足一致性,可用性系统,通常可扩展性较差
  • CP:满足一致性,分区容错性的系统,通常性能不是特别高
  • AP:满足可用性,分区容错性的系统,通常可能对一致性要求低一些

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

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

  • Zookeeper保证的是CP;
  • 当向注册中心注册时,可以容忍注册中心返回的是几分钟以前注册的信息,但不能接收服务器直接down掉不可用(可用性高于一致性)主节点down掉,其他节点会进行leader选举,选举时间太长,选举期间整个zk集群不可用
  • Eureka保证的是AP;
  • Eureka优先保证可用性,各个节点都是平等的,几个节点掉不会影响正常节点的工作,剩余节点依然可以提供注册和查询服务,只要有一台eureka还在,就能保证注册服务的可用性,只不过查到的信息可能不是最新的,除此之外,eureka还有一种自我保护机制,15分钟超过85%的节点没有正常的心跳,Eureka就会认为服务端与注册中心出现了网络故障,会出现一下几种情况:
  • 1.Eureka不再从注册列表中移除因为长时间没收到心跳而应该过期的服务
  • 2.Eureka仍能接收新服务的注册和查询请求,但不会听不到其他节点上
  • 3.当网络稳定时,当前新的注册信息会被同步到其他节点

因此,Eureka可以很好的应对因网络故障导致节点失联的情况,而Zookeeper回事整个注册服务瘫痪