Skip to content

Conversation

@felixgis14
Copy link
Contributor

  1. Add the registry-consul submodule to the service-registry module to register and discover Consul services.

  2. Add the config-consul submodule under the dynamic-config module to implement the key-value of the consul as the configuration center.

  3. Add demo-consul to the demo module to test the Consul service registration, discovery, and configuration center.

<artifactId>java-chassis-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why need this starter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

During development, I found that I could not use the @slf4j annotation to add logs. Later, I used the Logger method. I forgot to delete this dependency.

<module>registry-nacos</module>
<module>registry-zookeeper</module>
<module>registry-etcd</module>
<module>registry-consul</module>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indention

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.25.5</version>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it ptotobuf-java needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dependencies copied from other modules have been deleted

flygoods and others added 2 commits December 27, 2024 17:56
}
}

@Resource
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is constructor injection. Maybe it's not necessary for @Resource

consulConfig.getInstanceTag());

runTask(tagData, path);
this.tagData = parseData(path);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this line necessary? runTask has already updated tagData

}

private void runTask(Map<String, Object> serviceData, String path) {
watchFuture = taskScheduler.scheduleWithFixedDelay(new GetDataRunnable(serviceData, this, path),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to poll all type of configs in each job, not each type a job.

try {
consulConfigClient = consulConfigClient(environment);
ConsulConfigClientClear consulConfigClientClear = consulConfigClear();
LOGGER.info("consulConfigClientClear getPhase:{}", consulConfigClientClear.getPhase());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ConsulConfigClientClear can be deleted? Seems no use. And this code slip will create many ConsulConfigClient instance and is not correct

<module>registry-nacos</module>
<module>registry-zookeeper</module>
<module>registry-etcd</module>
<module>registry-consul</module>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indention

scheduledFuture = taskScheduler.scheduleWithFixedDelay(
() -> instanceChangedListener.onInstanceChanged(
name(), BootStrapProperties.readApplication(environment), serviceName, getInstances(serviceName, new QueryParams(consulDiscoveryProperties.getConsistencyMode()))
), Duration.ofMillis(consulDiscoveryProperties.getDelayTime())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call onInstanceChanged must base on instance list already changed. Make sure if consul can indication instance change.

felixgis14 and others added 5 commits January 1, 2025 00:35
Refactored service registration, service discovery, and configuration center using the new consul client
Refactored service registration, service discovery, and configuration center using the new consul client
@Bean
@ConditionalOnBean(value = {Consul.class, Environment.class, RegistrationId.class, DataCenterProperties.class})
@ConditionalOnMissingBean
public ConsulDiscovery etcdDiscovery() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consul

import java.util.concurrent.ConcurrentHashMap;

public class UpdateHandler {
private final Map<String, Object> valueCache = new ConcurrentHashMap<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

valueCache never used

public class ConsulDynamicPropertiesSource implements DynamicPropertiesSource {
public static final String SOURCE_NAME = "consul";

private final Map<String, Object> valueCache = new ConcurrentHashMap<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

valueCache never updated

if (!CollectionUtils.isEmpty(healthServices)) {
for (ServiceHealth serviceHealth : healthServices) {
Service service = serviceHealth.getService();
logger.info("healthService:{}", service.getId());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not print too many logs


public class ConsulDiscovery implements Discovery<ConsulDiscoveryInstance> {

private static final Logger logger = LoggerFactory.getLogger(ConsulDiscovery.class);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using LOGGER

Comment on lines 111 to 118
Future<String> result = this.executorService.submit(address::getHostName);

String hostname;
try {
hostname = result.get(1, TimeUnit.SECONDS);
} catch (Exception e) {
hostname = "localhost";
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why create a new thread to get host name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use EndpointUtils to obtain the IP address and port number.

String instanceId = registrationId.getInstanceId();
String serviceName = BootStrapProperties.readServiceName(environment);
consulInstance = new ConsulInstance();
consulInstance.setServiceId(serviceName + "-" + serverPort);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can serviceName be serviceId? Why add port?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a problem. In the case of multiple instances, IP addresses and ports are used to identify different services.

String serverPort;
if (restAddress.contains("?")) {
serverPort = restAddress.substring(restAddress.indexOf(":") + 1, restAddress.indexOf("?"));
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is serverPort required?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a problem. In the case of multiple instances, IP addresses and ports are used to identify different services.

Comment on lines 156 to 163
meta.put("serviceName", consulInstance.getServiceName());
meta.put("version", consulInstance.getVersion());
meta.put("instanceId", registrationId.getInstanceId());
meta.put("env", consulInstance.getEnvironment());
meta.put("application", consulInstance.getApplication());
meta.put("alias", consulInstance.getAlias() != null ? consulInstance.getAlias() : "");
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
meta.put("endpoints", gson.toJson(consulInstance.getEndpoints()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can add consulInstance as a json propertly, and code can be simpler. See Zookeeper for an example.

registrationBuilder = ImmutableRegistration.builder()
.id(consulInstance.getServiceId())
.name(consulInstance.getServiceName())
.address(consulDiscoveryProperties.getIpAddress())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See zookeeper/nacos implementation how to get ip address and port. Do not use the new InetUtils added. InetUtils implementation is not correct.

Service id
Service information storage
valueCache usage
@liubao68 liubao68 merged commit 2fc6d40 into apache:master Jan 3, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants