Search in sources :

Example 1 with OkHttpResourceExtractor

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();
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) Connection(okhttp3.Connection) DefaultOkHttpFallback(com.alibaba.csp.sentinel.adapter.okhttp.fallback.DefaultOkHttpFallback) OkHttpResourceExtractor(com.alibaba.csp.sentinel.adapter.okhttp.extractor.OkHttpResourceExtractor) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

OkHttpResourceExtractor (com.alibaba.csp.sentinel.adapter.okhttp.extractor.OkHttpResourceExtractor)1 DefaultOkHttpFallback (com.alibaba.csp.sentinel.adapter.okhttp.fallback.DefaultOkHttpFallback)1 ClusterNode (com.alibaba.csp.sentinel.node.ClusterNode)1 Connection (okhttp3.Connection)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 Test (org.junit.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1