创建spring cloud 项目 将所有服务用maven父项目包裹不用idea多页面切换

创建maven父类项目

 

 

删掉下面的,只是做父类用,这些都不要

把pom 的父类替换掉  用zuul和gateway不一样的哦

二。下面建各个子类

 

1.注册中心

 

启动类启动标加上

 

 application.yml  详情

spring:
  application:
    name: lrkj-regist
  profiles:
    active: dev

application-dev.yml  详情

spring:
  application:
    name: lrkj-regist
  security:
    basic:
      enabled: true
server:
  port: 8600

eureka:
  enviroment: dev
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
  server:
    enable-self-preservation: false
    eviction-interval-timer-in-ms: 4000
  client:
    registerWithEureka: false  #false:不作为一个客户端注册到注册中心
    fetchRegistry: false      #为true时,可以启动,但报异常:Cannot execute request on any known server
    service-url:
      defaultZone: http://localhost:${server.port}/eureka/
访问:
http://localhost:8600/  好了成功

但是要加检测,日志展示上下线的服务,

直接上类内容了哈

package com.lanrenkongjian.cheng.lrkjregist.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.eureka.server.event.*;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * 用于监听Eureka服务停机通知
 *
 * spring
 **/
@Configuration
@EnableScheduling
public class EurekaInstanceCanceledListener implements ApplicationListener {

    private static Logger log = LoggerFactory.getLogger(EurekaInstanceCanceledListener.class);



    @Override
    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        if (applicationEvent instanceof EurekaInstanceCanceledEvent) {//服务下线事件
            EurekaInstanceCanceledEvent event = (EurekaInstanceCanceledEvent) applicationEvent;
            log.info("服务:{}下线了。。。", event.getAppName());
//            SendEmail.sendEmail("下线通知",event.getAppName()+"下线了",new String[]{},new String[]{"[email protected]"});
            //获取当前Eureka示例中的节点信息
//            PeerAwareInstanceRegistry registry = EurekaServerContextHolder.getInstance().getServerContext().getRegistry();
//            Applications applications = registry.getApplications();
//          /*  applications.getRegisteredApplications().forEach(registryApplication -> {
//                registryApplication.getInstances().forEach(instance -> {
//                    if(Objects.equals(instance.getInstanceId(),event.getServerId())){
//                        //log.info("服务:{}挂啦。。。",instance.getAppName());
//                    }
//                });
//            })*/
//            List<Application> ls = applications.getRegisteredApplications();
//            for (Application application : ls) {
//                for (InstanceInfo instanceInfo : application.getInstances()) {
//                    if (instanceInfo.getInstanceId().equals(((EurekaInstanceCanceledEvent) applicationEvent).getServerId())) {
//                        log.info("服务:{}挂啦。。。", instanceInfo.getAppName());
//                    }
//                }
//            }
        }
        if (applicationEvent instanceof EurekaInstanceRegisteredEvent) {//服务注册事件
            EurekaInstanceRegisteredEvent event = (EurekaInstanceRegisteredEvent) applicationEvent;
            log.info("服务:{}注册成功啦。。。", event.getInstanceInfo().getAppName());
        }
        if (applicationEvent instanceof EurekaInstanceRenewedEvent) {//服务续约事件
            EurekaInstanceRenewedEvent event = (EurekaInstanceRenewedEvent) applicationEvent;
            log.info("心跳检测服务:{}。。。", event.getInstanceInfo().getAppName());
        }

        if (applicationEvent instanceof EurekaRegistryAvailableEvent) {//Eureka注册中心启动事件
        }
        if (applicationEvent instanceof EurekaServerStartedEvent) {// Eureka Server启动事件
        }
    }
}

启动后就能看到相关检测内容,如果有注册就会有日志展示的哦

完工,注册中心完了,接下来下个博客

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章