Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[imporve]: Split discovery center related monitoring into a separat module #2717

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
12 changes: 0 additions & 12 deletions collector/collector-basic/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,6 @@
<artifactId>dnsjava</artifactId>
<version>${dnsjava.version}</version>
</dependency>
<!-- consul -->
<dependency>
<groupId>com.ecwid.consul</groupId>
<artifactId>consul-api</artifactId>
<version>${consul-api.version}</version>
</dependency>
<!-- nacos -->
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>${nacos-client.version}</version>
</dependency>
<!-- mqtt -->
<dependency>
<groupId>com.hivemq</groupId>
Expand Down
57 changes: 57 additions & 0 deletions collector/collector-discovery/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.hertzbeat</groupId>
<artifactId>hertzbeat-collector</artifactId>
<version>2.0-SNAPSHOT</version>
</parent>

<artifactId>hertzbeat-collector-discovery</artifactId>
<name>${project.artifactId}</name>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.hertzbeat</groupId>
<artifactId>hertzbeat-collector-common</artifactId>
<scope>provided</scope>
</dependency>
<!-- consul -->
<dependency>
<groupId>com.ecwid.consul</groupId>
<artifactId>consul-api</artifactId>
<version>${consul-api.version}</version>
</dependency>
<!-- nacos -->
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>${nacos-client.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
* under the License.
*/

package org.apache.hertzbeat.collector.collect.httpsd.discovery;
package org.apache.hertzbeat.collector.collect.discovery;

import java.util.List;
import org.apache.hertzbeat.collector.collect.httpsd.discovery.entity.ConnectConfig;
import org.apache.hertzbeat.collector.collect.httpsd.discovery.entity.ServerInfo;
import org.apache.hertzbeat.collector.collect.httpsd.discovery.entity.ServiceInstance;
import org.apache.hertzbeat.common.entity.job.protocol.HttpsdProtocol;

import org.apache.hertzbeat.collector.collect.discovery.entity.ConnectConfig;
import org.apache.hertzbeat.collector.collect.discovery.entity.ServerInfo;
import org.apache.hertzbeat.collector.collect.discovery.entity.ServiceInstance;
import org.apache.hertzbeat.common.entity.job.protocol.DiscoveryProtocol;

/**
* DiscoveryClient interface.
Expand All @@ -32,10 +33,10 @@ public interface DiscoveryClient extends AutoCloseable {

/**
* Build connect config.
* @param httpsdProtocol httpsd protocol.
* @param discoveryProtocol discovery protocol.
* @return connect config object.
*/
ConnectConfig buildConnectConfig(HttpsdProtocol httpsdProtocol);
ConnectConfig buildConnectConfig(DiscoveryProtocol discoveryProtocol);

/**
* Initialize client.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,39 @@
* under the License.
*/

package org.apache.hertzbeat.collector.collect.httpsd.discovery;
package org.apache.hertzbeat.collector.collect.discovery;

import java.util.Objects;
import org.apache.hertzbeat.collector.collect.httpsd.constant.DiscoveryClientInstance;
import org.apache.hertzbeat.collector.collect.httpsd.discovery.impl.ConsulDiscoveryClient;
import org.apache.hertzbeat.collector.collect.httpsd.discovery.impl.NacosDiscoveryClient;
import org.apache.hertzbeat.common.entity.job.protocol.HttpsdProtocol;
import org.apache.hertzbeat.collector.collect.discovery.constant.DiscoveryClientInstance;
import org.apache.hertzbeat.collector.collect.discovery.impl.ConsulDiscoveryClient;
import org.apache.hertzbeat.collector.collect.discovery.impl.NacosDiscoveryClient;
import org.apache.hertzbeat.common.entity.job.protocol.DiscoveryProtocol;

/**
* Discovery Client Management
*/
public class DiscoveryClientManagement {

public DiscoveryClient getClient(HttpsdProtocol httpsdProtocol) {
public DiscoveryClient getClient(DiscoveryProtocol httpsdProtocol) {
return createClient(httpsdProtocol, DiscoveryClientInstance.getByName(httpsdProtocol.getDiscoveryClientTypeName()));
}

private DiscoveryClient createClient(HttpsdProtocol httpsdProtocol, DiscoveryClientInstance discoveryClientInstance) {
private DiscoveryClient createClient(DiscoveryProtocol discoveryProtocol, DiscoveryClientInstance discoveryClientInstance) {
if (Objects.equals(discoveryClientInstance, DiscoveryClientInstance.NOT_SUPPORT)) {
return null;
}

return doCreateClient(httpsdProtocol, discoveryClientInstance);
return doCreateClient(discoveryProtocol, discoveryClientInstance);
}

private DiscoveryClient doCreateClient(HttpsdProtocol httpsdProtocol, DiscoveryClientInstance discoveryClientInstance) {
private DiscoveryClient doCreateClient(DiscoveryProtocol discoveryProtocol, DiscoveryClientInstance discoveryClientInstance) {
DiscoveryClient discoveryClient;
switch (discoveryClientInstance) {
case CONSUL -> discoveryClient = new ConsulDiscoveryClient();
case NACOS -> discoveryClient = new NacosDiscoveryClient();
default -> { return null; }
}
discoveryClient.initClient(discoveryClient.buildConnectConfig(httpsdProtocol));
discoveryClient.initClient(discoveryClient.buildConnectConfig(discoveryProtocol));
return discoveryClient;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,32 @@
* under the License.
*/

package org.apache.hertzbeat.collector.collect.httpsd;
package org.apache.hertzbeat.collector.collect.discovery;

import com.ecwid.consul.transport.TransportException;
import com.google.common.annotations.VisibleForTesting;
import java.lang.reflect.Field;
import java.util.Objects;

import com.ecwid.consul.transport.TransportException;
import com.google.common.annotations.VisibleForTesting;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.hertzbeat.collector.collect.AbstractCollect;
import org.apache.hertzbeat.collector.collect.httpsd.discovery.DiscoveryClient;
import org.apache.hertzbeat.collector.collect.httpsd.discovery.DiscoveryClientManagement;
import org.apache.hertzbeat.collector.collect.httpsd.discovery.entity.ServerInfo;
import org.apache.hertzbeat.collector.constants.CollectorConstants;
import org.apache.hertzbeat.collector.dispatch.DispatchConstants;
import org.apache.hertzbeat.collector.collect.discovery.entity.ServerInfo;
import org.apache.hertzbeat.common.constants.CommonConstants;
import org.apache.hertzbeat.common.entity.job.Metrics;
import org.apache.hertzbeat.common.entity.job.protocol.HttpsdProtocol;
import org.apache.hertzbeat.common.entity.message.CollectRep;
import org.apache.hertzbeat.common.util.CommonUtil;

/**
* http_sd protocol collection implementation
* Discovery collect implementation
*/

@Slf4j
public class HttpsdImpl extends AbstractCollect {
public class DiscoveryImpl extends AbstractCollect {

private static final String SERVER = "server";

@Setter
Expand All @@ -51,17 +51,17 @@ public class HttpsdImpl extends AbstractCollect {

@Override
public void preCheck(Metrics metrics) throws IllegalArgumentException {
HttpsdProtocol httpsdProtocol = metrics.getHttpsd();
if (Objects.isNull(httpsdProtocol) || httpsdProtocol.isInvalid()){
var discoveryProtocol = metrics.getDiscovery();
if (Objects.isNull(discoveryProtocol) || discoveryProtocol.isInvalid()){
throw new IllegalArgumentException("http_sd collect must have a valid http_sd protocol param! ");
}
}

@Override
public void collect(CollectRep.MetricsData.Builder builder, long monitorId, String app, Metrics metrics) {
HttpsdProtocol httpsdProtocol = metrics.getHttpsd();
var discoveryProtocol = metrics.getDiscovery();

try (DiscoveryClient discoveryClient = discoveryClientManagement.getClient(httpsdProtocol)) {
try (DiscoveryClient discoveryClient = discoveryClientManagement.getClient(discoveryProtocol)) {
collectMetrics(builder, metrics, discoveryClient);
} catch (TransportException e1) {
String errorMsg = "Consul " + CommonUtil.getMessageFromThrowable(e1);
Expand Down Expand Up @@ -106,10 +106,6 @@ public String supportProtocol() {
return DispatchConstants.PROTOCOL_HTTP_SD;
}

private boolean checkParamsFailed(HttpsdProtocol httpsd) {
return Objects.isNull(httpsd) || httpsd.isInvalid();
}

private void addColumnIfMatched(String fieldName, Object sourceObj, CollectRep.ValueRow.Builder valueRowBuilder) {
String columnValue = null;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

package org.apache.hertzbeat.collector.collect.httpsd.constant;
package org.apache.hertzbeat.collector.collect.discovery.constant;

/**
* Discovery client instance status.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

package org.apache.hertzbeat.collector.collect.httpsd.constant;
package org.apache.hertzbeat.collector.collect.discovery.constant;

import java.util.Arrays;
import org.apache.commons.lang3.StringUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

package org.apache.hertzbeat.collector.collect.httpsd.discovery.entity;
package org.apache.hertzbeat.collector.collect.discovery.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

package org.apache.hertzbeat.collector.collect.httpsd.discovery.entity;
package org.apache.hertzbeat.collector.collect.discovery.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
* under the License.
*/

package org.apache.hertzbeat.collector.collect.httpsd.discovery.entity;
package org.apache.hertzbeat.collector.collect.discovery.entity;

import java.util.Map;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,23 @@
* under the License.
*/

package org.apache.hertzbeat.collector.collect.httpsd.discovery.impl;
package org.apache.hertzbeat.collector.collect.discovery.impl;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.agent.model.Check;
import com.ecwid.consul.v1.agent.model.Self;
import com.ecwid.consul.v1.agent.model.Service;
import com.google.common.collect.Lists;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.apache.hertzbeat.collector.collect.discovery.DiscoveryClient;
import org.apache.hertzbeat.collector.collect.discovery.entity.ConnectConfig;
import org.apache.hertzbeat.collector.collect.discovery.entity.ServerInfo;
import org.apache.hertzbeat.collector.collect.discovery.entity.ServiceInstance;
import org.apache.commons.lang3.StringUtils;
import org.apache.hertzbeat.collector.collect.httpsd.discovery.DiscoveryClient;
import org.apache.hertzbeat.collector.collect.httpsd.discovery.entity.ConnectConfig;
import org.apache.hertzbeat.collector.collect.httpsd.discovery.entity.ServerInfo;
import org.apache.hertzbeat.collector.collect.httpsd.discovery.entity.ServiceInstance;
import org.apache.hertzbeat.common.entity.job.protocol.HttpsdProtocol;
import org.apache.hertzbeat.common.entity.job.protocol.DiscoveryProtocol;

/**
* DiscoveryClient impl of Consul
Expand All @@ -41,10 +42,10 @@ public class ConsulDiscoveryClient implements DiscoveryClient {
private ConsulClient consulClient;

@Override
public ConnectConfig buildConnectConfig(HttpsdProtocol httpsdProtocol) {
public ConnectConfig buildConnectConfig(DiscoveryProtocol discoveryProtocol) {
return ConnectConfig.builder()
.host(httpsdProtocol.getHost())
.port(Integer.parseInt(httpsdProtocol.getPort()))
.host(discoveryProtocol.getHost())
.port(Integer.parseInt(discoveryProtocol.getPort()))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,23 @@
* under the License.
*/

package org.apache.hertzbeat.collector.collect.httpsd.discovery.impl;
package org.apache.hertzbeat.collector.collect.discovery.impl;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import com.google.common.collect.Lists;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.apache.hertzbeat.collector.collect.httpsd.constant.DiscoveryClientHealthStatus;
import org.apache.hertzbeat.collector.collect.httpsd.discovery.DiscoveryClient;
import org.apache.hertzbeat.collector.collect.httpsd.discovery.entity.ConnectConfig;
import org.apache.hertzbeat.collector.collect.httpsd.discovery.entity.ServerInfo;
import org.apache.hertzbeat.collector.collect.httpsd.discovery.entity.ServiceInstance;
import org.apache.hertzbeat.common.entity.job.protocol.HttpsdProtocol;
import org.apache.hertzbeat.collector.collect.discovery.DiscoveryClient;
import org.apache.hertzbeat.collector.collect.discovery.constant.DiscoveryClientHealthStatus;
import org.apache.hertzbeat.collector.collect.discovery.entity.ConnectConfig;
import org.apache.hertzbeat.collector.collect.discovery.entity.ServerInfo;
import org.apache.hertzbeat.collector.collect.discovery.entity.ServiceInstance;
import org.apache.hertzbeat.common.entity.job.protocol.DiscoveryProtocol;

/**
* DiscoveryClient impl of Nacos
Expand All @@ -43,10 +44,10 @@ public class NacosDiscoveryClient implements DiscoveryClient {
private ConnectConfig localConnectConfig;

@Override
public ConnectConfig buildConnectConfig(HttpsdProtocol httpsdProtocol) {
public ConnectConfig buildConnectConfig(DiscoveryProtocol discoveryProtocol) {
return ConnectConfig.builder()
.host(httpsdProtocol.getHost())
.port(Integer.parseInt(httpsdProtocol.getPort()))
.host(discoveryProtocol.getHost())
.port(Integer.parseInt(discoveryProtocol.getPort()))
.build();
}

Expand Down
Loading
Loading