Skip to content

Commit f85332b

Browse files
committed
Add availability conditions for discovery clients
Introduce composed conditional annotations for blocking and reactive discovery availability and update auto-configs to use them. Adds ConditionalOnBlockingDiscoveryAvailable and ConditionalOnReactiveDiscoveryAvailable to encapsulate ConditionalOnClass + ConditionalOnDiscoveryEnabled + the respective blocking/reactive enablement checks, replaces repetitive annotations in DiscoveryClientAutoConfiguration and ReactiveDiscoveryClientAutoConfiguration, and adds REACTIVE_DISCOVERY_CLIENT_CLASS_NAME constant to DiscoveryClientConstants.
1 parent 0bf2437 commit f85332b

5 files changed

Lines changed: 152 additions & 10 deletions

File tree

microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/discovery/autoconfigure/DiscoveryClientAutoConfiguration.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import io.microsphere.annotation.ConfigurationProperty;
2020
import io.microsphere.spring.cloud.client.discovery.UnionDiscoveryClient;
21+
import io.microsphere.spring.cloud.client.discovery.condition.ConditionalOnBlockingDiscoveryAvailable;
2122
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
2223
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2324
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@@ -42,11 +43,7 @@
4243
* @since 1.0.0
4344
*/
4445
@Configuration(proxyBeanMethods = false)
45-
@ConditionalOnClass(name = {
46-
DISCOVERY_CLIENT_CLASS_NAME
47-
})
48-
@ConditionalOnDiscoveryEnabled
49-
@ConditionalOnBlockingDiscoveryEnabled
46+
@ConditionalOnBlockingDiscoveryAvailable
5047
@AutoConfigureBefore(name = {
5148
COMMONS_CLIENT_AUTO_CONFIGURATION_CLASS_NAME
5249
})

microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/discovery/autoconfigure/ReactiveDiscoveryClientAutoConfiguration.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package io.microsphere.spring.cloud.client.discovery.autoconfigure;
1919

2020
import io.microsphere.spring.cloud.client.discovery.ReactiveDiscoveryClientAdapter;
21+
import io.microsphere.spring.cloud.client.discovery.condition.ConditionalOnReactiveDiscoveryAvailable;
2122
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2223
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
2324
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
@@ -43,11 +44,7 @@
4344
* @since 1.0.0
4445
*/
4546
@Configuration(proxyBeanMethods = false)
46-
@ConditionalOnClass(name = {
47-
DISCOVERY_CLIENT_CLASS_NAME
48-
})
49-
@ConditionalOnDiscoveryEnabled
50-
@ConditionalOnReactiveDiscoveryEnabled
47+
@ConditionalOnReactiveDiscoveryAvailable
5148
@AutoConfigureBefore(name = {
5249
REACTIVE_COMMONS_CLIENT_AUTO_CONFIGURATION_CLASS_NAME
5350
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package io.microsphere.spring.cloud.client.discovery.condition;
19+
20+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
21+
import org.springframework.cloud.client.ConditionalOnBlockingDiscoveryEnabled;
22+
import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled;
23+
24+
import java.lang.annotation.Documented;
25+
import java.lang.annotation.Inherited;
26+
import java.lang.annotation.Retention;
27+
import java.lang.annotation.Target;
28+
29+
import static io.microsphere.spring.cloud.client.discovery.constants.DiscoveryClientConstants.DISCOVERY_CLIENT_CLASS_NAME;
30+
import static java.lang.annotation.ElementType.METHOD;
31+
import static java.lang.annotation.ElementType.TYPE;
32+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
33+
34+
/**
35+
* A condition that checks if Blocking Spring Cloud Discovery is available.
36+
* <p>
37+
* This annotation combines {@link ConditionalOnClass} (checking for the presence of {@code DiscoveryClient}),
38+
* {@link ConditionalOnDiscoveryEnabled}, and {@link ConditionalOnBlockingDiscoveryEnabled} to ensure that
39+
* the discovery client is both present in the classpath and enabled for blocking operations.
40+
*
41+
* <h3>Example Usage:</h3>
42+
* <pre>{@code
43+
* @Configuration
44+
* @ConditionalOnDiscoveryAvailable
45+
* public class DiscoveryClientConfiguration {
46+
*
47+
* @Bean
48+
* public MyService myService(DiscoveryClient discoveryClient) {
49+
* return new MyService(discoveryClient);
50+
* }
51+
* }
52+
* }</pre>
53+
*
54+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
55+
* @see ConditionalOnDiscoveryEnabled
56+
* @see ConditionalOnBlockingDiscoveryEnabled
57+
* @see ConditionalOnClass
58+
* @since 1.0.0
59+
*/
60+
@Target({TYPE, METHOD})
61+
@Retention(RUNTIME)
62+
@Documented
63+
@Inherited
64+
@ConditionalOnClass(name = {
65+
DISCOVERY_CLIENT_CLASS_NAME
66+
})
67+
@ConditionalOnDiscoveryEnabled
68+
@ConditionalOnBlockingDiscoveryEnabled
69+
public @interface ConditionalOnBlockingDiscoveryAvailable {
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package io.microsphere.spring.cloud.client.discovery.condition;
19+
20+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
21+
import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled;
22+
import org.springframework.cloud.client.ConditionalOnReactiveDiscoveryEnabled;
23+
24+
import java.lang.annotation.Documented;
25+
import java.lang.annotation.Inherited;
26+
import java.lang.annotation.Retention;
27+
import java.lang.annotation.Target;
28+
29+
import static io.microsphere.spring.cloud.client.discovery.constants.DiscoveryClientConstants.REACTIVE_DISCOVERY_CLIENT_CLASS_NAME;
30+
import static java.lang.annotation.ElementType.METHOD;
31+
import static java.lang.annotation.ElementType.TYPE;
32+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
33+
34+
/**
35+
* {@link ConditionalOnReactiveDiscoveryEnabled} and {@link ConditionalOnClass} for Reactive Discovery Client.
36+
* <p>
37+
* This annotation can be used to conditionally enable a bean or configuration only when:
38+
* <ul>
39+
* <li>The Spring Cloud Discovery is enabled ({@link ConditionalOnDiscoveryEnabled}).</li>
40+
* <li>The Reactive Discovery is explicitly enabled ({@link ConditionalOnReactiveDiscoveryEnabled}).</li>
41+
* <li>The Reactive Discovery Client class is present on the classpath.</li>
42+
* </ul>
43+
*
44+
* <h3>Example Usage:</h3>
45+
* <pre>{@code
46+
* @Configuration
47+
* public class ReactiveDiscoveryConfig {
48+
*
49+
* @Bean
50+
* @ConditionalOnReactiveDiscoveryAvailable
51+
* public MyReactiveService myReactiveService(ReactiveDiscoveryClient client) {
52+
* return new MyReactiveService(client);
53+
* }
54+
* }
55+
* }</pre>
56+
*
57+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
58+
* @since 1.0.0
59+
*/
60+
@Target({TYPE, METHOD})
61+
@Retention(RUNTIME)
62+
@Documented
63+
@Inherited
64+
@ConditionalOnClass(name = {
65+
REACTIVE_DISCOVERY_CLIENT_CLASS_NAME
66+
})
67+
@ConditionalOnDiscoveryEnabled
68+
@ConditionalOnReactiveDiscoveryEnabled
69+
public @interface ConditionalOnReactiveDiscoveryAvailable {
70+
}

microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/discovery/constants/DiscoveryClientConstants.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.springframework.cloud.client.CommonsClientAutoConfiguration;
2020
import org.springframework.cloud.client.ReactiveCommonsClientAutoConfiguration;
2121
import org.springframework.cloud.client.discovery.DiscoveryClient;
22+
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
2223
import org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClient;
2324
import org.springframework.cloud.client.discovery.composite.reactive.ReactiveCompositeDiscoveryClientAutoConfiguration;
2425
import org.springframework.cloud.client.discovery.simple.reactive.SimpleReactiveDiscoveryClientAutoConfiguration;
@@ -38,6 +39,13 @@ public interface DiscoveryClientConstants {
3839
*/
3940
String DISCOVERY_CLIENT_CLASS_NAME = "org.springframework.cloud.client.discovery.DiscoveryClient";
4041

42+
/**
43+
* The class name of {@link ReactiveDiscoveryClient}
44+
*
45+
* @see org.springframework.cloud.client.discovery.ReactiveDiscoveryClient
46+
*/
47+
String REACTIVE_DISCOVERY_CLIENT_CLASS_NAME = "org.springframework.cloud.client.discovery.ReactiveDiscoveryClient";
48+
4149
/**
4250
* The class name of {@link CompositeDiscoveryClient}
4351
*

0 commit comments

Comments
 (0)