zoukankan      html  css  js  c++  java
  • http请求端口占用异常——处理思路

    解决思路:

    1.  ESTABLISHED 过多,使用static解决

           static HttpClientHandler StaticHttpClientHandler = new HttpClientHandler
            {
                AllowAutoRedirect = true,
                Proxy = new System.Net.WebProxy(ConfigHelper.FacebookProxyHostUrl)
            };
    
            static HttpClient StaticHttpClient = new HttpClient(StaticHttpClientHandler, false)
            {
                Timeout = TimeSpan.FromSeconds(10)
            };
    
            [HttpGet]
            [Route(nameof(TestProxy))]
            public async Task<bool> TestProxy()
            {
                string siteUrl = "https://www.google.com";
                var httpClient = StaticHttpClient;
                var requestMessage = new HttpRequestMessage
                {
                    Method = HttpMethod.Get,
                    RequestUri = new Uri(siteUrl, UriKind.Absolute),
                };
                var result =await httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead);
                result.EnsureSuccessStatusCode();
                return true;
            }
    

      

    2.方案1导致ESTABLISHED 控制住,但是CLOSE_WAIT太多,使用IHttpClientFactory解决

                //Startup.cs中的public void ConfigureServices(IServiceCollection services)
                services.AddHttpClient();
                services.AddHttpClient("configured-proxy-handler")
                .ConfigurePrimaryHttpMessageHandler(() =>
                {
                    return new HttpClientHandler()
                    {
                        AllowAutoRedirect = true,
                        Proxy = new System.Net.WebProxy(ConfigHelper.FacebookProxyHostUrl)
                    };
                });    
    
    
             
            //使用
            private readonly IHttpClientFactory _httpClientFactory;
            public OpsController(IHttpClientFactory httpClientFactory)
            {
                _httpClientFactory = httpClientFactory;
            }
    
            [HttpGet]
            [Route(nameof(TestProxy3))]
            public async Task<bool> TestProxy3()
            {
                string siteUrl = "https://www.google.com";
                var httpClient = _httpClientFactory.CreateClient("configured-proxy-handler");
                httpClient.Timeout = TimeSpan.FromSeconds(10);
                try
                {
                    var requestMessage = new HttpRequestMessage
                    {
                        Method = HttpMethod.Get,
                        RequestUri = new Uri(siteUrl, UriKind.Absolute),
                    };
                    var result = await httpClient.GetAsync(siteUrl, HttpCompletionOption.ResponseHeadersRead);
                    result.EnsureSuccessStatusCode();
                    return true;
                }catch(Exception ex)
                {
                    return false;
                }
            }
    

      

  • 相关阅读:
    读《豆瓣的基础架构》有感
    读《【解密】京东B2B业务架构演变》有感
    soa
    读《京东咚咚架构演进》有感
    读《游戏服务器的架构演进(完整版)》有感
    读《京东物流系统架构演进中的最佳实践》有感
    读《微博众筹的架构设计》有感
    读《新浪微博如何应对极端峰值下的弹性扩容挑战》有感
    读《微博推荐架构的演进》有感
    读《新浪微博用户兴趣建模系统架构》有感
  • 原文地址:https://www.cnblogs.com/panpanwelcome/p/12203076.html
Copyright © 2011-2022 走看看