Skip to content

Commit d05e8d0

Browse files
authored
Merge pull request #84 from microsphere-projects/dev-1.x
Release 0.1.4
2 parents 336c90e + 6f35449 commit d05e8d0

23 files changed

+690
-57
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ pom.xml:
6262

6363
| **Branches** | **Purpose** | **Latest Version** |
6464
|--------------|--------------------------------------------------|--------------------|
65-
| **0.2.x** | Compatible with Spring Cloud 2022.0.x - 2025.0.x | 0.2.3 |
66-
| **0.1.x** | Compatible with Spring Cloud Hoxton - 2021.0.x | 0.1.3 |
65+
| **0.2.x** | Compatible with Spring Cloud 2022.0.x - 2025.0.x | 0.2.4 |
66+
| **0.1.x** | Compatible with Spring Cloud Hoxton - 2021.0.x | 0.1.4 |
6767

6868
Then add the specific modules you need:
6969

microsphere-spring-cloud-commons/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@
6767
<optional>true</optional>
6868
</dependency>
6969

70+
<dependency>
71+
<groupId>org.springframework.cloud</groupId>
72+
<artifactId>spring-cloud-loadbalancer</artifactId>
73+
<optional>true</optional>
74+
</dependency>
7075

7176
<!-- Netflix Eureka Client -->
7277
<dependency>

microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/condition/ConditionalOnFeaturesEnabled.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.springframework.cloud.client.CommonsClientAutoConfiguration;
2222
import org.springframework.cloud.client.actuator.FeaturesEndpoint;
2323
import org.springframework.cloud.client.actuator.HasFeatures;
24-
import org.springframework.core.annotation.AliasFor;
2524

2625
import java.lang.annotation.Documented;
2726
import java.lang.annotation.Retention;
@@ -47,15 +46,6 @@
4746
@Retention(RUNTIME)
4847
@Target({TYPE, METHOD})
4948
@Documented
50-
@ConditionalOnProperty(name = FEATURES_ENABLED_PROPERTY_NAME)
49+
@ConditionalOnProperty(name = FEATURES_ENABLED_PROPERTY_NAME, matchIfMissing = true)
5150
public @interface ConditionalOnFeaturesEnabled {
52-
53-
/**
54-
* Specify if the condition should match if the property is not set. Defaults to
55-
* {@code true}.
56-
*
57-
* @return if the condition should match if the property is missing
58-
*/
59-
@AliasFor(annotation = ConditionalOnProperty.class, attribute = "matchIfMissing")
60-
boolean matchIfMissing() default true;
6151
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@
2121
import org.springframework.cloud.client.discovery.DiscoveryClient;
2222
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
2323
import reactor.core.publisher.Flux;
24+
import reactor.core.publisher.Mono;
2425

2526
import java.util.List;
2627

28+
import static io.microsphere.lang.function.ThrowableSupplier.execute;
29+
import static reactor.core.scheduler.Schedulers.isInNonBlockingThread;
30+
2731
/**
2832
* An adapter {@link DiscoveryClient} class based on {@link ReactiveDiscoveryClient}
2933
*
@@ -67,6 +71,10 @@ public int getOrder() {
6771
}
6872

6973
static <T> List<T> toList(Flux<T> flux) {
70-
return flux.collectList().block();
74+
Mono<List<T>> mono = flux.collectList();
75+
if (isInNonBlockingThread()) {
76+
return execute(() -> mono.toFuture().get());
77+
}
78+
return mono.block();
7179
}
7280
}

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

Lines changed: 7 additions & 0 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 org.springframework.boot.autoconfigure.AutoConfigureAfter;
2122
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
2223
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2324
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -30,6 +31,8 @@
3031

3132
import static io.microsphere.spring.cloud.client.discovery.constants.DiscoveryClientConstants.DISCOVERY_CLIENT_CLASS_NAME;
3233
import static io.microsphere.spring.cloud.client.discovery.constants.DiscoveryClientConstants.REACTIVE_COMMONS_CLIENT_AUTO_CONFIGURATION_CLASS_NAME;
34+
import static io.microsphere.spring.cloud.client.discovery.constants.DiscoveryClientConstants.REACTIVE_COMPOSITE_DISCOVERY_CLIENT_AUTO_CONFIGURATION_CLASS_NAME;
35+
import static io.microsphere.spring.cloud.client.discovery.constants.DiscoveryClientConstants.SIMPLE_REACTIVE_DISCOVERY_CLIENT_AUTO_CONFIGURATION_CLASS_NAME;
3336

3437
/**
3538
* The Auto-Configuration class for {@link ReactiveDiscoveryClient}
@@ -48,6 +51,10 @@
4851
@AutoConfigureBefore(name = {
4952
REACTIVE_COMMONS_CLIENT_AUTO_CONFIGURATION_CLASS_NAME
5053
})
54+
@AutoConfigureAfter(name = {
55+
SIMPLE_REACTIVE_DISCOVERY_CLIENT_AUTO_CONFIGURATION_CLASS_NAME,
56+
REACTIVE_COMPOSITE_DISCOVERY_CLIENT_AUTO_CONFIGURATION_CLASS_NAME
57+
})
5158
public class ReactiveDiscoveryClientAutoConfiguration {
5259

5360
@Configuration(proxyBeanMethods = false)

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.springframework.cloud.client.ReactiveCommonsClientAutoConfiguration;
2121
import org.springframework.cloud.client.discovery.DiscoveryClient;
2222
import org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClient;
23+
import org.springframework.cloud.client.discovery.composite.reactive.ReactiveCompositeDiscoveryClientAutoConfiguration;
24+
import org.springframework.cloud.client.discovery.simple.reactive.SimpleReactiveDiscoveryClientAutoConfiguration;
2325

2426
/**
2527
* The constants for {@link DiscoveryClient}
@@ -56,4 +58,18 @@ public interface DiscoveryClientConstants {
5658
* @see org.springframework.cloud.client.ReactiveCommonsClientAutoConfiguration
5759
*/
5860
String REACTIVE_COMMONS_CLIENT_AUTO_CONFIGURATION_CLASS_NAME = "org.springframework.cloud.client.ReactiveCommonsClientAutoConfiguration";
61+
62+
/**
63+
* The class name of {@link SimpleReactiveDiscoveryClientAutoConfiguration}
64+
*
65+
* @see org.springframework.cloud.client.discovery.simple.reactive.SimpleReactiveDiscoveryClientAutoConfiguration
66+
*/
67+
String SIMPLE_REACTIVE_DISCOVERY_CLIENT_AUTO_CONFIGURATION_CLASS_NAME = "org.springframework.cloud.client.discovery.simple.reactive.SimpleReactiveDiscoveryClientAutoConfiguration";
68+
69+
/**
70+
* The class name of {@link ReactiveCompositeDiscoveryClientAutoConfiguration}
71+
*
72+
* @see org.springframework.cloud.client.discovery.composite.reactive.ReactiveCompositeDiscoveryClientAutoConfiguration
73+
*/
74+
String REACTIVE_COMPOSITE_DISCOVERY_CLIENT_AUTO_CONFIGURATION_CLASS_NAME = "org.springframework.cloud.client.discovery.composite.reactive.ReactiveCompositeDiscoveryClientAutoConfiguration";
5975
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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.util;
19+
20+
import io.microsphere.annotation.Nonnull;
21+
import io.microsphere.util.Utils;
22+
import org.springframework.cloud.client.DefaultServiceInstance;
23+
import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryProperties;
24+
import org.springframework.cloud.client.discovery.simple.reactive.SimpleReactiveDiscoveryProperties;
25+
26+
import java.util.List;
27+
import java.util.Map;
28+
29+
import static io.microsphere.reflect.MethodUtils.invokeMethod;
30+
import static io.microsphere.spring.cloud.client.service.util.ServiceInstanceUtils.setProperties;
31+
32+
/**
33+
* The utilities class for Spring Cloud Discovery
34+
*
35+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
36+
* @see Utils
37+
* @since 1.0.0
38+
*/
39+
public abstract class DiscoveryUtils implements Utils {
40+
41+
/**
42+
* Get the instances map from {@link SimpleDiscoveryProperties}
43+
*
44+
* @param properties {@link SimpleDiscoveryProperties}
45+
* @return the instances map
46+
*/
47+
@Nonnull
48+
public static Map<String, List<DefaultServiceInstance>> getInstancesMap(@Nonnull SimpleDiscoveryProperties properties) {
49+
return properties.getInstances();
50+
}
51+
52+
/**
53+
* Get the instances map from {@link SimpleReactiveDiscoveryProperties}
54+
*
55+
* @param properties {@link SimpleReactiveDiscoveryProperties}
56+
* @return the instances map
57+
*/
58+
@Nonnull
59+
public static Map<String, List<DefaultServiceInstance>> getInstancesMap(@Nonnull SimpleReactiveDiscoveryProperties properties) {
60+
return invokeMethod(properties, "getInstances");
61+
}
62+
63+
/**
64+
* Convert {@link SimpleDiscoveryProperties} to {@link SimpleReactiveDiscoveryProperties}
65+
*
66+
* @param properties {@link SimpleDiscoveryProperties}
67+
* @return {@link SimpleReactiveDiscoveryProperties}
68+
*/
69+
@Nonnull
70+
public static SimpleReactiveDiscoveryProperties simpleReactiveDiscoveryProperties(@Nonnull SimpleDiscoveryProperties properties) {
71+
SimpleReactiveDiscoveryProperties simpleReactiveDiscoveryProperties = new SimpleReactiveDiscoveryProperties();
72+
simpleReactiveDiscoveryProperties.setOrder(properties.getOrder());
73+
74+
DefaultServiceInstance local = properties.getLocal();
75+
DefaultServiceInstance targetLocal = simpleReactiveDiscoveryProperties.getLocal();
76+
setProperties(targetLocal, local);
77+
78+
Map<String, List<DefaultServiceInstance>> instances = getInstancesMap(properties);
79+
simpleReactiveDiscoveryProperties.setInstances(instances);
80+
81+
return simpleReactiveDiscoveryProperties;
82+
}
83+
84+
/**
85+
* Convert {@link SimpleReactiveDiscoveryProperties} to {@link SimpleDiscoveryProperties}
86+
*
87+
* @param properties {@link SimpleReactiveDiscoveryProperties}
88+
* @return {@link SimpleDiscoveryProperties}
89+
*/
90+
@Nonnull
91+
public static SimpleDiscoveryProperties simpleDiscoveryProperties(@Nonnull SimpleReactiveDiscoveryProperties properties) {
92+
SimpleDiscoveryProperties simpleDiscoveryProperties = new SimpleDiscoveryProperties();
93+
simpleDiscoveryProperties.setOrder(properties.getOrder());
94+
95+
DefaultServiceInstance local = properties.getLocal();
96+
simpleDiscoveryProperties.setInstance(local.getServiceId(), local.getHost(), local.getPort());
97+
98+
Map<String, List<DefaultServiceInstance>> instances = invokeMethod(properties, "getInstances");
99+
simpleDiscoveryProperties.setInstances(instances);
100+
101+
return simpleDiscoveryProperties;
102+
}
103+
104+
private DiscoveryUtils() {
105+
}
106+
}

microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/SimpleServiceRegistry.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,27 @@
1919

2020
import org.springframework.cloud.client.DefaultServiceInstance;
2121
import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryProperties;
22+
import org.springframework.cloud.client.discovery.simple.reactive.SimpleReactiveDiscoveryProperties;
2223
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
2324

2425
import java.util.ArrayList;
2526
import java.util.List;
2627
import java.util.Map;
2728

29+
import static io.microsphere.spring.cloud.client.discovery.util.DiscoveryUtils.getInstancesMap;
2830
import static io.microsphere.spring.cloud.client.service.util.ServiceInstanceUtils.getMetadata;
2931
import static io.microsphere.spring.cloud.client.service.util.ServiceInstanceUtils.setMetadata;
3032

3133
/**
32-
* Simple {@link ServiceRegistry} class that is based on {@link SimpleDiscoveryProperties} to register
34+
* Simple {@link ServiceRegistry} class that is based on {@link SimpleDiscoveryProperties}
35+
* or {@link SimpleReactiveDiscoveryProperties} to register
3336
* {@link DefaultRegistration}.
3437
*
3538
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
3639
* @see ServiceRegistry
3740
* @see DefaultRegistration
3841
* @see SimpleDiscoveryProperties#getInstances()
42+
* @see SimpleReactiveDiscoveryProperties#getInstances()
3943
* @since 1.0.0
4044
*/
4145
public class SimpleServiceRegistry implements ServiceRegistry<DefaultRegistration> {
@@ -44,8 +48,16 @@ public class SimpleServiceRegistry implements ServiceRegistry<DefaultRegistratio
4448

4549
private final Map<String, List<DefaultServiceInstance>> instancesMap;
4650

47-
public SimpleServiceRegistry(SimpleDiscoveryProperties simpleDiscoveryProperties) {
48-
this.instancesMap = simpleDiscoveryProperties.getInstances();
51+
public SimpleServiceRegistry(SimpleDiscoveryProperties properties) {
52+
this(getInstancesMap(properties));
53+
}
54+
55+
public SimpleServiceRegistry(SimpleReactiveDiscoveryProperties properties) {
56+
this(getInstancesMap(properties));
57+
}
58+
59+
public SimpleServiceRegistry(Map<String, List<DefaultServiceInstance>> instancesMap) {
60+
this.instancesMap = instancesMap;
4961
}
5062

5163
@Override

microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/util/ServiceInstanceUtils.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import static io.microsphere.util.StringUtils.EMPTY_STRING;
5252
import static io.microsphere.util.StringUtils.EMPTY_STRING_ARRAY;
5353
import static io.microsphere.util.StringUtils.isBlank;
54+
import static java.lang.String.valueOf;
5455
import static java.net.URI.create;
5556
import static java.util.Collections.emptyList;
5657

@@ -102,12 +103,16 @@ public static String getUriString(ServiceInstance instance) {
102103
boolean isSecure = instance.isSecure();
103104
String prefix = isSecure ? "https://" : "http://";
104105
String host = instance.getHost();
105-
String port = String.valueOf(instance.getPort());
106-
StringBuilder urlStringBuilder = new StringBuilder((isSecure ? 9 : 8) + host.length() + port.length());
106+
int port = instance.getPort();
107+
if (port <= 0) {
108+
port = isSecure ? 443 : 80;
109+
}
110+
String portString = valueOf(port);
111+
StringBuilder urlStringBuilder = new StringBuilder((isSecure ? 9 : 8) + host.length() + portString.length());
107112
urlStringBuilder.append(prefix)
108113
.append(host)
109114
.append(COLON_CHAR)
110-
.append(port);
115+
.append(portString);
111116
return urlStringBuilder.toString();
112117
}
113118

@@ -162,6 +167,23 @@ public static String removeMetadata(ServiceInstance serviceInstance, String meta
162167
return metadata.remove(metadataName);
163168
}
164169

170+
/**
171+
* Set properties from source to target
172+
*
173+
* @param source source {@link ServiceInstance}
174+
* @param target target {@link DefaultServiceInstance}
175+
*/
176+
public static void setProperties(ServiceInstance source, DefaultServiceInstance target) {
177+
target.setInstanceId(source.getInstanceId());
178+
target.setServiceId(source.getServiceId());
179+
target.setUri(source.getUri());
180+
target.setHost(source.getHost());
181+
target.setPort(source.getPort());
182+
Map<String, String> metadata = source.getMetadata();
183+
metadata.clear();
184+
metadata.putAll(source.getMetadata());
185+
}
186+
165187
static List<WebEndpointMapping> parseWebEndpointMappings(String encodedJSON) {
166188
if (isBlank(encodedJSON)) {
167189
return emptyList();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
package io.microsphere.spring.cloud.commons.condition;
18+
19+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
20+
import org.springframework.cloud.commons.util.UtilAutoConfiguration;
21+
22+
import java.lang.annotation.Documented;
23+
import java.lang.annotation.Retention;
24+
import java.lang.annotation.Target;
25+
26+
import static io.microsphere.spring.cloud.commons.constants.SpringCloudPropertyConstants.UTIL_ENABLED_PROPERTY_NAME;
27+
import static java.lang.annotation.ElementType.METHOD;
28+
import static java.lang.annotation.ElementType.TYPE;
29+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
30+
31+
/**
32+
* The conditional annotation meta-annotates {@link ConditionalOnProperty @ConditionalOnProperty} for
33+
* {@link UtilAutoConfiguration} enabled.
34+
*
35+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
36+
* @see UtilAutoConfiguration
37+
* @see ConditionalOnProperty
38+
* @since 1.0.0
39+
*/
40+
@Retention(RUNTIME)
41+
@Target({TYPE, METHOD})
42+
@Documented
43+
@ConditionalOnProperty(name = UTIL_ENABLED_PROPERTY_NAME, matchIfMissing = true)
44+
public @interface ConditionalOnUtilEnabled {
45+
}

0 commit comments

Comments
 (0)