spring cloud K8s源码阅读:
package org.springframework.cloud.kubernetes.ribbon;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashSet;
import java.util.Set;
import com.netflix.client.config.IClientConfigKey;
import org.springframework.util.Assert;
/**
* Kubernetes implementation of a Ribbon {@link IClientConfigKey}.
*
* @param <T> type of key
* @author Ioannis Canellos
*/
public abstract class KubernetesConfigKey<T> implements IClientConfigKey<T> {
/**
* Namespace configuration key.
*/
public static final IClientConfigKey<String> Namespace = new KubernetesConfigKey<String>(
"KubernetesNamespace") {
};
/**
* Port name configuration key.
*/
public static final IClientConfigKey<String> PortName = new KubernetesConfigKey<String>(
"PortName") {
};
private static final Set<IClientConfigKey> keys = new HashSet<IClientConfigKey>();
/*请问这段是什么意思? 该方法没有方法名,没有参数?*/
static {
for (Field f : KubernetesConfigKey.class.getDeclaredFields()) {
if (Modifier.isStatic(f.getModifiers()) // &&
// Modifier.isPublic(f.getModifiers())
&& IClientConfigKey.class.isAssignableFrom(f.getType())) {
try {
keys.add((IClientConfigKey) f.get(null));
}
catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
}
private final String configKey;
private final Class<T> type;
@SuppressWarnings("unchecked")
protected KubernetesConfigKey(String configKey) {
this.configKey = configKey;
Type superclass = getClass().getGenericSuperclass();
Assert.isTrue(superclass instanceof ParameterizedType,
superclass + " isn't parameterized");
Type runtimeType = ((ParameterizedType) superclass).getActualTypeArguments()[0];
this.type = (Class<T>) Types.rawType(runtimeType);
}
/**
* @deprecated see {@link #keys()}
* @return array of {@link IClientConfigKey}
*/
@Deprecated
public static IClientConfigKey[] values() {
return keys().toArray(new IClientConfigKey[0]);
}
/**
* @return all the public static keys defined in this class
*/
public static Set<IClientConfigKey> keys() {
return keys;
}
public static IClientConfigKey valueOf(final String name) {
for (IClientConfigKey key : keys()) {
if (key.key().equals(name)) {
return key;
}
}
return new IClientConfigKey() {
@Override
public String key() {
return name;
}
@Override
public Class type() {
return String.class;
}
};
}
@Override
public Class<T> type() {
return this.type;
}
/*
* (non-Javadoc)
*
* @see com.netflix.niws.client.ClientConfig#key()
*/
@Override
public String key() {
return this.configKey;
}
@Override
public String toString() {
return this.configKey;
}
来吧,给大家讲讲这段代码!