zoukankan      html  css  js  c++  java
  • Spring Cloud之Eureka对服务的监控处理等

    一,应用场景:多个服务启动后可能会因为一些其他或者内在原因导致服务会挂掉,而我们维护人员不知情的情况下服务会一直挂到有人发现为止,

    所以我们需要对Eureka的各个服务进行监听,当他们发生上线,下线,启动等,利用短信,邮箱等通知维护人员,维护人员会第一时间了解服务情况,进行处理。

    二.Eureka服务状态分类

    Eureka的server端会发出5个事件通知,分别是:

    2.1  EurekaInstanceCanceledEvent                     当有服务下线时会执行(掉线顾名思义,就是某个服务关闭时触发)
    2.2  EurekaInstanceRegisteredEvent                   当有服务注册时会执行(注册中心检测到有新的服务注册进来时触发)
    2.3  EurekaInstanceRenewedEvent                     当有服务续约时会执行(服务设置了心跳时间,如果下一次心跳还正常,就会把服务续约的信息更新到自身的Eureka Server中,然后再同步到其它Eureka Server中)
    2.4  EurekaRegistryAvailableEvent                      Eureka 注册中心启动执行

    2.5  EurekaServerStartedEvent                            Eureka Server 启动时执行

    那接下来就好说了

    第一步 : 引入maven依赖

    略: 就是基本的依赖

    第二步 : 监听服务

    在网上查询了部分资料之后,发现部分的博客缺少一些东西,会对新手有很多的误导,在这里给出一个详细的步骤

      Eureka服务端的配置

    server:
      port: 9999
    
    spring:
      application:
        name: eureka01
      security:
        user:
          name: root
          password: root
    
    eureka:
      client:
        register-with-eureka: false #是否注册给服务中心,本身就是注册中心,所以不需要注册
        service-url:
          defaultZone: http://root:root@localhost:9997/eureka/,http://root:root@localhost:9998/eureka/   #分别指向除自己外的注册中心地址,多个地址之间用“,”号隔开
      server:
        enable-self-preservation: false   # 测试时关闭自我保护机制,保证不可用服务及时踢出
        eviction-interval-timer-in-ms: 5000 # 续期时间,即扫描失效服务的间隔时间(缺省为60*1000ms)
    

      

    服务配置这里的 关闭自我保护和清理间隔一定要打开 (虽然我也不知道为什么)

    启动类

    package com.web;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    /**
     * Hello world!
     *
     */
    @SpringBootApplication
    @EnableEurekaServer
    public class EurekaServer  extends WebSecurityConfigurerAdapter{
        public static void main( String[] args ) {
    
            SpringApplication.run(EurekaServer.class, args);
        }
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable();//关闭csrf
            super.configure(http);
        }
    }

      服务监听类

    package com.web.event;
    
    import com.netflix.appinfo.InstanceInfo;
    import lombok.extern.log4j.Log4j2;
    import org.springframework.cloud.netflix.eureka.server.event.*;
    import org.springframework.context.event.EventListener;
    import org.springframework.stereotype.Component;
    
    @Log4j2
    @Component
    public class EurekaStateListener {
        //private final static Logger logger = LoggerFactory.getLogger(EurekaStateListener.class);
    
        @EventListener
        public void listen(EurekaInstanceCanceledEvent event) {
            log.info("服务{}已下线", event.getAppName());
            log.info("server地址信息{}", event.getServerId());
        }
    
        @EventListener
        public void listen(EurekaInstanceRegisteredEvent event) {
            InstanceInfo instanceInfo = event.getInstanceInfo();
            log.info("服务{}进行注册", instanceInfo.getAppName()+ instanceInfo.getHostName() +"  "+ instanceInfo.getIPAddr() +"  "+ instanceInfo.getPort());
        }
    
        @EventListener
        public void listen(EurekaInstanceRenewedEvent event) {
            log.info("服务{}进行续约", event.getServerId() +"  "+ event.getAppName());
        }
    
        @EventListener
        public void listen(EurekaRegistryAvailableEvent event) {
            log.info("注册中心启动,{}", System.currentTimeMillis());
        }
    
        @EventListener
        public void listen(EurekaServerStartedEvent event) {
            log.info("注册中心服务端启动,{}", System.currentTimeMillis());
        }
    }

    这里只是简单的记录,实际项目还是要做大量的通知,如果有注册中心是集群,挨个如上配置即可,我这就是集群的配置,以一个作文样列,就不每个都展示出来了。

    第三步 : 客户端

           客户端配置

    server:
      port: 8032
    
    spring:
      application:
        name: productAclient
    
    eureka:
      instance:
        lease-renewal-interval-in-seconds: 1 #每间隔10s,向服务端发送一次心跳
        lease-expiration-duration-in-seconds: 2 #如果我2s之内没有给你发心跳,就代表我“死”了,将我踢出掉。
        prefer-ip-address: true
        hostname: 192.168.1.83
        instance-id:  ${eureka.instance.hostname}:${server.port}
      client:
        service-url:
          defaultZone: http://root:root@localhost:9998/eureka/,http://root:root@localhost:9997/eureka/,http://root:root@localhost:9999/eureka/

    这里的instance配置一定要写,很多新手忘记写这个导致服务下线无法被监听,因为服务下线后eureka没有将该服务踢出

    启动类

    package com.web;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    /**
     * Hello world!
     *
     */
    @SpringBootApplication
    @EnableEurekaClient
    public class productA_Client {
    
        public static void main( String[] args ) {
            SpringApplication.run(productA_Client.class);
    
        }
    
    
    }

    第四步 : 测试看效果

    启动注册中心

    2019-07-10 11:10:33.966  INFO 17696 --- [      Thread-30] c.n.e.r.PeerAwareInstanceRegistryImpl    : Changing status to UP
    2019-07-10 11:10:33.974  INFO 17696 --- [      Thread-30] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
    2019-07-10 11:10:33.977  INFO 17696 --- [      Thread-30] com.web.event.EurekaStateListener        : 注册中心启动,1562728233974
    2019-07-10 11:10:33.978  INFO 17696 --- [      Thread-30] com.web.event.EurekaStateListener        : 注册中心服务端启动,1562728233978
    2019-07-10 11:10:33.990  INFO 17696 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 9998 (http) with context path ''
    2019-07-10 11:10:33.991  INFO 17696 --- [           main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 9998

    服务注册

    2019-07-10 11:10:35.860  INFO 17696 --- [nio-9998-exec-3] com.web.event.EurekaStateListener        : 服务PRODUCTACLIENT192.168.1.83  192.168.1.83  8031进行注册
    2019-07-10 11:10:35.875  INFO 17696 --- [nio-9998-exec-3] c.n.e.registry.AbstractInstanceRegistry  : Registered instance PRODUCTACLIENT/192.168.1.83:8031 with status UP (replication=true)
    2019-07-10 11:10:35.876  INFO 17696 --- [nio-9998-exec-3] com.web.event.EurekaStateListener        : 服务PRODUCTACLIENT192.168.1.83  192.168.1.83  8030进行注册
    2019-07-10 11:10:35.876  INFO 17696 --- [nio-9998-exec-3] c.n.e.registry.AbstractInstanceRegistry  : Registered instance PRODUCTACLIENT/192.168.1.83:8030 with status UP (replication=true)
    2019-07-10 11:10:36.389  INFO 17696 --- [nio-9998-exec-4] com.web.event.EurekaStateListener        : 服务PRODUCTACLIENT192.168.1.83  192.168.1.83  8031进行注册
    2019-07-10 11:10:36.389  INFO 17696 --- [nio-9998-exec-4] c.n.e.registry.AbstractInstanceRegistry  : Registered instance PRODUCTACLIENT/192.168.1.83:8031 with status UP (replication=true)
    2019-07-10 11:10:36.390  INFO 17696 --- [nio-9998-exec-4] com.web.event.EurekaStateListener        : 服务192.168.1.83:8030  PRODUCTACLIENT进行续约
    2019-07-10 11:10:36.391  INFO 17696 --- [nio-9998-exec-4] com.web.event.EurekaStateListener        : 服务192.168.1.83:8031  PRODUCTACLIENT进行续约
    2019-07-10 11:10:37.407  INFO 17696 --- [nio-9998-exec-5] com.web.event.EurekaStateListener        : 服务192.168.1.83:8030  PRODUCTACLIENT进行续约
    2019-07-10 11:10:37.408  INFO 17696 --- [nio-9998-exec-5] com.web.event.EurekaStateListener        : 服务192.168.1.83:8031  PRODUCTACLIENT进行续约
    2019-07-10 11:10:38.415  INFO 17696 --- [nio-9998-exec-6] com.web.event.EurekaStateListener        : 服务192.168.1.83:8030  PRODUCTACLIENT进行续约
    2019-07-10 11:10:38.416  INFO 17696 --- [nio-9998-exec-6] com.web.event.EurekaStateListener        : 服务192.168.1.83:8031  PRODUCTACLIENT进行续约
    2019-07-10 11:10:38.967  INFO 17696 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms

    停止8082端口的服务

    2018-07-24 09:41:59.599  INFO 16524 --- [a-EvictionTimer] com.lh.event.EurekaStateListener         : 服务MEMBER已下线
    2018-07-24 09:41:59.600  INFO 16524 --- [a-EvictionTimer] com.lh.event.EurekaStateListener         : server地址信息DESKTOP-SHDQ5I0:member:8082
    2018-07-24 09:41:59.601  INFO 16524 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Cancelled instance MEMBER/DESKTOP-SHDQ5I0:member:8082 (replication=false)
  • 相关阅读:
    arduino链接GY521(MPU6050)模块
    I2C Python Library ITG3205 API
    [翻译]AxureInteractive Prototypes原型设计工具Axure学习第2.3节
    [Java]XML数据的请求和DOM技术解析
    [Linux]VI相关操作
    mysql数据库连接错误问题
    关于管理单元初始化失败的解决方法
    彻底明白Java的IO系统(网上找的,还没看,先放这)
    一个关于C++ Inline关键字的引发的一个错误
    orcale 中日期类型相加的处理
  • 原文地址:https://www.cnblogs.com/KdeS/p/11162944.html
Copyright © 2011-2022 走看看