Commit 0d468b39 by Spencer Gibb

attempt ordering of RibbonClients.defaultConfiguration's

ref gh-383
parent 0f2fbf8e
...@@ -39,7 +39,7 @@ public class RibbonClientConfigurationRegistrar implements ImportBeanDefinitionR ...@@ -39,7 +39,7 @@ public class RibbonClientConfigurationRegistrar implements ImportBeanDefinitionR
AnnotationAttributes[] clients = (AnnotationAttributes[]) attrs.get("value"); AnnotationAttributes[] clients = (AnnotationAttributes[]) attrs.get("value");
for (AnnotationAttributes client : clients) { for (AnnotationAttributes client : clients) {
registerClientConfiguration(registry, getClientName(client), registerClientConfiguration(registry, getClientName(client),
client.get("configuration")); client.get("configuration"), 0);
} }
} }
if (attrs != null && attrs.containsKey("defaultConfiguration")) { if (attrs != null && attrs.containsKey("defaultConfiguration")) {
...@@ -50,13 +50,13 @@ public class RibbonClientConfigurationRegistrar implements ImportBeanDefinitionR ...@@ -50,13 +50,13 @@ public class RibbonClientConfigurationRegistrar implements ImportBeanDefinitionR
name = "default." + metadata.getClassName(); name = "default." + metadata.getClassName();
} }
registerClientConfiguration(registry, name, registerClientConfiguration(registry, name,
attrs.get("defaultConfiguration")); attrs.get("defaultConfiguration"), attrs.get("defaultConfigurationOrder"));
} }
Map<String, Object> client = metadata.getAnnotationAttributes( Map<String, Object> client = metadata.getAnnotationAttributes(
RibbonClient.class.getName(), true); RibbonClient.class.getName(), true);
String name = getClientName(client); String name = getClientName(client);
if (name != null) { if (name != null) {
registerClientConfiguration(registry, name, client.get("configuration")); registerClientConfiguration(registry, name, client.get("configuration"), 0);
} }
} }
...@@ -76,11 +76,12 @@ public class RibbonClientConfigurationRegistrar implements ImportBeanDefinitionR ...@@ -76,11 +76,12 @@ public class RibbonClientConfigurationRegistrar implements ImportBeanDefinitionR
} }
private void registerClientConfiguration(BeanDefinitionRegistry registry, private void registerClientConfiguration(BeanDefinitionRegistry registry,
Object name, Object configuration) { Object name, Object configuration, Object order) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder BeanDefinitionBuilder builder = BeanDefinitionBuilder
.genericBeanDefinition(RibbonClientSpecification.class); .genericBeanDefinition(RibbonClientSpecification.class);
builder.addConstructorArgValue(name); builder.addConstructorArgValue(name);
builder.addConstructorArgValue(configuration); builder.addConstructorArgValue(configuration);
builder.addConstructorArgValue(order);
registry.registerBeanDefinition(name + ".RibbonClientSpecification", registry.registerBeanDefinition(name + ".RibbonClientSpecification",
builder.getBeanDefinition()); builder.getBeanDefinition());
} }
......
...@@ -30,4 +30,10 @@ public class RibbonClientSpecification { ...@@ -30,4 +30,10 @@ public class RibbonClientSpecification {
private Class<?>[] configuration; private Class<?>[] configuration;
private int order;
public Integer order() {
return order;
}
} }
...@@ -42,4 +42,5 @@ public @interface RibbonClients { ...@@ -42,4 +42,5 @@ public @interface RibbonClients {
Class<?>[] defaultConfiguration() default {}; Class<?>[] defaultConfiguration() default {};
int defaultConfigurationOrder() default 0;
} }
...@@ -16,11 +16,14 @@ ...@@ -16,11 +16,14 @@
package org.springframework.cloud.netflix.ribbon; package org.springframework.cloud.netflix.ribbon;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
...@@ -125,8 +128,19 @@ public class SpringClientFactory implements DisposableBean, ApplicationContextAw ...@@ -125,8 +128,19 @@ public class SpringClientFactory implements DisposableBean, ApplicationContextAw
context.register(configuration); context.register(configuration);
} }
} }
for (Entry<String, RibbonClientSpecification> entry : this.configurations
.entrySet()) { List<Entry<String, RibbonClientSpecification>> entries = new ArrayList<>(this.configurations
.entrySet());
Collections.sort(entries, new Comparator<Entry<String, RibbonClientSpecification>>() {
@Override
public int compare(Entry<String, RibbonClientSpecification> o1, Entry<String, RibbonClientSpecification> o2) {
return o1.getValue().order().compareTo(o2.getValue().order());
}
});
for (Entry<String, RibbonClientSpecification> entry : entries) {
if (entry.getKey().startsWith("default.")) { if (entry.getKey().startsWith("default.")) {
for (Class<?> configuration : entry.getValue().getConfiguration()) { for (Class<?> configuration : entry.getValue().getConfiguration()) {
context.register(configuration); context.register(configuration);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment