generated from micronaut-projects/micronaut-project-template
-
Notifications
You must be signed in to change notification settings - Fork 17
/
SpringCloudConfigurationClient.java
176 lines (159 loc) · 8.67 KB
/
SpringCloudConfigurationClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* Copyright 2017-2020 original authors
*
* Licensed 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
*
* https://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.
*/
package io.micronaut.discovery.spring.config;
import io.micronaut.context.annotation.BootstrapContextCompatible;
import io.micronaut.context.annotation.Requires;
import io.micronaut.context.env.Environment;
import io.micronaut.context.env.EnvironmentPropertySource;
import io.micronaut.context.env.PropertySource;
import io.micronaut.context.exceptions.ConfigurationException;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.core.util.StringUtils;
import io.micronaut.discovery.config.ConfigurationClient;
import io.micronaut.discovery.spring.config.client.SpringCloudConfigClient;
import io.micronaut.discovery.spring.config.client.ConfigServerPropertySource;
import io.micronaut.discovery.spring.config.client.ConfigServerResponse;
import io.micronaut.http.HttpStatus;
import io.micronaut.http.client.exceptions.HttpClientResponseException;
import io.micronaut.runtime.ApplicationConfiguration;
import io.micronaut.scheduling.TaskExecutors;
import jakarta.inject.Named;
import jakarta.inject.Singleton;
import org.reactivestreams.Publisher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Flux;
import reactor.core.scheduler.Schedulers;
import java.util.*;
import java.util.concurrent.ExecutorService;
/**
* A {@link ConfigurationClient} for Spring Cloud client.
*
* @author Thiago Locatelli
* @author graemerocher
* @since 1.0
*/
@Singleton
@BootstrapContextCompatible
@Requires(beans = SpringCloudClientConfiguration.class)
public class SpringCloudConfigurationClient implements ConfigurationClient {
private static final Logger LOG = LoggerFactory.getLogger(SpringCloudConfigurationClient.class);
private final SpringCloudConfigClient springCloudConfigClient;
private final SpringCloudClientConfiguration springCloudConfiguration;
private final ApplicationConfiguration applicationConfiguration;
private ExecutorService executionService;
/**
* @param springCloudConfigClient The Spring Cloud client
* @param springCloudConfiguration The Spring Cloud configuration
* @param applicationConfiguration The application configuration
* @param executionService The executor service to use
*/
protected SpringCloudConfigurationClient(SpringCloudConfigClient springCloudConfigClient,
SpringCloudClientConfiguration springCloudConfiguration,
ApplicationConfiguration applicationConfiguration,
@Named(TaskExecutors.IO) @Nullable ExecutorService executionService) {
this.springCloudConfigClient = springCloudConfigClient;
this.springCloudConfiguration = springCloudConfiguration;
this.applicationConfiguration = applicationConfiguration;
this.executionService = executionService;
}
@Override
public Publisher<PropertySource> getPropertySources(Environment environment) {
if (!springCloudConfiguration.getConfiguration().isEnabled()) {
return Flux.empty();
}
Optional<String> configuredApplicationName = applicationConfiguration.getName();
if (configuredApplicationName.isEmpty()) {
return Flux.empty();
} else {
String applicationName = springCloudConfiguration.getName().orElse(configuredApplicationName.get());
Set<String> activeNames = environment.getActiveNames();
String profiles = StringUtils.trimToNull(String.join(",", activeNames));
if (LOG.isDebugEnabled()) {
LOG.debug("Spring Cloud Config Active: {}", springCloudConfiguration.getUri());
LOG.debug("Application Name: {}, Application Profiles: {}, label: {}", applicationName, profiles,
springCloudConfiguration.getLabel());
}
String authorization = getAuthorization(springCloudConfiguration);
Publisher<ConfigServerResponse> responsePublisher;
if (authorization == null) {
responsePublisher =
springCloudConfiguration.getLabel() == null ?
springCloudConfigClient.readValues(applicationName, profiles) :
springCloudConfigClient.readValues(applicationName,
profiles, springCloudConfiguration.getLabel());
} else {
responsePublisher =
springCloudConfiguration.getLabel() == null ?
springCloudConfigClient.readValuesAuthorized(applicationName, profiles, authorization) :
springCloudConfigClient.readValuesAuthorized(applicationName,
profiles, springCloudConfiguration.getLabel(), authorization);
}
Flux<PropertySource> configurationValues = Flux.from(responsePublisher)
.onErrorResume(throwable -> {
if (throwable instanceof HttpClientResponseException httpClientResponseException && httpClientResponseException.getStatus() == HttpStatus.NOT_FOUND) {
if (springCloudConfiguration.isFailFast()) {
return Flux.error(
new ConfigurationException("Could not locate PropertySource and the fail fast property is set", throwable));
} else {
return Flux.empty();
}
}
return Flux.error(new ConfigurationException("Error reading distributed configuration from Spring Cloud: " + throwable.getMessage(), throwable));
})
.flatMap(response -> {
List<ConfigServerPropertySource> springSources = response.getPropertySources();
if (CollectionUtils.isEmpty(springSources)) {
return Flux.empty();
}
int baseOrder = EnvironmentPropertySource.POSITION + 100;
List<PropertySource> propertySources = new ArrayList<>(springSources.size());
//spring returns the property sources with the highest precedence first
//reverse order and increment priority so the last (after reversed) item will
//have the highest order
for (int i = springSources.size() - 1; i >= 0; i--) {
ConfigServerPropertySource springSource = springSources.get(i);
propertySources.add(PropertySource.of(springSource.getName(), springSource.getSource(), ++baseOrder));
}
return Flux.fromIterable(propertySources);
});
if (executionService != null) {
return configurationValues.subscribeOn(Schedulers.fromExecutor(executionService));
} else {
return configurationValues;
}
}
}
@Override
public final @NonNull String getDescription() {
return io.micronaut.discovery.spring.config.client.SpringCloudConfigClient.CLIENT_DESCRIPTION;
}
/**
* Gets Basic authorization from the spring cloud client configuration if both username and password are provided, otherwise returns null.
* @param springCloudConfiguration the spring cloud client configuration
* @return the Basic authorization header or null if no credentials are configured for spring cloud client
*/
private static String getAuthorization(SpringCloudClientConfiguration springCloudConfiguration) {
String authorization = null;
if (springCloudConfiguration.getUsername().isPresent() && springCloudConfiguration.getPassword().isPresent()) {
String basicAuth = springCloudConfiguration.getUsername().get() + ":" + springCloudConfiguration.getPassword().get();
authorization = "Basic " + Base64.getEncoder().encodeToString(basicAuth.getBytes());
}
return authorization;
}
}