-
Notifications
You must be signed in to change notification settings - Fork 826
Adding Consul as the Service Discovery and Configuration Center #4647
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
Conversation
dynamic-config/config-consul/pom.xml
Outdated
| <artifactId>java-chassis-core</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.springframework.boot</groupId> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why need this starter?
There was a problem hiding this comment.
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.
service-registry/pom.xml
Outdated
| <module>registry-nacos</module> | ||
| <module>registry-zookeeper</module> | ||
| <module>registry-etcd</module> | ||
| <module>registry-consul</module> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indention
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
demo/demo-consul/consumer/pom.xml
Outdated
| <dependency> | ||
| <groupId>com.google.protobuf</groupId> | ||
| <artifactId>protobuf-java</artifactId> | ||
| <version>3.25.5</version> |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
checkstyle,rat_check
linelint check
| } | ||
| } | ||
|
|
||
| @Resource |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
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.
...consul/src/main/java/org/apache/servicecomb/config/consul/ConsulDynamicPropertiesSource.java
Outdated
Show resolved
Hide resolved
| try { | ||
| consulConfigClient = consulConfigClient(environment); | ||
| ConsulConfigClientClear consulConfigClientClear = consulConfigClear(); | ||
| LOGGER.info("consulConfigClientClear getPhase:{}", consulConfigClientClear.getPhase()); |
There was a problem hiding this comment.
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
service-registry/pom.xml
Outdated
| <module>registry-nacos</module> | ||
| <module>registry-zookeeper</module> | ||
| <module>registry-etcd</module> | ||
| <module>registry-consul</module> |
There was a problem hiding this comment.
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()) |
There was a problem hiding this comment.
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.
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() { |
There was a problem hiding this comment.
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<>(); |
There was a problem hiding this comment.
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<>(); |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using LOGGER
| Future<String> result = this.executorService.submit(address::getHostName); | ||
|
|
||
| String hostname; | ||
| try { | ||
| hostname = result.get(1, TimeUnit.SECONDS); | ||
| } catch (Exception e) { | ||
| hostname = "localhost"; | ||
| } |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is serverPort required?
There was a problem hiding this comment.
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.
| 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())); |
There was a problem hiding this comment.
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()) |
There was a problem hiding this comment.
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
Add the registry-consul submodule to the service-registry module to register and discover Consul services.
Add the config-consul submodule under the dynamic-config module to implement the key-value of the consul as the configuration center.
Add demo-consul to the demo module to test the Consul service registration, discovery, and configuration center.