[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"question-mikroservisy-kak-organizovat-sinkhronnoe-vzaimodeystvie-mikroservisov-cherez-rest":3},{"id":4,"slug":5,"topicId":6,"topicSlug":7,"topicName":8,"topicEmoji":9,"question":10,"answer":11,"codeLang":12,"codeSrc":12,"important":12,"commonMistakes":12,"modernUsage":12,"difficulty":13,"tags":14,"related":16,"progress":17,"seo":18},822,"kak-organizovat-sinkhronnoe-vzaimodeystvie-mikroservisov-cherez-rest",23,"mikroservisy","Микросервисы","🔗","Как организовать синхронное взаимодействие микросервисов через REST?","В Spring Boot для синхронного взаимодействия между микросервисами используются HTTP-клиенты: RestTemplate (устаревший), WebClient (рекомендуемый), Declarative HTTP Client (Spring 6+) и OpenFeign.\n\n\u003Cdetails>\u003Csummary>1. RestTemplate (устаревший, но ещё встречается)\u003C\u002Fsummary>\n\n```java\n@Service\npublic class PaymentService {\n    private final RestTemplate restTemplate;\n\n    public PaymentService(RestTemplateBuilder builder) {\n        this.restTemplate = builder\n            .rootUri(\"http:\u002F\u002Fcustomer-service\")\n            .setConnectTimeout(Duration.ofSeconds(3))\n            .setReadTimeout(Duration.ofSeconds(5))\n            .build();\n    }\n\n    public CustomerDto getCustomer(Long customerId) {\n        return restTemplate.getForObject(\n            \"\u002Fapi\u002Fcustomers\u002F{id}\", CustomerDto.class, customerId\n        );\n    }\n}\n```\n\n\u003C\u002Fdetails>\n\n\u003Cdetails>\u003Csummary>2. WebClient (реактивный, рекомендуется)\u003C\u002Fsummary>\n\n```java\n@Service\npublic class PaymentService {\n    private final WebClient webClient;\n\n    public PaymentService(WebClient.Builder builder) {\n        this.webClient = builder\n            .baseUrl(\"http:\u002F\u002Fcustomer-service\")\n            .build();\n    }\n\n    public Mono\u003CCustomerDto> getCustomer(Long customerId) {\n        return webClient.get()\n            .uri(\"\u002Fapi\u002Fcustomers\u002F{id}\", customerId)\n            .retrieve()\n            .bodyToMono(CustomerDto.class);\n    }\n}\n```\n\n\u003C\u002Fdetails>\n\n\u003Cdetails>\u003Csummary>3. Declarative HTTP Client (Spring 6+ \u002F Spring Boot 3+)\u003C\u002Fsummary>\n\n```java\n\u002F\u002F Декларативный клиент — аналог Feign, но нативный Spring\n@HttpExchange(\"\u002Fapi\u002Fcustomers\")\npublic interface CustomerClient {\n\n    @GetExchange(\"\u002F{id}\")\n    CustomerDto getCustomer(@PathVariable Long id);\n\n    @PostExchange\n    CustomerDto createCustomer(@RequestBody CreateCustomerRequest request);\n}\n\n@Configuration\npublic class ClientConfig {\n    @Bean\n    public CustomerClient customerClient(WebClient.Builder builder) {\n        WebClient webClient = builder.baseUrl(\"http:\u002F\u002Fcustomer-service\").build();\n        HttpServiceProxyFactory factory = HttpServiceProxyFactory\n            .builderFor(WebClientAdapter.create(webClient))\n            .build();\n        return factory.createClient(CustomerClient.class);\n    }\n}\n```\n\n\u003C\u002Fdetails>\n\n\u003Cdetails>\u003Csummary>4. OpenFeign (Spring Cloud)\u003C\u002Fsummary>\n\n```java\n@FeignClient(name = \"customer-service\", url = \"${customer-service.url}\")\npublic interface CustomerClient {\n\n    @GetMapping(\"\u002Fapi\u002Fcustomers\u002F{id}\")\n    CustomerDto getCustomer(@PathVariable(\"id\") Long id);\n}\n```\n\n\u003C\u002Fdetails>\n\n### Важные моменты\n\n- Всегда устанавливайте таймауты (connect timeout, read timeout).\n- Используйте Circuit Breaker для защиты от каскадных сбоев.\n- Применяйте retry с exponential backoff для transient errors.\n- При использовании Service Discovery имена сервисов резолвятся автоматически.\n\n> **На собеседовании:** знайте эволюцию: RestTemplate -> WebClient -> Declarative HTTP Client. В Spring Boot 3+ рекомендуется Declarative HTTP Client или WebClient. Feign остаётся в Spring Cloud-экосистеме.","","middle",[15],"microservices",[],null,{"title":19,"description":20,"ogTitle":19,"ogDescription":21,"keywords":22,"schemaAnswer":23,"featuredSnippetReady":24},"Как организовать синхронное взаимодействие микросервисов чер — Gymterview","В Spring Boot для синхронного взаимодействия между микросервисами используются HTTP-клиенты: RestTemplate (устаревший), WebClient (рекомендуемый), Declarative H","В Spring Boot для синхронного взаимодействия между микросервисами используются HTTP-клиенты: RestTemplate (устаревший), ",[15,13],"В Spring Boot для синхронного взаимодействия между микросервисами используются HTTP-клиенты: RestTemplate (устаревший), WebClient (рекомендуемый), Declarative HTTP Client (Spring 6+) и OpenFeign.",true]