HttpTodoConfig.java 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. //package com.backendsys.service.HttpTodo;
  2. //
  3. //import io.netty.channel.ChannelOption;
  4. //import io.netty.handler.timeout.ReadTimeoutHandler;
  5. //import io.netty.handler.timeout.WriteTimeoutHandler;
  6. //import lombok.SneakyThrows;
  7. //import org.springframework.beans.factory.annotation.Value;
  8. //import org.springframework.context.annotation.Bean;
  9. //import org.springframework.context.annotation.Configuration;
  10. //import org.springframework.http.HttpStatusCode;
  11. //import org.springframework.web.reactive.function.client.WebClient;
  12. //import org.springframework.web.reactive.function.client.support.WebClientAdapter;
  13. //import org.springframework.web.service.invoker.HttpServiceProxyFactory;
  14. //import reactor.core.publisher.Mono;
  15. //import reactor.netty.http.client.HttpClient;
  16. //
  17. // // 全局配置,仅可用于同一个域的配置
  18. //
  19. //@Configuration
  20. //public class HttpTodoConfig {
  21. //
  22. // // 引用配置变量
  23. // @Value("${HTTP_ACTUATOR_URI}")
  24. // private String baseUrl;
  25. //
  26. // // 定制Http服务
  27. // @Bean
  28. // public WebClient webClient() {
  29. //
  30. // System.out.println("========= webClient ===========");
  31. //
  32. // HttpClient httpClient = HttpClient.create()
  33. // .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 30 * 1000) // 连接时间 3W 毫秒
  34. // .doOnConnected(conn -> {
  35. // conn.addHandlerLast(new ReadTimeoutHandler(10)); // 读超时 10秒
  36. // conn.addHandlerLast(new WriteTimeoutHandler(10)); // 写超时 10秒
  37. // });
  38. //
  39. //
  40. // return WebClient.builder()
  41. // .baseUrl(baseUrl)
  42. // .defaultStatusHandler(HttpStatusCode::isError, clientResponse -> {
  43. // System.out.println("WebClient defaultStatusHandler:");
  44. // System.out.println(clientResponse.statusCode());
  45. // return Mono.error(new RuntimeException(clientResponse.statusCode().toString()));
  46. // })
  47. // //.defaultStatusHandler(HttpStatusCode::is4xxClientError,
  48. // // error -> Mono.error(new RuntimeException("API not found")))
  49. // //.defaultStatusHandler(HttpStatusCode::is5xxServerError,
  50. // // error -> Mono.error(new RuntimeException("Server is not responding")))
  51. // .build();
  52. // }
  53. //
  54. // // 创建代理
  55. // @SneakyThrows
  56. // @Bean
  57. // public HttpTodoService requestTodoService(WebClient webClient) {
  58. // HttpServiceProxyFactory httpServiceProxyFactory = HttpServiceProxyFactory.builder(WebClientAdapter.forClient(webClient)).build();
  59. // return httpServiceProxyFactory.createClient(HttpTodoService.class);
  60. // }
  61. //
  62. //}