Skip to content

Commit fd4f19f

Browse files
author
Auri Munoz
committed
Refactors
1 parent a1d0905 commit fd4f19f

File tree

3 files changed

+43
-21
lines changed

3 files changed

+43
-21
lines changed

extensions/smallrye-stork/deployment/src/main/java/io/quarkus/stork/deployment/SmallRyeStorkProcessor.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,24 +111,28 @@ void checkStorkConsulRegistrar(BuildProducer<StorkRegistrationBuildItem> registr
111111
BuildProducer<RunTimeConfigurationDefaultBuildItem> config,
112112
StorkRegistrarConfigRecorder registrarConfigRecorder, StorkConfiguration configuration, Capabilities capabilities,
113113
CombinedIndexBuildItem index) {
114-
String smallryeHealthCheckDefaultUrl = "";
114+
String smallryeHealthCheckDefaultPath = getDefaultHealthCheckPath(capabilities, ConfigProvider.getConfig());
115115
if (QuarkusClassLoader.isClassPresentAtRuntime(CONSUL_SERVICE_REGISTRAR_PROVIDER)) {
116-
if (capabilities.isPresent(Capability.SMALLRYE_HEALTH)) {
117-
Config quarkusConfig = ConfigProvider.getConfig();
118-
smallryeHealthCheckDefaultUrl = quarkusConfig.getConfigValue("quarkus.management.root-path").getValue() + "/"
119-
+ quarkusConfig.getConfigValue("quarkus.smallrye-health.root-path").getValue() + "/"
120-
+ quarkusConfig.getConfigValue("quarkus.smallrye-health.liveness-path").getValue();
121-
}
122116
registrarConfigRecorder.setupServiceRegistrarConfig(configuration, CONSUL_SERVICE_REGISTRAR_TYPE,
123-
smallryeHealthCheckDefaultUrl);
117+
smallryeHealthCheckDefaultPath);
124118
} else if (QuarkusClassLoader.isClassPresentAtRuntime(EUREKA_SERVICE_REGISTRAR_PROVIDER)) {
125119
registrarConfigRecorder.setupServiceRegistrarConfig(configuration, EUREKA_SERVICE_REGISTRAR_TYPE,
126-
smallryeHealthCheckDefaultUrl);
120+
smallryeHealthCheckDefaultPath);
127121
}
128122
registration.produce(new StorkRegistrationBuildItem());
129123

130124
}
131125

126+
private static String getDefaultHealthCheckPath(Capabilities capabilities, Config quarkusConfig) {
127+
String smallryeHealthCheckDefaultPath = "";
128+
if (capabilities.isPresent(Capability.SMALLRYE_HEALTH)) {
129+
smallryeHealthCheckDefaultPath = quarkusConfig.getConfigValue("quarkus.management.root-path").getValue() + "/"
130+
+ quarkusConfig.getConfigValue("quarkus.smallrye-health.root-path").getValue() + "/"
131+
+ quarkusConfig.getConfigValue("quarkus.smallrye-health.liveness-path").getValue();
132+
}
133+
return smallryeHealthCheckDefaultPath;
134+
}
135+
132136
@BuildStep
133137
@Record(ExecutionTime.RUNTIME_INIT)
134138
void registerServiceInstance(StorkInitializedBuildItem storkInitializedBuildItem, ShutdownContextBuildItem shutdown,

extensions/smallrye-stork/runtime/src/main/java/io/quarkus/stork/SmallRyeStorkRegistrationRecorder.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.quarkus.stork;
22

3-
import java.net.InetAddress;
43
import java.util.List;
54
import java.util.Map;
65

@@ -28,14 +27,9 @@ public void registerServiceInstance(StorkConfiguration configuration) {
2827
}
2928
}
3029
Map<String, String> parameters = serviceConfig.serviceRegistrar().parameters();
31-
String host = parameters.containsKey("ip-address") ? parameters.get("ip-address")
32-
: quarkusConfig.getValue("quarkus.http.host", String.class);
33-
int port = parameters.containsKey("port") ? Integer.parseInt(parameters.get("port"))
34-
: Integer.parseInt(quarkusConfig.getValue("quarkus.http.port", String.class));
35-
if (host == null || host.isEmpty()) {
36-
InetAddress inetAddress = StorkConfigUtil.detectAddress();
37-
host = inetAddress != null ? inetAddress.getHostAddress() : host;
38-
}
30+
String host = StorkConfigUtil.getOrDefaultHost(parameters,
31+
quarkusConfig);
32+
int port = StorkConfigUtil.getOrDefaultPort(parameters, quarkusConfig);
3933
Stork.getInstance().getService(serviceName).getServiceRegistrar().registerServiceInstance(serviceName, host,
4034
port).await().indefinitely();
4135
}

extensions/smallrye-stork/runtime/src/main/java/io/quarkus/stork/StorkConfigUtil.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import java.util.Optional;
1313
import java.util.Set;
1414

15+
import org.eclipse.microprofile.config.Config;
16+
import org.eclipse.microprofile.config.ConfigProvider;
1517
import org.jboss.logging.Logger;
1618

1719
import io.smallrye.stork.api.config.ServiceConfig;
@@ -20,6 +22,8 @@
2022
public class StorkConfigUtil {
2123

2224
private static final Logger LOGGER = Logger.getLogger(StorkConfigUtil.class.getName());
25+
public static final String HTTPS = "https://";
26+
public static final String QUARKUS_HTTP_HOST = "quarkus.http.host";
2327

2428
public static List<ServiceConfig> toStorkServiceConfig(StorkConfiguration storkConfiguration) {
2529
List<ServiceConfig> storkServicesConfigs = new ArrayList<>();
@@ -48,10 +52,14 @@ public static List<ServiceConfig> toStorkServiceConfig(StorkConfiguration storkC
4852
return storkServicesConfigs;
4953
}
5054

51-
public static ServiceConfiguration buildDefaultRegistrarConfiguration(String serviceRegistrarType, String healthCheckUrl) {
55+
public static ServiceConfiguration buildDefaultRegistrarConfiguration(String serviceRegistrarType, String healthCheckPath) {
5256
Map<String, String> parameters = new HashMap<>();
53-
if (healthCheckUrl != null && !healthCheckUrl.isBlank()) {
54-
parameters.put("health-check-url", healthCheckUrl);
57+
Config quarkusConfig = ConfigProvider.getConfig();
58+
String defaultHost = quarkusConfig.getValue(QUARKUS_HTTP_HOST, String.class);
59+
if (healthCheckPath != null && !healthCheckPath.isBlank()) {
60+
healthCheckPath = HTTPS + getOrDefaultHost(parameters, quarkusConfig) + ":"
61+
+ getOrDefaultPort(parameters, quarkusConfig) + healthCheckPath;
62+
parameters.put("health-check-url", healthCheckPath);
5563
}
5664
return buildServiceConfigurationWithRegistrar(serviceRegistrarType, parameters);
5765
}
@@ -106,6 +114,22 @@ public Map<String, String> parameters() {
106114
};
107115
}
108116

117+
public static String getOrDefaultHost(Map<String, String> parameters, Config quarkusConfig) {
118+
String customHost = parameters.containsKey("ip-address") ? parameters.get("ip-address")
119+
: null;
120+
String defaultHost = quarkusConfig.getValue("quarkus.http.host", String.class);
121+
if (customHost == null || customHost.isEmpty()) {
122+
InetAddress inetAddress = StorkConfigUtil.detectAddress();
123+
customHost = inetAddress != null ? inetAddress.getHostAddress() : defaultHost;
124+
}
125+
return customHost;
126+
}
127+
128+
public static int getOrDefaultPort(Map<String, String> parameters, Config quarkusConfig) {
129+
String customPort = parameters.getOrDefault("port", quarkusConfig.getValue("quarkus.http.port", String.class));
130+
return Integer.parseInt(customPort);
131+
}
132+
109133
public static InetAddress detectAddress() {
110134
InetAddress result = null;
111135
try {

0 commit comments

Comments
 (0)