創建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啓動事件
        }
    }
}

啓動後就能看到相關檢測內容,如果有註冊就會有日誌展示的哦

完工,註冊中心完了,接下來下個博客

 

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