Search in sources :

Example 1 with SentinelApacheHttpClientConfig

use of com.alibaba.csp.sentinel.adapter.apache.httpclient.config.SentinelApacheHttpClientConfig in project Sentinel by alibaba.

the class ApacheHttpClientTestController method sync.

@RequestMapping("/httpclient/sync")
public String sync() throws Exception {
    SentinelApacheHttpClientConfig config = new SentinelApacheHttpClientConfig();
    config.setExtractor(new ApacheHttpClientResourceExtractor() {

        @Override
        public String extractor(HttpRequestWrapper request) {
            String contains = "/httpclient/back/";
            String uri = request.getRequestLine().getUri();
            if (uri.startsWith(contains)) {
                uri = uri.substring(0, uri.indexOf(contains) + contains.length()) + "{id}";
            }
            return request.getMethod() + ":" + uri;
        }
    });
    CloseableHttpClient httpclient = new SentinelApacheHttpClientBuilder(config).build();
    HttpGet httpGet = new HttpGet("http://localhost:" + port + "/httpclient/back");
    return getRemoteString(httpclient, httpGet);
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) ApacheHttpClientResourceExtractor(com.alibaba.csp.sentinel.adapter.apache.httpclient.extractor.ApacheHttpClientResourceExtractor) SentinelApacheHttpClientConfig(com.alibaba.csp.sentinel.adapter.apache.httpclient.config.SentinelApacheHttpClientConfig) HttpGet(org.apache.http.client.methods.HttpGet) HttpRequestWrapper(org.apache.http.client.methods.HttpRequestWrapper) SentinelApacheHttpClientBuilder(com.alibaba.csp.sentinel.adapter.apache.httpclient.SentinelApacheHttpClientBuilder) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with SentinelApacheHttpClientConfig

use of com.alibaba.csp.sentinel.adapter.apache.httpclient.config.SentinelApacheHttpClientConfig in project Sentinel by alibaba.

the class ApacheHttpClientTestController method sync.

@RequestMapping("/httpclient/sync/{id}")
public String sync(@PathVariable String id) throws Exception {
    SentinelApacheHttpClientConfig config = new SentinelApacheHttpClientConfig();
    config.setExtractor(new ApacheHttpClientResourceExtractor() {

        @Override
        public String extractor(HttpRequestWrapper request) {
            String contains = "/httpclient/back/";
            String uri = request.getRequestLine().getUri();
            if (uri.startsWith(contains)) {
                uri = uri.substring(0, uri.indexOf(contains) + contains.length()) + "{id}";
            }
            return request.getMethod() + ":" + uri;
        }
    });
    CloseableHttpClient httpclient = new SentinelApacheHttpClientBuilder(config).build();
    HttpGet httpGet = new HttpGet("http://localhost:" + port + "/httpclient/back/" + id);
    return getRemoteString(httpclient, httpGet);
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) ApacheHttpClientResourceExtractor(com.alibaba.csp.sentinel.adapter.apache.httpclient.extractor.ApacheHttpClientResourceExtractor) SentinelApacheHttpClientConfig(com.alibaba.csp.sentinel.adapter.apache.httpclient.config.SentinelApacheHttpClientConfig) HttpGet(org.apache.http.client.methods.HttpGet) HttpRequestWrapper(org.apache.http.client.methods.HttpRequestWrapper) SentinelApacheHttpClientBuilder(com.alibaba.csp.sentinel.adapter.apache.httpclient.SentinelApacheHttpClientBuilder) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with SentinelApacheHttpClientConfig

use of com.alibaba.csp.sentinel.adapter.apache.httpclient.config.SentinelApacheHttpClientConfig in project Sentinel by alibaba.

the class SentinelApacheHttpClientTest method testSentinelOkHttpInterceptor1.

@Test
public void testSentinelOkHttpInterceptor1() throws Exception {
    SentinelApacheHttpClientConfig config = new SentinelApacheHttpClientConfig();
    config.setExtractor(new ApacheHttpClientResourceExtractor() {

        @Override
        public String extractor(HttpRequestWrapper request) {
            String contains = "/httpclient/back/";
            String uri = request.getRequestLine().getUri();
            if (uri.startsWith(contains)) {
                uri = uri.substring(0, uri.indexOf(contains) + contains.length()) + "{id}";
            }
            return request.getMethod() + ":" + uri;
        }
    });
    CloseableHttpClient httpclient = new SentinelApacheHttpClientBuilder(config).build();
    HttpGet httpGet = new HttpGet("http://localhost:" + port + "/httpclient/back/1");
    System.out.println(getRemoteString(httpclient, httpGet));
    ClusterNode cn = ClusterBuilderSlot.getClusterNode("httpclient:GET:/httpclient/back/{id}");
    assertNotNull(cn);
    Constants.ROOT.removeChildList();
    ClusterBuilderSlot.getClusterNodeMap().clear();
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) ApacheHttpClientResourceExtractor(com.alibaba.csp.sentinel.adapter.apache.httpclient.extractor.ApacheHttpClientResourceExtractor) SentinelApacheHttpClientConfig(com.alibaba.csp.sentinel.adapter.apache.httpclient.config.SentinelApacheHttpClientConfig) HttpGet(org.apache.http.client.methods.HttpGet) HttpRequestWrapper(org.apache.http.client.methods.HttpRequestWrapper) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

SentinelApacheHttpClientConfig (com.alibaba.csp.sentinel.adapter.apache.httpclient.config.SentinelApacheHttpClientConfig)3 ApacheHttpClientResourceExtractor (com.alibaba.csp.sentinel.adapter.apache.httpclient.extractor.ApacheHttpClientResourceExtractor)3 HttpGet (org.apache.http.client.methods.HttpGet)3 HttpRequestWrapper (org.apache.http.client.methods.HttpRequestWrapper)3 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)3 SentinelApacheHttpClientBuilder (com.alibaba.csp.sentinel.adapter.apache.httpclient.SentinelApacheHttpClientBuilder)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ClusterNode (com.alibaba.csp.sentinel.node.ClusterNode)1 Test (org.junit.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1