feat:支持规则路由功能

pull/198/head
andrewshan 3 years ago
parent c87eea20a6
commit 2df485b621

@ -116,6 +116,10 @@ public class RouterLabelFeignInterceptor implements RequestInterceptor, Ordered
} }
// pass label by header // pass label by header
if (escapeLabels.size() == 0) {
requestTemplate.header(RouterConstants.ROUTER_LABEL_HEADER);
return;
}
requestTemplate.header(RouterConstants.ROUTER_LABEL_HEADER, JacksonUtils.serialize2Json(escapeLabels)); requestTemplate.header(RouterConstants.ROUTER_LABEL_HEADER, JacksonUtils.serialize2Json(escapeLabels));
} }

@ -18,9 +18,14 @@
package com.tencent.cloud.polaris.router.grayrelease.gateway; package com.tencent.cloud.polaris.router.grayrelease.gateway;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -34,14 +39,36 @@ public class GatewayController {
@Autowired @Autowired
private RouterService routerService; private RouterService routerService;
private final Map<Integer, String> uidMapping;
public GatewayController() {
String mapping = System.getenv("uid_env_mapping");
uidMapping = parseUidMapping(mapping);
System.out.println("parsed mapping is " + uidMapping);
}
/** /**
* Get information of callee. * Get information of callee.
* @return information of callee * @return information of callee
*/ */
@GetMapping("/entry") @GetMapping("/route_rule")
public String rest() { public String routeRule(@RequestHeader("uid") int userId) {
String appName = environment.getProperty("spring.application.name"); String appName = environment.getProperty("spring.application.name");
String resp = routerService.rest(); String resp = routerService.restByUser(userId);
return appName + " -> " + resp; return appName + " -> " + resp;
} }
private Map<Integer, String> parseUidMapping(String mapText) {
Map<Integer, String> result = new HashMap<>();
if (!StringUtils.hasText(mapText)) {
return result;
}
String[] tokens = mapText.split("\\|");
for (String token : tokens) {
String[] pairs = token.split(":");
result.put(Integer.parseInt(pairs[0]), pairs[1]);
}
return result;
}
} }

@ -20,6 +20,7 @@ package com.tencent.cloud.polaris.router.grayrelease.gateway;
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
/** /**
* Router callee feign client. * Router callee feign client.
@ -30,6 +31,5 @@ import org.springframework.web.bind.annotation.GetMapping;
public interface RouterService { public interface RouterService {
@GetMapping("/router/gray/rest") @GetMapping("/router/gray/rest")
String rest(); String restByUser(@RequestHeader("uid") int user);
} }

@ -84,6 +84,7 @@ public class ServiceRuleManager {
ServiceEventKey srcSvcEventKey = new ServiceEventKey(new ServiceKey(namespace, sourceService), ServiceEventKey srcSvcEventKey = new ServiceEventKey(new ServiceKey(namespace, sourceService),
ServiceEventKey.EventType.ROUTING); ServiceEventKey.EventType.ROUTING);
routerKeys.add(srcSvcEventKey);
DefaultServiceEventKeysProvider svcKeysProvider = new DefaultServiceEventKeysProvider(); DefaultServiceEventKeysProvider svcKeysProvider = new DefaultServiceEventKeysProvider();
svcKeysProvider.setSvcEventKeys(routerKeys); svcKeysProvider.setSvcEventKeys(routerKeys);

Loading…
Cancel
Save