Skip to content

Commit 2cc8c34

Browse files
authored
Do not create http refresh bean when using messaging services
Signed-off-by: wind57 <eugen.rabii@gmail.com>
1 parent 2b9709b commit 2cc8c34

File tree

3 files changed

+148
-2
lines changed

3 files changed

+148
-2
lines changed

spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/src/main/java/org/springframework/cloud/kubernetes/configuration/watcher/ConfigurationWatcherConfigurationProperties.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2020 the original author or authors.
2+
* Copyright 2013-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,6 +38,11 @@ public class ConfigurationWatcherConfigurationProperties {
3838
*/
3939
public static final String KAFKA = "bus-kafka";
4040

41+
/**
42+
* not AMQP or KAFKA profile name.
43+
*/
44+
static final String NOT_AMQP_NOT_KAFKA = "!" + AMQP + " & !" + KAFKA;
45+
4146
/**
4247
* label to enable refresh/restart when using configmaps.
4348
*/

spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/src/main/java/org/springframework/cloud/kubernetes/configuration/watcher/RefreshTriggerAutoConfiguration.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2024 the original author or authors.
2+
* Copyright 2013-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,6 +29,7 @@
2929

3030
import static org.springframework.cloud.kubernetes.configuration.watcher.ConfigurationWatcherConfigurationProperties.AMQP;
3131
import static org.springframework.cloud.kubernetes.configuration.watcher.ConfigurationWatcherConfigurationProperties.KAFKA;
32+
import static org.springframework.cloud.kubernetes.configuration.watcher.ConfigurationWatcherConfigurationProperties.NOT_AMQP_NOT_KAFKA;
3233

3334
/**
3435
* @author wind57
@@ -47,6 +48,7 @@ BusRefreshTrigger busRefreshTrigger(ApplicationEventPublisher applicationEventPu
4748

4849
@Bean
4950
@ConditionalOnMissingBean
51+
@Profile({ NOT_AMQP_NOT_KAFKA })
5052
HttpRefreshTrigger httpRefreshTrigger(KubernetesInformerReactiveDiscoveryClient client,
5153
ConfigurationWatcherConfigurationProperties properties, WebClient webClient) {
5254
return new HttpRefreshTrigger(client, properties, webClient);
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Copyright 2013-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.configuration.watcher;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.mockito.Mockito;
21+
22+
import org.springframework.boot.test.context.TestConfiguration;
23+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
24+
import org.springframework.cloud.bus.BusProperties;
25+
import org.springframework.cloud.kubernetes.client.discovery.reactive.KubernetesInformerReactiveDiscoveryClient;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.context.annotation.Primary;
28+
import org.springframework.web.reactive.function.client.WebClient;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
import static org.springframework.cloud.kubernetes.configuration.watcher.ConfigurationWatcherConfigurationProperties.AMQP;
32+
import static org.springframework.cloud.kubernetes.configuration.watcher.ConfigurationWatcherConfigurationProperties.KAFKA;
33+
34+
/**
35+
* @author wind57
36+
*/
37+
class RefreshTriggerAutoConfigurationTests {
38+
39+
private ApplicationContextRunner applicationContextRunner;
40+
41+
@Test
42+
void amqpOnly() {
43+
setup(AMQP);
44+
applicationContextRunner.run(context -> {
45+
assertThat(context).hasSingleBean(BusRefreshTrigger.class);
46+
assertThat(context).doesNotHaveBean(HttpRefreshTrigger.class);
47+
});
48+
}
49+
50+
@Test
51+
void kafkaOnly() {
52+
setup(KAFKA);
53+
applicationContextRunner.run(context -> {
54+
assertThat(context).hasSingleBean(BusRefreshTrigger.class);
55+
assertThat(context).doesNotHaveBean(HttpRefreshTrigger.class);
56+
});
57+
}
58+
59+
@Test
60+
void kafkaAndAmqp() {
61+
setup(KAFKA + " , " + AMQP);
62+
applicationContextRunner.run(context -> {
63+
assertThat(context).hasSingleBean(BusRefreshTrigger.class);
64+
assertThat(context).doesNotHaveBean(HttpRefreshTrigger.class);
65+
});
66+
}
67+
68+
@Test
69+
void notAmqp() {
70+
setup("not-" + AMQP);
71+
applicationContextRunner.run(context -> {
72+
assertThat(context).doesNotHaveBean(BusRefreshTrigger.class);
73+
assertThat(context).hasSingleBean(HttpRefreshTrigger.class);
74+
});
75+
}
76+
77+
@Test
78+
void notKafka() {
79+
setup("not-" + KAFKA);
80+
applicationContextRunner.run(context -> {
81+
assertThat(context).doesNotHaveBean(BusRefreshTrigger.class);
82+
assertThat(context).hasSingleBean(HttpRefreshTrigger.class);
83+
});
84+
}
85+
86+
@Test
87+
void amqpNotKafka() {
88+
setup(AMQP + "," + "not-" + KAFKA);
89+
applicationContextRunner.run(context -> {
90+
assertThat(context).hasSingleBean(BusRefreshTrigger.class);
91+
assertThat(context).doesNotHaveBean(HttpRefreshTrigger.class);
92+
});
93+
}
94+
95+
@Test
96+
void kafkaNotAmqp() {
97+
setup(KAFKA + "," + "not-" + AMQP);
98+
applicationContextRunner.run(context -> {
99+
assertThat(context).hasSingleBean(BusRefreshTrigger.class);
100+
assertThat(context).doesNotHaveBean(HttpRefreshTrigger.class);
101+
});
102+
}
103+
104+
private void setup(String activeProfiles) {
105+
applicationContextRunner = new ApplicationContextRunner()
106+
.withUserConfiguration(TestConfig.class, RefreshTriggerAutoConfiguration.class)
107+
.withPropertyValues("spring.main.cloud-platform=kubernetes", "spring.profiles.active=" + activeProfiles);
108+
}
109+
110+
@TestConfiguration
111+
static class TestConfig {
112+
113+
@Bean
114+
@Primary
115+
BusProperties busProperties() {
116+
return new BusProperties();
117+
}
118+
119+
@Bean
120+
@Primary
121+
KubernetesInformerReactiveDiscoveryClient client() {
122+
return Mockito.mock(KubernetesInformerReactiveDiscoveryClient.class);
123+
}
124+
125+
@Bean
126+
@Primary
127+
ConfigurationWatcherConfigurationProperties configurationWatcherConfigurationProperties() {
128+
return new ConfigurationWatcherConfigurationProperties();
129+
}
130+
131+
@Primary
132+
@Bean
133+
WebClient webClient() {
134+
return Mockito.mock(WebClient.class);
135+
}
136+
137+
}
138+
139+
}

0 commit comments

Comments
 (0)