Skip to content

Commit

Permalink
[refactor] change name from http_sd to registry (apache#2827)
Browse files Browse the repository at this point in the history
Co-authored-by: Zhang Yuxuan <[email protected]>
  • Loading branch information
Calvin979 and zuobiao-zhou authored Nov 22, 2024
1 parent ca9934e commit 12f4ecc
Show file tree
Hide file tree
Showing 18 changed files with 106 additions and 110 deletions.
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;
package org.apache.hertzbeat.collector.collect.registry;

import com.ecwid.consul.transport.TransportException;
import com.google.common.annotations.VisibleForTesting;
Expand All @@ -27,22 +27,22 @@
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.collect.registry.discovery.DiscoveryClient;
import org.apache.hertzbeat.collector.collect.registry.discovery.DiscoveryClientManagement;
import org.apache.hertzbeat.collector.collect.registry.discovery.entity.ServerInfo;
import org.apache.hertzbeat.collector.constants.CollectorConstants;
import org.apache.hertzbeat.collector.dispatch.DispatchConstants;
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.job.protocol.RegistryProtocol;
import org.apache.hertzbeat.common.entity.message.CollectRep;
import org.apache.hertzbeat.common.util.CommonUtil;

/**
* http_sd protocol collection implementation
* registry protocol collection implementation
*/
@Slf4j
public class HttpsdImpl extends AbstractCollect {
public class RegistryImpl 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()){
throw new IllegalArgumentException("http_sd collect must have a valid http_sd protocol param! ");
RegistryProtocol registryProtocol = metrics.getRegistry();
if (Objects.isNull(registryProtocol) || registryProtocol.isInvalid()){
throw new IllegalArgumentException("registry collect must have a valid registry protocol param! ");
}
}

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

try (DiscoveryClient discoveryClient = discoveryClientManagement.getClient(httpsdProtocol)) {
try (DiscoveryClient discoveryClient = discoveryClientManagement.getClient(registryProtocol)) {
collectMetrics(builder, metrics, discoveryClient);
} catch (TransportException e1) {
String errorMsg = "Consul " + CommonUtil.getMessageFromThrowable(e1);
Expand Down Expand Up @@ -103,11 +103,7 @@ private void collectMetrics(CollectRep.MetricsData.Builder builder, Metrics metr

@Override
public String supportProtocol() {
return DispatchConstants.PROTOCOL_HTTP_SD;
}

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

private void addColumnIfMatched(String fieldName, Object sourceObj, CollectRep.ValueRow.Builder valueRowBuilder) {
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.registry.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.registry.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,13 +17,13 @@
* under the License.
*/

package org.apache.hertzbeat.collector.collect.httpsd.discovery;
package org.apache.hertzbeat.collector.collect.registry.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.registry.discovery.entity.ConnectConfig;
import org.apache.hertzbeat.collector.collect.registry.discovery.entity.ServerInfo;
import org.apache.hertzbeat.collector.collect.registry.discovery.entity.ServiceInstance;
import org.apache.hertzbeat.common.entity.job.protocol.RegistryProtocol;

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

/**
* Build connect config.
* @param httpsdProtocol httpsd protocol.
* @param registryProtocol registry protocol.
* @return connect config object.
*/
ConnectConfig buildConnectConfig(HttpsdProtocol httpsdProtocol);
ConnectConfig buildConnectConfig(RegistryProtocol registryProtocol);

/**
* 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.registry.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.registry.constant.DiscoveryClientInstance;
import org.apache.hertzbeat.collector.collect.registry.discovery.impl.ConsulDiscoveryClient;
import org.apache.hertzbeat.collector.collect.registry.discovery.impl.NacosDiscoveryClient;
import org.apache.hertzbeat.common.entity.job.protocol.RegistryProtocol;

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

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

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

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

private DiscoveryClient doCreateClient(HttpsdProtocol httpsdProtocol, DiscoveryClientInstance discoveryClientInstance) {
private DiscoveryClient doCreateClient(RegistryProtocol registryProtocol, 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(registryProtocol));
return discoveryClient;
}
}
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.registry.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.registry.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.registry.discovery.entity;

import java.util.Map;
import lombok.AllArgsConstructor;
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.impl;
package org.apache.hertzbeat.collector.collect.registry.discovery.impl;

import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.agent.model.Check;
Expand All @@ -28,11 +28,11 @@
import java.util.List;
import java.util.Map;
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.collector.collect.registry.discovery.DiscoveryClient;
import org.apache.hertzbeat.collector.collect.registry.discovery.entity.ConnectConfig;
import org.apache.hertzbeat.collector.collect.registry.discovery.entity.ServerInfo;
import org.apache.hertzbeat.collector.collect.registry.discovery.entity.ServiceInstance;
import org.apache.hertzbeat.common.entity.job.protocol.RegistryProtocol;

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

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

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.impl;
package org.apache.hertzbeat.collector.collect.registry.discovery.impl;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingFactory;
Expand All @@ -27,12 +27,12 @@
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.registry.constant.DiscoveryClientHealthStatus;
import org.apache.hertzbeat.collector.collect.registry.discovery.DiscoveryClient;
import org.apache.hertzbeat.collector.collect.registry.discovery.entity.ConnectConfig;
import org.apache.hertzbeat.collector.collect.registry.discovery.entity.ServerInfo;
import org.apache.hertzbeat.collector.collect.registry.discovery.entity.ServiceInstance;
import org.apache.hertzbeat.common.entity.job.protocol.RegistryProtocol;

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@
* limitations under the License.
*/

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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.util.ArrayList;
import java.util.List;
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.collect.httpsd.discovery.entity.ServiceInstance;
import org.apache.hertzbeat.collector.collect.registry.discovery.DiscoveryClient;
import org.apache.hertzbeat.collector.collect.registry.discovery.DiscoveryClientManagement;
import org.apache.hertzbeat.collector.collect.registry.discovery.entity.ServerInfo;
import org.apache.hertzbeat.collector.collect.registry.discovery.entity.ServiceInstance;
import org.apache.hertzbeat.common.entity.job.Metrics;
import org.apache.hertzbeat.common.entity.job.protocol.HttpsdProtocol;
import org.apache.hertzbeat.common.entity.job.protocol.RegistryProtocol;
import org.apache.hertzbeat.common.entity.message.CollectRep;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -37,13 +37,13 @@
import org.mockito.junit.jupiter.MockitoExtension;

/**
* Test case for {@link HttpsdImpl}
* Test case for {@link RegistryImpl}
*/
@ExtendWith(MockitoExtension.class)
class HttpsdImplTest {
class RegistryImplTest {
@InjectMocks
@Spy
private HttpsdImpl httpsd;
private RegistryImpl registry;

@Mock
private DiscoveryClient client;
Expand All @@ -57,7 +57,7 @@ void testServerCollect() {

String port = "123";
String host = "127.0.0.1";
HttpsdProtocol httpsdProtocol = HttpsdProtocol.builder()
RegistryProtocol registryProtocol = RegistryProtocol.builder()
.port(port)
.host(host)
.discoveryClientTypeName("consul")
Expand All @@ -68,18 +68,18 @@ void testServerCollect() {
aliasField.add("responseTime");
Metrics metrics = new Metrics();
metrics.setName("server");
metrics.setHttpsd(httpsdProtocol);
metrics.setRegistry(registryProtocol);
metrics.setAliasFields(aliasField);

Mockito.when(discoveryClientManagement.getClient(httpsdProtocol)).thenReturn(client);
Mockito.when(discoveryClientManagement.getClient(registryProtocol)).thenReturn(client);
ServerInfo serverInfo = ServerInfo.builder()
.address(host)
.port(port)
.build();
Mockito.when(client.getServerInfo()).thenReturn(serverInfo);
httpsd.setDiscoveryClientManagement(discoveryClientManagement);
httpsd.preCheck(metrics);
httpsd.collect(builder, 1L, "test", metrics);
registry.setDiscoveryClientManagement(discoveryClientManagement);
registry.preCheck(metrics);
registry.collect(builder, 1L, "test", metrics);
for (CollectRep.ValueRow valueRow : builder.getValuesList()) {
assertEquals(host, valueRow.getColumns(0));
assertEquals(port, valueRow.getColumns(1));
Expand All @@ -93,7 +93,7 @@ void testServiceCollect() {

String port = "123";
String host = "127.0.0.1";
HttpsdProtocol httpsdProtocol = HttpsdProtocol.builder()
RegistryProtocol registryProtocol = RegistryProtocol.builder()
.port(port)
.host(host)
.discoveryClientTypeName("consul")
Expand All @@ -105,10 +105,10 @@ void testServiceCollect() {
aliasField.add("port");
Metrics metrics = new Metrics();
metrics.setName("service");
metrics.setHttpsd(httpsdProtocol);
metrics.setRegistry(registryProtocol);
metrics.setAliasFields(aliasField);

Mockito.when(discoveryClientManagement.getClient(httpsdProtocol)).thenReturn(client);
Mockito.when(discoveryClientManagement.getClient(registryProtocol)).thenReturn(client);

String serviceId = "test";
String serviceName = "service";
Expand All @@ -121,9 +121,9 @@ void testServiceCollect() {
.build());

Mockito.when(client.getServices()).thenReturn(serviceInstances);
httpsd.setDiscoveryClientManagement(discoveryClientManagement);
httpsd.preCheck(metrics);
httpsd.collect(builder, 1L, "test", metrics);
registry.setDiscoveryClientManagement(discoveryClientManagement);
registry.preCheck(metrics);
registry.collect(builder, 1L, "test", metrics);
assertEquals(builder.getValuesCount(), 1);
for (CollectRep.ValueRow valueRow : builder.getValuesList()) {
assertEquals(serviceId, valueRow.getColumns(0));
Expand Down
Loading

0 comments on commit 12f4ecc

Please sign in to comment.