- How to do online debugging.
- How to customize the routing rules in gateway to go for request distribution so that the request hits the node we want in cluster mode.
1. Configure remote debug
1. Configure the parameters in the startup parameters:
-Xdebug
-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=6364
2. New remote
3. Start remote
remodel
My personal need is to take a user's request and hit the node I want (the node with debug mode turned on), to avoid the load on the cluster nodes causing the request to hit other nodes, and to avoid affecting the normal request flow of other users.
My approach is to intercept the Authorization=xxx in the request cast to determine this, hence the reference to gateway's routing strategy:
gateway Route Matching Policy
And the routing policy that comes with gateway is implemented by regix (regular matching), so I made the following modification:
1. Add HeaderValueRoutePredicateFactory
public class HeaderValueRoutePredicateFactory extends AbstractRoutePredicateFactory<> {
private static final String KEY_1 = "headerName";
private static final String KEY_2 = "headerValue";
public HeaderValueRoutePredicateFactory() {
super();
}
@Override
public List<String> shortcutFieldOrder() {
return (KEY_1, KEY_2);
}
@Override
public Predicate<ServerWebExchange> apply(Config config) {
return new GatewayPredicate() {
@Override
public boolean test(ServerWebExchange exchange) {
List<String> values = ().getHeaders().getOrDefault((),
());
if (()) {
return false;
}
boolean match = ().allMatch(item -> (()));
if (match) {
("debugparadigmrouteStrategy triggered");
}
return match;
}
@Override
public Object getConfig() {
return config;
}
@Override
public String toString() {
return ("HeaderName: %s headerValue=%s", (), ());
}
};
}
public static class Config {
private String headerName;
private String headerValue;
public String getHeaderName() {
return headerName;
}
public Config setHeaderName(String headerName) {
= headerName;
return this;
}
public String getHeaderValue() {
return headerValue;
}
public Config setHeaderValue(String headerValue) {
= headerValue;
return this;
}
}
}
Code Notes:
- Get the system cache routing policy (you can see the full policy configured in the gateway)
#getRoutes - Get routing policy (matching based on request conditions)
#getHandlerInternal - Get routing policy (matching based on request conditions)
#lookupRoute
2. Configuration class
@Configuration
public class Config {
@Bean
public HeaderValueRoutePredicateFactory headerValueRoutePredicateFactory() {
return new HeaderValueRoutePredicateFactory();
}
}
3. Modify the gateway configuration
spring.
cloud.
spring: cloud.
routes.
# When a request with Authorization=ff4a4ce5-5276-4263-b817-34d1ce553421 in the request header cuts the path to /ims/** it will be routed by this rule
- id: ims
uri: lb://ims
uri: lb://ims
- Path=/ims/**
filters.
- StripPrefix=1
# When the request header with Authorization=ff4a4ce5-5276-4263-b817-34d1ce553421 cuts the path to /ims/** it will be routed by this rule
- id: ims-debug
uri: lb://ims-debug
# Configure -1 so that this routing policy is judged before id: ims, otherwise it would trigger the id=ims routing policy, not the id=ims-debug routing policy.
order: -1
predicates.
- Path=/ims/**
- HeaderValue=Authorization,ff4a4ce5-5276-4263-b817-34d1ce553421
filters.
- StripPrefix=1
configure
1. Normal node configuration
-=ims
The node configuration of the
-=ims-debug
Essentially ims and ims-debug are the same service, just with different service names, and the distinction is made for routing convenience
4. Other issues
- Under normal circumstances, if it is a production environment, the ims-debug service, even though it is in debug mode, will not be able to connect locally on the office network due to network problems. In this case, we can ask the operation and maintenance to apply for a springboard or VPN to connect to the private network, in addition to the external port of the debug to apply for a good.
- I've seen other online debugging methods on the webClick to jump to someone else's linkSo my approach may not be suitable for everyone (but neither considers the issue of requests under clustering if they don't go to the current node), I'm just compromising.
- Even if you learn it, it won't be of much use, and normally it won't allow developers to debug online. the author himself is just on a whim.