{"id":549,"date":"2023-03-12T16:30:35","date_gmt":"2023-03-12T07:30:35","guid":{"rendered":"https:\/\/hirabay.net\/?p=549"},"modified":"2023-03-24T22:20:08","modified_gmt":"2023-03-24T13:20:08","slug":"%e3%80%90resttemplate%e3%80%91%e9%81%8b%e7%94%a8%e3%81%ab%e4%bd%bf%e3%81%88%e3%82%8b%e8%a8%ad%e5%ae%9a%e9%9b%86","status":"publish","type":"post","link":"https:\/\/hirabay.net\/?p=549","title":{"rendered":"\u3010RestTemplate\u3011\u904b\u7528\u306b\u4f7f\u3048\u308b\u8a2d\u5b9a\u96c6"},"content":{"rendered":"\n<p>RestTemplate\u3092\u4f7f\u3063\u305f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u672c\u756a\u904b\u7528\u3059\u308b\u305f\u3081\u306b\u610f\u8b58\u3057\u3066\u304a\u304d\u305f\u3044\u8a2d\u5b9a\u3068\u305d\u306e\u8a2d\u5b9a\u65b9\u6cd5\u3092\u307e\u3068\u3081\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<p>\u81ea\u5206\u306e\u73fe\u5834\u3067\u610f\u8b58\u3057\u3066\u3044\u308b\u3053\u3068\u304c\u4e2d\u5fc3\u306b\u306a\u308a\u307e\u3059\u304c\u3001\u30b3\u30d4\u30da\u3067\u305d\u306e\u307e\u307e\u672c\u756a\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306b\u4f7f\u3046\u3088\u3046\u306a\u30b3\u30fc\u30c9\u3092\u6b8b\u3057\u3066\u304a\u304d\u307e\u3059\uff01<\/p>\n\n\n\n<div class=\"toc\"><br \/>\n<b>Warning<\/b>:  Undefined array key \"is_admin\" in <b>\/home\/c4179872\/public_html\/hirabay.net\/wp-content\/themes\/sango-theme\/library\/gutenberg\/dist\/classes\/Toc.php<\/b> on line <b>110<\/b><br \/>\n<br \/>\n<b>Warning<\/b>:  Undefined array key \"is_category_top\" in <b>\/home\/c4179872\/public_html\/hirabay.net\/wp-content\/themes\/sango-theme\/library\/gutenberg\/dist\/classes\/Toc.php<\/b> on line <b>115<\/b><br \/>\n<br \/>\n<b>Warning<\/b>:  Undefined array key \"is_top\" in <b>\/home\/c4179872\/public_html\/hirabay.net\/wp-content\/themes\/sango-theme\/library\/gutenberg\/dist\/classes\/Toc.php<\/b> on line <b>122<\/b><br \/>\n    <div id=\"toc_container\" class=\"sgb-toc--bullets js-smooth-scroll\">\n      <p class=\"toc_title\">\u76ee\u6b21 <\/p>\n      <ul class=\"toc_list\">\n  <li class=\"first\">\n    <a href=\"#i-0\">\u4f9d\u5b58\u30e9\u30a4\u30d6\u30e9\u30ea<\/a>\n  <\/li>\n  <li>\n    <a href=\"#i-1\">\u5171\u901a\u306e\u8a2d\u5b9a<\/a>\n  <\/li>\n  <li class=\"last\">\n    <a href=\"#i-2\">\u30e1\u30c8\u30ea\u30af\u30b9\u53ce\u96c6<\/a>\n    <ul class=\"menu_level_1\">\n      <li class=\"first last\">\n        <a href=\"#i-3\">URL\u306e\u6307\u5b9a\u65b9\u6cd5\u306b\u6ce8\u610f\uff01\uff01\uff01<\/a>\n      <\/li>\n    <\/ul>\n  <\/li>\n<\/ul>\n\n      \n    <\/div><\/div><h2 class=\"wp-block-heading\" id=\"i-0\">\u4f9d\u5b58\u30e9\u30a4\u30d6\u30e9\u30ea<\/h2>\n\n\n\n<p>\u52d5\u4f5c\u78ba\u8a8d\u3092\u3057\u305f<code>build.gradle<\/code>\u306e\u629c\u7c8b\u3067\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-groovy\" data-file=\"build.gradle\" data-lang=\"Groovy\"><code>plugins {\n\tid 'java'\n\tid 'org.springframework.boot' version '3.0.2'\n\tid 'io.spring.dependency-management' version '1.1.0'\n}\n\ndependencies {\n\timplementation 'org.springframework.boot:spring-boot-starter-web'\n\timplementation 'org.apache.httpcomponents.client5:httpclient5'\n}<\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-1\">\u5171\u901a\u306e\u8a2d\u5b9a<\/h2>\n\n\n\n<p>\u3069\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u3082\u5171\u901a\u3067\u610f\u8b58\u3059\u308b\u3067\u3042\u308d\u3046\u8a2d\u5b9a\u306e\u5b9f\u88c5\u65b9\u6cd5\u3092\u307e\u3068\u3081\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-java\" data-lang=\"Java\"><code>@Configuration\npublic class RestTemplateAutoConfiguration {\n    @Bean\n    RestTemplate restTemplate() {\n        var socketConfig = SocketConfig.custom()\n                \/\/ read timeout\uff08\u4f8b\uff1a1000\u30df\u30ea\u79d2\uff09\n                .setSoTimeout(1_000, TimeUnit.MILLISECONDS)\n                .build();\n        var connectionManager = PoolingHttpClientConnectionManagerBuilder.create()\n                \/\/ keep alive timeout\uff08\u4f8b: 59\u79d2\uff09\n                .setConnectionTimeToLive(TimeValue.ofSeconds(59))\n                \/\/ \u5168\u30eb\u30fc\u30c8\u5408\u7b97\u306e\u6700\u5927\u63a5\u7d9a\u6570\n                .setMaxConnTotal(100)\n                \/\/ \u30eb\u30fc\u30c8\uff08\u57fa\u672c\u7684\u306b\u306f\u30c9\u30e1\u30a4\u30f3\uff09\u3054\u3068\u306e\u6700\u5927\u63a5\u7d9a\u6570\n                \/\/ !!! \u30c7\u30d5\u30a9\u30eb\u30c8\u304c\u300c5\u300d\u3067\u9ad8\u8ca0\u8377\u306b\u306f\u8010\u3048\u3089\u308c\u306a\u3044\u8a2d\u5b9a\u5024\u306a\u306e\u3067\u6ce8\u610f !!!\n                .setMaxConnPerRoute(100)\n                .setDefaultSocketConfig(socketConfig)\n                .build();\n\n        var httpClient = HttpClientBuilder.create()\n                .setConnectionManager(connectionManager)\n                .build();\n\n        var requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);\n\n        return new RestTemplateBuilder()\n                .requestFactory(() -&gt; requestFactory)\n                .rootUri(\"http:\/\/localhost:8080\")\n                .setConnectTimeout(Duration.ofMillis(500))\n                .build();\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-2\">\u30e1\u30c8\u30ea\u30af\u30b9\u53ce\u96c6<\/h2>\n\n\n\n<p>\u30e1\u30c8\u30ea\u30af\u30b9\u53ce\u96c6\u3092\u3059\u308b\u306b\u306f <code>actuator<\/code>\u3092\u4f9d\u5b58\u306b\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-groovy\" data-file=\"build.gradle\" data-lang=\"Groovy\"><code>dependencies {\n  ...\n\timplementation 'org.springframework.boot:spring-boot-starter-actuator'\n\n  \/\/ \uff08\u4efb\u610f\uff09\u30e1\u30c8\u30ea\u30af\u30b9\u3092prometheus\u3068\u9023\u643a\u3059\u308b\u5834\u5408\u306b\u5fc5\u8981\n\timplementation 'io.micrometer:micrometer-registry-prometheus'\n  ...\n}<\/code><\/pre><\/div>\n\n\n\n<p>\u8ffd\u52a0\u3057\u305f\u4f9d\u5b58\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u4e2d\u306b\u30e1\u30c8\u30ea\u30af\u30b9\u53ce\u96c6\u3092\u3057\u3066\u304f\u308c\u308b\u5b9f\u88c5\u304c\u3059\u3067\u306b\u7528\u610f\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u7279\u5225\u306a\u8981\u4ef6\u304c\u306a\u3044\u9650\u308a\u306f\u305d\u308c\u3092\u5229\u7528\u3059\u308c\u3070OK\uff01<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-java\" data-lang=\"Java\"><code>    @Bean\n    RestTemplate restTemplateWithMetrics(ObservationRestTemplateCustomizer observationCustomizer) {\n        return new RestTemplateBuilder()\n                .rootUri(\"http:\/\/localhost:8080\")\n                .additionalCustomizers(observationCustomizer)\n                .build();\n    }<\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-3\">URL\u306e\u6307\u5b9a\u65b9\u6cd5\u306b\u6ce8\u610f\uff01\uff01\uff01<\/h3>\n\n\n\n<p>\u30e1\u30c8\u30ea\u30af\u30b9\u3092\u6709\u52b9\u306b\u3059\u308b\u5834\u5408\u3001RestTemplate\u5b9f\u884c\u6642\u306eURL\u306e\u6307\u5b9a\u65b9\u6cd5\u3092\u6c17\u3092\u4ed8\u3051\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u307e\u305a\u306f\u30c0\u30e1\u306a\u4f8b\u3067\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-java\" data-lang=\"Java\"><code>        var uuid = UUID.randomUUID().toString();\n        restTemplate.getForObject(\"\/server?uuid=\" + uuid, String.class);<\/code><\/pre><\/div>\n\n\n\n<p>\u30e1\u30c8\u30ea\u30af\u30b9\u306f\u3053\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-plain\" data-file=\"\u30e1\u30c8\u30ea\u30af\u30b9\u4f8b\"><code>http_client_requests_seconds_count{error=\"none\",exception=\"none\",method=\"GET\",outcome=\"SUCCESS\",status=\"200\",uri=\"\/server?uuid=2ca3e70f-8d3a-4bcb-b7af-1fed52ed2b74\",} 1.0<\/code><\/pre><\/div>\n\n\n\n<p>uri\u306bUUID\u306e\u5024\u304c\u5165\u308a\u8fbc\u3093\u3067\u304a\u308a\u3001uuid\u5206\u30e1\u30c8\u30ea\u30af\u30b9\u304c\u80a5\u5927\u5316\u3057\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u305d\u3057\u3066\u80a5\u5927\u5316\u3057\u305f\u30e1\u30c8\u30ea\u30af\u30b9\u306b\u3088\u3063\u3066OOM\u304c\u5f15\u304d\u8d77\u3053\u3055\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002<br>\uff08\u5b9f\u4f53\u9a13\u3068\u3057\u3066\u3053\u306e\u7406\u7531\u306eOOM\u3092\u7d4c\u9a13\u3057\u3066\u3044\u307e\u3059\uff09<\/p>\n\n\n\n<p>\u3067\u306f\u3001\u3069\u3046\u5b9f\u88c5\u3059\u308c\u3070\u826f\u3044\u306e\u304b\u3068\u3044\u3046\u3068\u5909\u6570\u90e8\u5206\u306f\u5225\u9014\u5f15\u6570\u3067\u8a2d\u5b9a\u3059\u308b\u3088\u3046\u306a\u3053\u3068\u3092\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-java\" data-lang=\"Java\"><code>        var uuid = UUID.randomUUID().toString();\n        restTemplateWithMetrics.getForObject(\"\/server?uuid={uuid}\", String.class, uuid);<\/code><\/pre><\/div>\n\n\n\n<p>\u3053\u306e\u5b9f\u88c5\u3060\u3068\u30e1\u30c8\u30ea\u30af\u30b9\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-java\" data-lang=\"Java\"><code>http_client_requests_seconds_count{error=\"none\",exception=\"none\",method=\"GET\",outcome=\"SUCCESS\",status=\"200\",uri=\"\/server?uuid={uuid}\",} 1.0<\/code><\/pre><\/div>\n\n\n\n<p>uri\u304c\u4e38\u3081\u8fbc\u307e\u308c\u3066\u3044\u308b\u306e\u3067\u30e1\u30c8\u30ea\u30af\u30b9\u306e\u80a5\u5927\u5316\u306e\u5fc3\u914d\u304c\u3042\u308a\u307e\u305b\u3093\uff01<br>\uff08\u305d\u3057\u3066\u3053\u3061\u3089\u306e\u5f62\u5f0f\u306e\u65b9\u304c\u53d6\u5f97\u3057\u305f\u304b\u3063\u305f\u5f62\u5f0f\u306b\u8fd1\u3044\u306f\u305a\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>RestTemplate\u3092\u4f7f\u3063\u305f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u672c\u756a\u904b\u7528\u3059\u308b\u305f\u3081\u306b\u610f\u8b58\u3057\u3066\u304a\u304d\u305f\u3044\u8a2d\u5b9a\u3068\u305d\u306e\u8a2d\u5b9a\u65b9\u6cd5\u3092\u307e\u3068\u3081\u3066\u307f\u307e\u3057\u305f\u3002 \u81ea\u5206\u306e\u73fe\u5834\u3067\u610f\u8b58\u3057\u3066\u3044\u308b\u3053\u3068\u304c\u4e2d\u5fc3\u306b\u306a\u308a\u307e\u3059\u304c\u3001\u30b3\u30d4\u30da\u3067\u305d\u306e\u307e\u307e\u672c\u756a\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306b\u4f7f\u3046\u3088\u3046\u306a &#8230; <\/p>\n","protected":false},"author":1,"featured_media":568,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[46,13],"class_list":{"0":"post-549","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-6","8":"tag-resttemplate","9":"tag-springboot"},"jetpack_featured_media_url":"https:\/\/hirabay.net\/wp-content\/uploads\/2023\/03\/RestTemplate-1.png","_links":{"self":[{"href":"https:\/\/hirabay.net\/index.php?rest_route=\/wp\/v2\/posts\/549","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hirabay.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hirabay.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hirabay.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hirabay.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=549"}],"version-history":[{"count":5,"href":"https:\/\/hirabay.net\/index.php?rest_route=\/wp\/v2\/posts\/549\/revisions"}],"predecessor-version":[{"id":687,"href":"https:\/\/hirabay.net\/index.php?rest_route=\/wp\/v2\/posts\/549\/revisions\/687"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/hirabay.net\/index.php?rest_route=\/wp\/v2\/media\/568"}],"wp:attachment":[{"href":"https:\/\/hirabay.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hirabay.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hirabay.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}