use of com.alibaba.csp.sentinel.adapter.okhttp.extractor.OkHttpResourceExtractor in project Sentinel by alibaba.
the class SentinelOkHttpInterceptorTest method testSentinelOkHttpInterceptor1.
@Test
public void testSentinelOkHttpInterceptor1() throws Exception {
String url0 = "http://localhost:" + port + "/okhttp/back/1";
SentinelOkHttpConfig config = new SentinelOkHttpConfig(new OkHttpResourceExtractor() {
@Override
public String extract(Request request, Connection connection) {
String regex = "/okhttp/back/";
String url = request.url().toString();
if (url.contains(regex)) {
url = url.substring(0, url.indexOf(regex) + regex.length()) + "{id}";
}
return request.method() + ":" + url;
}
}, new DefaultOkHttpFallback());
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new SentinelOkHttpInterceptor(config)).build();
Request request = new Request.Builder().url(url0).build();
System.out.println(client.newCall(request).execute().body().string());
String url1 = config.getResourcePrefix() + "GET:http://localhost:" + port + "/okhttp/back/{id}";
ClusterNode cn = ClusterBuilderSlot.getClusterNode(url1);
assertNotNull(cn);
Constants.ROOT.removeChildList();
ClusterBuilderSlot.getClusterNodeMap().clear();
}
Aggregations