Skip to content

Commit

Permalink
feat: 添加服务实例数监控
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyQing committed May 6, 2024
1 parent a522cb9 commit 66fb00b
Show file tree
Hide file tree
Showing 12 changed files with 325 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@
import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
import io.github.smart.cloud.starter.monitor.AppChangeNotifier;
import io.github.smart.cloud.starter.monitor.component.GitLabComponent;
import io.github.smart.cloud.starter.monitor.schedule.OfflineCheckSchedule;
import io.github.smart.cloud.starter.monitor.component.ReminderComponent;
import io.github.smart.cloud.starter.monitor.component.RobotComponent;
import io.github.smart.cloud.starter.monitor.condition.ConditionOnWework;
import io.github.smart.cloud.starter.monitor.listener.AppChangeWeworkNotice;
import io.github.smart.cloud.starter.monitor.listener.OfflineCheckListener;
import io.github.smart.cloud.starter.monitor.listener.OfflineWeworkNotice;
import io.github.smart.cloud.starter.monitor.properties.MonitorProperties;
import io.github.smart.cloud.starter.monitor.schedule.OfflineCheckSchedule;
import io.github.smart.cloud.starter.monitor.schedule.ServiceNodeCountCheckSchedule;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
Expand All @@ -49,45 +46,24 @@ public MonitorProperties monitorProperties() {
return new MonitorProperties();
}

@Bean
@ConditionalOnMissingBean
public RobotComponent robotComponent(final MonitorProperties monitorProperties) {
return new RobotComponent(monitorProperties);
}

@Bean
@ConditionalOnMissingBean
public GitLabComponent gitLabComponent(final MonitorProperties monitorProperties) {
return new GitLabComponent(monitorProperties);
}

@Bean
@ConditionalOnMissingBean
public ReminderComponent reminderComponent(final GitLabComponent gitLabComponent, final MonitorProperties monitorProperties) {
return new ReminderComponent(gitLabComponent, monitorProperties);
}

@Bean
@ConditionalOnMissingBean
public OfflineCheckSchedule offlineCheckSchedule(final InstanceRepository instanceRepository, final MonitorProperties monitorProperties,
final ApplicationEventPublisher applicationEventPublisher) {
final ApplicationEventPublisher applicationEventPublisher) {
return new OfflineCheckSchedule(instanceRepository, monitorProperties, applicationEventPublisher);
}

@Bean
@ConditionOnWework
@ConditionalOnMissingBean
public OfflineWeworkNotice offlineWeworkNotice(final RobotComponent robotComponent, final MonitorProperties monitorProperties,
final ReminderComponent reminderComponent) {
return new OfflineWeworkNotice(robotComponent, monitorProperties, reminderComponent);
}

@Bean
@ConditionOnWework
@ConditionalOnMissingBean
public AppChangeWeworkNotice appChangeWeworkNotice(final MonitorProperties monitorProperties, final RobotComponent robotComponent,
final ReminderComponent reminderComponent) {
return new AppChangeWeworkNotice(monitorProperties, robotComponent, reminderComponent);
public ServiceNodeCountCheckSchedule serviceNodeCountCheckSchedule(final InstanceRepository instanceRepository, final MonitorProperties monitorProperties,
final ApplicationEventPublisher applicationEventPublisher) {
return new ServiceNodeCountCheckSchedule(instanceRepository, monitorProperties, applicationEventPublisher);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright © 2019 collin (1634753825@qq.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.smart.cloud.starter.monitor.autoconfigure;

import io.github.smart.cloud.starter.monitor.component.GitLabComponent;
import io.github.smart.cloud.starter.monitor.component.ReminderComponent;
import io.github.smart.cloud.starter.monitor.component.RobotComponent;
import io.github.smart.cloud.starter.monitor.listener.AppChangeWeworkNotice;
import io.github.smart.cloud.starter.monitor.listener.wework.OfflineNotice;
import io.github.smart.cloud.starter.monitor.listener.wework.ServiceNodeCountCheckNotice;
import io.github.smart.cloud.starter.monitor.properties.MonitorProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* 企业微信通知配置
*
* @author collin
* @date 2024-05-06
*/
@Configuration
@ConditionalOnProperty(name = "smart.monitor.wework.enable", havingValue = "true", matchIfMissing = true)
public class WeworkNoticeAutoConfiguration {

@Bean
@ConditionalOnMissingBean
public RobotComponent robotComponent(final MonitorProperties monitorProperties) {
return new RobotComponent(monitorProperties);
}

@Bean
@ConditionalOnMissingBean
public ReminderComponent reminderComponent(final GitLabComponent gitLabComponent, final MonitorProperties monitorProperties) {
return new ReminderComponent(gitLabComponent, monitorProperties);
}

@Bean
@ConditionalOnMissingBean
public OfflineNotice offlineWeworkNotice(final RobotComponent robotComponent, final MonitorProperties monitorProperties, final ReminderComponent reminderComponent) {
return new OfflineNotice(robotComponent, monitorProperties, reminderComponent);
}

@Bean
@ConditionalOnMissingBean
public ServiceNodeCountCheckNotice serviceNodeCountCheckNotice(final RobotComponent robotComponent, final MonitorProperties monitorProperties, final ReminderComponent reminderComponent) {
return new ServiceNodeCountCheckNotice(robotComponent, monitorProperties, reminderComponent);
}

@Bean
@ConditionalOnMissingBean
public AppChangeWeworkNotice appChangeWeworkNotice(final MonitorProperties monitorProperties, final RobotComponent robotComponent, final ReminderComponent reminderComponent) {
return new AppChangeWeworkNotice(monitorProperties, robotComponent, reminderComponent);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.smart.cloud.starter.monitor.event.offline;
package io.github.smart.cloud.starter.monitor.event.notice;

import lombok.Getter;
import lombok.Setter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,37 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.smart.cloud.starter.monitor.condition;
package io.github.smart.cloud.starter.monitor.event.notice;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;

import java.lang.annotation.*;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.context.ApplicationEvent;

/**
* 企业微信通知是否可用
* 服务实例数检查通知事件
*
* @author collin
* @date 2024-02-23
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Documented
@ConditionalOnProperty(name = "smart.monitor.wework.enable", havingValue = "true", matchIfMissing = true)
public @interface ConditionOnWework {
@Getter
@Setter
@ToString
public class ServiceNodeCountCheckNoticeEvent extends ApplicationEvent {

/**
* 服务名
*/
private final String name;
/**
* 当前在线实例数
*/
private final int nodeCount;

public ServiceNodeCountCheckNoticeEvent(Object source, String name, int nodeCount) {
super(source);
this.name = name;
this.nodeCount = nodeCount;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.github.smart.cloud.starter.monitor.listener.wework;

import io.github.smart.cloud.starter.monitor.component.ReminderComponent;
import io.github.smart.cloud.starter.monitor.component.RobotComponent;
import io.github.smart.cloud.starter.monitor.properties.MonitorProperties;
import io.github.smart.cloud.starter.monitor.properties.ServiceInfoProperties;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;

import java.util.Set;

@RequiredArgsConstructor
public abstract class AbstractWeworkNotice<E extends ApplicationEvent> implements ApplicationListener<E> {

protected final RobotComponent robotComponent;
protected final MonitorProperties monitorProperties;
protected final ReminderComponent reminderComponent;

/**
* 获取提醒人
*
* @param serviceName
* @return
*/
protected String getReminderParams(String serviceName) {
ServiceInfoProperties projectProperties = monitorProperties.getServiceInfos().get(serviceName);
if (projectProperties == null) {
return StringUtils.EMPTY;
}
Set<String> reminders = projectProperties.getReminders();
if (CollectionUtils.isEmpty(reminders)) {
return StringUtils.EMPTY;
}

return reminderComponent.generateReminders(reminders);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,25 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.smart.cloud.starter.monitor.listener;
package io.github.smart.cloud.starter.monitor.listener.wework;

import io.github.smart.cloud.starter.monitor.component.ReminderComponent;
import io.github.smart.cloud.starter.monitor.component.RobotComponent;
import io.github.smart.cloud.starter.monitor.event.offline.OfflineNoticeEvent;
import io.github.smart.cloud.starter.monitor.event.notice.OfflineNoticeEvent;
import io.github.smart.cloud.starter.monitor.properties.MonitorProperties;
import io.github.smart.cloud.starter.monitor.properties.ServiceInfoProperties;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationListener;

import java.util.Set;

/**
* 在线实例为0时,企业微信通知
*
* @author collin
* @date 2024-02-23
*/
@RequiredArgsConstructor
public class OfflineWeworkNotice implements ApplicationListener<OfflineNoticeEvent> {
public class OfflineNotice extends AbstractWeworkNotice<OfflineNoticeEvent> {

private final RobotComponent robotComponent;
private final MonitorProperties monitorProperties;
private final ReminderComponent reminderComponent;
public OfflineNotice(RobotComponent robotComponent, MonitorProperties monitorProperties, ReminderComponent reminderComponent) {
super(robotComponent, monitorProperties, reminderComponent);
}

@Override
public void onApplicationEvent(OfflineNoticeEvent event) {
Expand All @@ -52,23 +45,4 @@ public void onApplicationEvent(OfflineNoticeEvent event) {
robotComponent.sendWxworkNotice(robotComponent.getRobotKey(name), content.toString());
}

/**
* 获取提醒人
*
* @param serviceName
* @return
*/
private String getReminderParams(String serviceName) {
ServiceInfoProperties projectProperties = monitorProperties.getServiceInfos().get(serviceName);
if (projectProperties == null) {
return StringUtils.EMPTY;
}
Set<String> reminders = projectProperties.getReminders();
if (CollectionUtils.isEmpty(reminders)) {
return StringUtils.EMPTY;
}

return reminderComponent.generateReminders(reminders);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright © 2019 collin (1634753825@qq.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.smart.cloud.starter.monitor.listener.wework;

import io.github.smart.cloud.starter.monitor.component.ReminderComponent;
import io.github.smart.cloud.starter.monitor.component.RobotComponent;
import io.github.smart.cloud.starter.monitor.event.notice.ServiceNodeCountCheckNoticeEvent;
import io.github.smart.cloud.starter.monitor.properties.MonitorProperties;
import io.github.smart.cloud.starter.monitor.properties.ServiceInfoProperties;
import org.apache.commons.lang3.StringUtils;

/**
* 在线实例为0时,企业微信通知
*
* @author collin
* @date 2024-02-23
*/
public class ServiceNodeCountCheckNotice extends AbstractWeworkNotice<ServiceNodeCountCheckNoticeEvent> {

public ServiceNodeCountCheckNotice(RobotComponent robotComponent, MonitorProperties monitorProperties, ReminderComponent reminderComponent) {
super(robotComponent, monitorProperties, reminderComponent);
}

@Override
public void onApplicationEvent(ServiceNodeCountCheckNoticeEvent event) {
String name = event.getName();
String reminders = getReminderParams(name);
StringBuilder content = new StringBuilder(64);
content.append("**服务**: ").append(name).append("\n");

ServiceInfoProperties serviceInfoProperties = monitorProperties.getServiceInfos().get(name);
if (serviceInfoProperties != null) {
Integer nodeCount = serviceInfoProperties.getNodeCount();
if (nodeCount != null) {
content.append("**期望实例数**: ").append(nodeCount).append("\n");
}
}
content.append("**当前实例数**: <font color=\\\"warning\\\">").append(event.getNodeCount()).append("</font>\n");
if (StringUtils.isNotBlank(reminders)) {
content.append(reminders);
}
robotComponent.sendWxworkNotice(robotComponent.getRobotKey(name), content.toString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public class MonitorProperties {
* 检查离线服务在线实例数间隔时间(单位:毫秒)
*/
private Long checkOfflineTs = 60 * 5 * 1000L;
/**
* 检查服务在线实例数间隔时间(单位:毫秒)
*/
private Long checkServiceNodeCountTs = 60 * 20 * 1000L;
/**
* 异常接口统计间隔时间(单位:分钟)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,9 @@ public class ServiceInfoProperties implements Serializable {
* 机器人key
*/
private String robotKey;
/**
* 服务实例节点数
*/
private Integer nodeCount;

}

0 comments on commit 66fb00b

Please sign in to comment.