遇到一个问题,需要根据请求参数进行自定义路由。
看了ribbon的实现源码,发现ribbon 在路由时,根本没有地方可以传参,以实现根据参数路由的功能。唯一的参数loadBalancerKey,是永远为null的坑爹设计。所以只好自己实现。
我的实现方式如下:
一,首先定义自定义RequestInterceptor,将参数传入到ThreadLocal里
@Component
public class MyRequestInterceptor implements RequestInterceptor {
String VERSION="version";
@Override
public void apply(RequestTemplate requestTemplate) {
Map<String,String> headers = getHeaders(getHttpServletRequest());
String version=headers.getOrDefault(VERSION,"");
if (!version.isEmpty()){
VersionContext.set(version);
}else {
VersionContext.set("");
}
}
private HttpServletRequest getHttpServletRequest() {
try {
return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private Map<String, String> getHeaders(HttpServletRequest request) {
Map<String, String> map = new LinkedHashMap<>();
Enumeration<String> enumeration = request.getHeaderNames();
while (enumeration.hasMoreElements()) {
String key = enumeration.nextElement();
String value = request.getHeader(key);
map.put(key, value);
}
return map;
}
}
用到的VersionContext.java
public class VersionContext {
private static ThreadLocal<String> version=new ThreadLocal<>();
public static String get(){
return version.get();
}
public static void set(String s){
version.set(s);
}
}
二,定义自定义路由MyRule
@Component
public class MyRule extends AbstractLoadBalancerRule {
@Override
public void initWithNiwsConfig(IClientConfig iClientConfig) {
}
@Override
public Server choose(Object o) {
String version=VersionContext.get();
List<Server> allServers = getLoadBalancer().getAllServers();
if (version.equals("v1")){
return allServers.get(0);
}
return allServers.get(1);
}
}
这样就实现了根据参数路由了。