{"id":18559133,"url":"https://github.com/linux-china/spring-boot-starter-httpclient","last_synced_at":"2025-04-10T02:30:40.112Z","repository":{"id":136596239,"uuid":"47729988","full_name":"linux-china/spring-boot-starter-httpclient","owner":"linux-china","description":"Spring Boot Starter for HttpClient","archived":false,"fork":false,"pushed_at":"2018-03-02T04:35:38.000Z","size":16,"stargazers_count":54,"open_issues_count":0,"forks_count":33,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-04-02T02:51:14.412Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/linux-china.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-12-10T01:31:02.000Z","updated_at":"2023-04-24T16:28:12.000Z","dependencies_parsed_at":null,"dependency_job_id":"9ad05901-a1f5-46ca-b64b-d5e3eb40b331","html_url":"https://github.com/linux-china/spring-boot-starter-httpclient","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linux-china%2Fspring-boot-starter-httpclient","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linux-china%2Fspring-boot-starter-httpclient/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linux-china%2Fspring-boot-starter-httpclient/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linux-china%2Fspring-boot-starter-httpclient/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/linux-china","download_url":"https://codeload.github.com/linux-china/spring-boot-starter-httpclient/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248144158,"owners_count":21054876,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T21:42:08.996Z","updated_at":"2025-04-10T02:30:40.105Z","avatar_url":"https://github.com/linux-china.png","language":"Java","funding_links":[],"categories":["HTTP客户端"],"sub_categories":["微服务框架"],"readme":"Spring Boot Starter HttpClient\n=============================================\n在Spring Boot下整合http client,方便客户端使用.\n\n### 为何要使用spring-boot-starter-httpclient\nhttp client已经非常简单啦,为何还需要创建一个starter httpclient,其主要的目的如下:\n\n* 让使用更简单,如全局proxy,timeout等\n* 结合dropwizard metrics,添加对metrics的管理\n\n### 如何使用\n\n* 在Spring Boot项目的pom.xml中添加以下依赖:\n\n          \u003cdependency\u003e\n                 \u003cgroupId\u003eorg.mvnsearch.spring.boot\u003c/groupId\u003e\n                 \u003cartifactId\u003espring-boot-starter-httpclient\u003c/artifactId\u003e\n                 \u003cversion\u003e1.0.0-SNAPSHOT\u003c/version\u003e\n          \u003c/dependency\u003e\n\n* 在Spring Boot的application.properties文件中添加http client的一些相关配置,当然也可以不需要配置如下:\n                    \n          spring.httpclient.connectTimeout=2000\n\n* 接下来在你的代码中直接应用cacheManager，然后就可以啦。\n        \n            @Autowired\n            private HttpClient httpClient;\n            @Autowired\n            private Executor executor;\n\n### spring-boot-start-httpclient提供的服务\n\n* org.apache.http.client.HttpClient: Http Client\n* org.apache.http.client.fluent.Executor: Executor\n\n### 典型的例子:\n\n* http client: \n\n            reporter.start(1, TimeUnit.SECONDS);\n            HttpGet httpGet = new HttpGet(\"https://www.yahoo.com/tech/s/favorite-iphone-feature-apple-added-ios-9-2-183202009.html\");\n            HttpResponse response = httpClient.execute(httpGet);\n            EntityUtils.toString(response.getEntity());\n* executor: \n         \n           String content = executor.execute(Request.Get(\"https://www.yahoo.com/\")).returnContent().asString();\n\n### httpclient metrics\nHttpClient结合了DropWizard的metrics,这样很方便获取http client的metrics信息, \n目前可以记录的级别主要是: METHOD_ONLY, HOST_AND_METHOD(default), QUERYLESS_URL_AND_METHOD, 以下是metrics的输出:\n```\n\n      -- Gauges ----------------------------------------------------------------------\n      org.apache.http.conn.HttpClientConnectionManager.available-connections\n                   value = 1\n      org.apache.http.conn.HttpClientConnectionManager.leased-connections\n                   value = 0\n      org.apache.http.conn.HttpClientConnectionManager.max-connections\n                   value = 20\n      org.apache.http.conn.HttpClientConnectionManager.pending-connections\n                   value = 0\n      \n      -- Timers ----------------------------------------------------------------------\n      org.apache.http.client.HttpClient.www.yahoo.com.get-requests\n                   count = 1\n               mean rate = 1.74 calls/second\n           1-minute rate = 0.00 calls/second\n           5-minute rate = 0.00 calls/second\n          15-minute rate = 0.00 calls/second\n                     min = 117.63 milliseconds\n                     max = 117.63 milliseconds\n                    mean = 117.63 milliseconds\n                  stddev = 0.00 milliseconds\n                  median = 117.63 milliseconds\n                    75% \u003c= 117.63 milliseconds\n                    95% \u003c= 117.63 milliseconds\n                    98% \u003c= 117.63 milliseconds\n                    99% \u003c= 117.63 milliseconds\n                  99.9% \u003c= 117.63 milliseconds\n```\n### FAQ\n\n* HttpClient线程安全吗? HttpClient implementations are expected to be thread safe.\nIt is recommended that the same instance of this class is reused for multiple request executions.\n\n* 我用Commons HttpClient 3.x挺好的,为何换到http components上? Http Components是 commons-httpclient的替代者,建议使用http components\n\n\n### 参考文档\n\n* Http Components: http://hc.apache.org/\n* HttpClient 4 Tutorial: http://www.baeldung.com/httpclient-guide","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flinux-china%2Fspring-boot-starter-httpclient","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flinux-china%2Fspring-boot-starter-httpclient","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flinux-china%2Fspring-boot-starter-httpclient/lists"}