springcloud数据接口监控_微服务

组件一:Netflix Eureka——服务注册与发现
1. 什么是Eureka?
  • Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。
  • Eureka包含两个组件:Eureka Server 和 Eureka Client
  • Netfilx在设计Eureka时,遵循的是A(Availability-可用性)P(Partition tolerance-分区容错性)原则
  • Eureka 采用了C-S的设计架构
  • 系统中的其他微服务,使用Eureka的客户端连接到Eureka Server 并维持心跳连接,这样系统的维护人员就可以通过Eureka Server来监控系统中的各个微服务是否正常运行。
2. Eureka 三大角色:
  • Eureka Server:提供服务的注册与发现
  • Service Provider:将自身服务注册到Eureka中,使服务消费方能找到服务提供方
  • Service Consumer: 服务消费方从Eureka中获取注册服务列表,找到服务提供方
3.Eureka 和 ZooKeeper 的区别
  • Netfilx在设计Eureka时,遵循的是A(Availability-可用性)P(Partition tolerance-分区容错性)原则
  • Eureka各个节点都是平等的,几个节点挂掉不会影响正常节点的工作,剩余的节点依然可以提供注册和查询服务。而Eureka的客户端在向某个Eureka注册时如果发现连接失败,则会自动切换至其它节点,只要有一台Eureka还在,就能保证注册服务可用(保证可用性),只不过查到的信息可能不是最新的(不保证强一致性)。
  • 除此之外,Eureka还有一种自我保护机制,如果在15分钟内超过85%的节点都没有正常的心跳,那么Eureka就认为客户端与注册中心出现了网络故障,此时会出现以下几种情况:
    1.Eureka不再从注册列表中移除因为长时间没有收到心跳而应该过期的服务
    2.Eureka任然能够提供服务注册和查询请求,但是暂时不会将注册的服务同步到其他节点上(即保证当前节点依然可用)
    3.当网络稳定时,当前实例新的注册信息才会被同步到其它节点上
  • 总而言之,Eureka可以很好的应对因网络故障导致部分节点失去联系的情况(可用性),而不会像Zookeeper那样使整个注册服务瘫痪。
  • ZooKeeper,遵循的是C(Consistency-一致性)P(Partition tolerance-分区容错性)原则
  • 在集群中,当master节点因为网络故障与其他节点失去联系时,剩余节点会重新进行leader选举,选举leader的时间太长,30~120秒,且选举期间整个ZooKeeper集群都是不可用的(保证一致性)
  • 这就导致在选举期间注册服务瘫痪,而在云部署的环境下,因网络问题使得ZooKeeper集群失去master节点是较大概率发生的事,虽然服务最终能够恢复,但是长时间选举时间导致的注册长期不可用是不能容忍的。
  • 像 618,双11等大型购物节日时,遵循的都是AP原则,防止宕机,提高用户的体验
4. 开始完成第一个组件子项目
  1. 在父项目下创建一个子项目(moudle),起名时尽量暴露端口号
  2. 引入依赖,这块没有写版本号是因为父项目中有版本号
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
  1. 编写application.yml核心配置文件
server:
  port: 7001
eureka:
  instance:
    hostname: localhost #Eureka 服务端的实例名称
  client:
    register-with-eureka: false # 表示是否向 Eureka 注册中心注册自己(服务提供者才需要注册)
    service-url: # 监控页面
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    fetch-registry: false # fetch-registry 如果为 false,则表示自己为注册中心
  1. 自定义启动类, @EnableEurekaServer 注解
package cn.edu.xiyou;

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

@SpringBootApplication
@EnableEurekaServer
public class Eureka_7001 {
    public static void main(String[] args) {
        SpringApplication.run(Eureka_7001.class,args);
    }
}
  1. 访问 http://localhost:7001/ 进入监控页面。
  2. springcloud数据接口监控_微服务_02

5. 将服务提供者与注册中心进行关联
  1. 给服务提供者加上Eureka的依赖
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
  1. 修改服务提供者的application.yml核心配置
server:
  port: 8001
#mybatis配置,相当于build设置
mybatis:
  type-aliases-package: cn.edu.xiyou.pojo
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml
#spring的配置
spring:
  application:
    name: spring-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: 123456
#Eureka 的配置,要将服务注册到哪里,刚刚服务注册中心的地址:http://localhost:7001/eureka/
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/
      instance:
  instance-id: springcloud-provider-8001 # 修改 Eureka 注册中心中的默认描述信息,可以隐藏掉自己的 IP 地址
  1. 给服务提供者的启动类加上 @EnableEurekaClient 注解
package cn.edu.xiyou;

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

//开启类
@SpringBootApplication
@EnableEurekaClient  //客户端,在服务启动后,会自动注册到 Eureka 服务注册中心中
public class Provider_8001 {
    public static void main(String[] args) {
        SpringApplication.run(Provider_8001.class,args);
    }
}
  1. 访问注册中心地址:http://localhost:7001,观察结果
  2. springcloud数据接口监控_服务提供者_03

  3. 此时停掉服务提供者,验证 Eureka 的自我保护机制
    自我保护机制:
  • 在默认的情况系,EurekaServer在一定的时间内没有接受到微服务的实例心跳,EurekaServer将会注销该实例(默认为90秒)
  • 但是在网络发生故障的时候,微服务于EurekaServer之间是无法通信的,这种情况就非常危险了,因为微服务本身实例是健康的,此刻本不应该注销这个微服务。那么Eureka自我保护模式就解决了这个问题了
  • 当EurekaServer节点在短时间内丢失过客户端时(包含发生的网络故障),那么节点就会进行自我保护
  • 一但进入自我保护模式,EurekaServer就会保护服务注册表中的信息,不再删除服务注册表中的数据不会注销任何的微服务
  • 当网络故障恢复后,该EurekaServer节点会自动的退出自我保护机制
  • 在自我保护模式中,EurekaServer会保护服务注册表中的信息,不在注销任何服务实例,当重新收到心跳数恢复阀值以上时,该EurekaServer节点就会自动的退出自我保护模式,这种社设计宁可保留错误的服务注册信息,也不盲目的注销删除任何可能健康的服务实例
  • 自我保护机制就是一种对网络异常的安全保护实施,他会报错所有的微服务(健康或者不健康的微服务都会保存) ,而不会盲目的删除任何微服务,可以让Eureka集群更加的健壮/稳定
  1. 添加 Eureka注册中心的 info 信息
  2. 给服务提供者加上 actuator 的依赖,这样就可以在yml中添加 info 信息了
<!-- actuator 用来完善监控信息-info-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
  1. 在 yml 配置文件中添加 info 信息,info 信息是自定义的键值对
server:
  port: 8001
#mybatis配置,相当于build设置
mybatis:
  type-aliases-package: cn.edu.xiyou.pojo
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml
#spring的配置
spring:
  application:
    name: spring-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: 123456
#Eureka 的配置,要将服务注册到哪里,刚刚服务注册中心的地址:http://localhost:7001/eureka/
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/
  instance:
    instance-id: springcloud-provider-8001 # 修改 Eureka 注册中心中的默认描述信息,可以隐藏掉自己的 IP 地址
#info配置
info:
  app.name: first-Eureka-test
  company.name: xiyou.edu.cn
  1. 打开服务注册中心,点击服务对应的 status 的链接
  2. springcloud数据接口监控_spring_04

  3. 下载后,打开文件,你会发现服务提供者配置的所有info信息
6. Eureka 集群环境配置
  1. 做一个域名映射,模拟集群的感觉
    打开 C:\Windows\System32\drivers\etc 路径下的 hosts 文件
    在底部添加以下域名映射,名字自已定义就行,127.0.0.1后面以TAB分割
    127.0.0.1 eureka01.com
    127.0.0.1 eureka02.com
    127.0.0.1 eureka03.com
  2. 将上面写的 Eureka 子项目复制两份,只用修改所有 Eureka 子项目的 yml 配置文件便可达到集群的效果
  • eureka01子项目的yml配置如下:
server:
  port: 7001
eureka:
  instance:
    hostname: eureka01.com #Eureka 服务端的实例名称
  client:
    register-with-eureka: false # 表示是否向 Eureka 注册中心注册自己(服务提供者才需要注册)
    service-url: # 监控页面
      # 单机: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      # 关联( 7002 和 7003 的 Eureka):
      defaultZone: http://eureka02.com:7002/eureka/,http://eureka03.com:7003/eureka/
    fetch-registry: false # fetch-registry 如果为 false,则表示自己为注册中心,默认值是 true
  • eureka02子项目的yml配置如下:
server:
  port: 7002
eureka:
  instance:
    hostname: eureka02.com #Eureka 服务端的实例名称
  client:
    register-with-eureka: false # 表示是否向 Eureka 注册中心注册自己(服务提供者才需要注册)
    service-url: # 监控页面
      # 单机: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      # 关联( 7001 和 7003 的 Eureka):
      defaultZone: http://eureka01.com:7001/eureka/,http://eureka03.com:7003/eureka/ #集群
    fetch-registry: false # fetch-registry 如果为 false,则表示自己为注册中心,默认值是 true
  • eureka03子项目的yml配置如下:
server:
  port: 7003
eureka:
  instance:
    hostname: eureka03.com #Eureka 服务端的实例名称
  client:
    register-with-eureka: false # 表示是否向 Eureka 注册中心注册自己(服务提供者才需要注册)
    service-url: # 监控页面
      # 单机: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      # 关联( 7001 和 7002 的 Eureka):
      defaultZone: http://eureka01.com:7001/eureka/,http://eureka02.com:7002/eureka/
    fetch-registry: false # fetch-registry 如果为 false,则表示自己为注册中心,默认值是 true
  1. 进入注册中心测试即可
  2. 修改服务提供者子项目的yml配置,将服务发送到集群
server:
  port: 8001
#mybatis配置,相当于build设置
mybatis:
  type-aliases-package: cn.edu.xiyou.pojo
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml
#spring的配置
spring:
  application:
    name: spring-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: 123456
#Eureka 的配置,要将服务注册到哪里,刚刚服务注册中心的地址:http://eureka01.com:7001/eureka/,http://eureka02.com:7002/eureka/,http://eureka03.com:7003/eureka/
eureka:
  client:
    service-url:
      defaultZone: http://eureka01.com:7001/eureka/,http://eureka02.com:7002/eureka/,http://eureka03.com:7003/eureka/
  instance:
    instance-id: springcloud-provider-8001 # 修改 Eureka 注册中心中的默认描述信息,可以隐藏掉自己的 IP 地址
#info配置
info:
  app.name: first-Eureka-test
    company.name: xiyou.edu.cn
  1. 运行 3 个 Eureka 子项目 和 服务提供者子项目
    会发现不管进哪一个注册中心,服务都被注册了进去