1、引入 OKHTTP 依赖

		<dependency>
			<groupId>com.squareup.okhttp3</groupId>
			<artifactId>okhttp</artifactId>
			<version>4.12.0</version>
		</dependency>

2、配置 OkHttpClient 客户端实例

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
public class OkHttpConfiguration {

    @Bean
    public okhttp3.OkHttpClient okHttpClient() {
        final okhttp3.Dispatcher dispatcher = new okhttp3.Dispatcher();
        dispatcher.setMaxRequests(Integer.MAX_VALUE);
        dispatcher.setMaxRequestsPerHost(Integer.MAX_VALUE);

        return new okhttp3.OkHttpClient.Builder()
                .connectionPool(new okhttp3.ConnectionPool(200, 5L, TimeUnit.MINUTES))
                .dispatcher(dispatcher)
                .connectTimeout(2, TimeUnit.SECONDS)
                .readTimeout(3, TimeUnit.SECONDS)
                .build();
    }

}

3、请求调用

import okhttp3.*;
import org.jetbrains.annotations.NotNull;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@RestController
public class IndexController {

    private final OkHttpClient okHttpClient;

    public IndexController(OkHttpClient okHttpClient) {
        this.okHttpClient = okHttpClient;
    }

    @GetMapping(path = "index", produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> execute(String arg) {
        final Map<String, Object> result = new HashMap<>(3);
        final Request request = new Request.Builder()
                .url("http://x.y.z/ivr/index")
                .post((new FormBody.Builder()).build())
                .build();

        final CompletableFuture<String> future = new CompletableFuture<>();
        this.okHttpClient.newCall(request).enqueue(new Callback() {

            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                future.completeExceptionally(e);
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                if (response.isSuccessful()) {
                    future.complete(response.body().string());
                } else {
                    future.completeExceptionally(new IOException("Unexpected code " + response));
                }
            }

        });

        result.put("timestamp", LocalDateTime.now());
        result.put("arg", arg);
        final String futureResult;
        try {
            futureResult = future.get();
            result.put("txt", futureResult);
        } catch (InterruptedException | ExecutionException e) {
            result.put("txt", e.getMessage());
        }


        return result;
    }
}
posted @ 2024-12-05 11:09 Bruce.Chang.Lee 阅读(181) 评论(0) 推荐(0) 编辑
摘要: 版本要求,druid-1.2.23.jar及以上版本生效 connectTimeout: 30000 # 建立链接需要的时间。该参数只在建立链接阶段生效。默认是0,不超时。建议配置connectTimeout=60000,单位毫秒。 socketTimeout: 60000 # 发送请求给数据库(建 阅读全文
posted @ 2024-11-01 19:55 Bruce.Chang.Lee 阅读(906) 评论(0) 推荐(0) 编辑
摘要: 目录1、使用root用户权限,创建名为solr的新用户,用于运行SOLR服务。2、使用root用户权限,将solr安装目录授权给solr用户3、从root用户切换到solr用户4、进入/opt/solr-8.11.4/bin目录,启动服务5、如果启动出现如下警告,若如下配置5.1、修改/etc/se 阅读全文
posted @ 2024-10-11 14:20 Bruce.Chang.Lee 阅读(74) 评论(0) 推荐(0) 编辑
摘要: 目录1、背景2、使用步骤2.1、pom.xml中引入相关依赖2.2、启用@Retryable2.3、在响应方法上加上注解 1、背景 日常开发中经常会下发接口回调通知失败、远程调用失败等等,这些错误可能是因为网络波动造成的,等待过后重处理就能成功。通常来说,会用try/catch,while循环之类的 阅读全文
posted @ 2023-12-21 16:25 Bruce.Chang.Lee 阅读(49) 评论(0) 推荐(0) 编辑
摘要: 1、生成订单号 import org.apache.commons.lang3.RandomStringUtils; public final class OrderUtils { /** * 生成20位长度订单号 * * @param id 商户ID * @return 20位长度订单号 */ p 阅读全文
posted @ 2023-12-21 14:48 Bruce.Chang.Lee 阅读(4) 评论(0) 推荐(0) 编辑
摘要: 目录1、背景2、常用注解2.1、Bean Validation 2.0 注解2.1.1、校验空值2.1.2、校验大小2.1.3、校验布尔值2.1.4、校验日期和时间2.1.5、正则表达式2.1.5.1、验证IP地址2.1.5.1、验证手机号码3、 使用3.1、当方法入参为 @PathVariable 阅读全文
posted @ 2023-12-21 13:39 Bruce.Chang.Lee 阅读(130) 评论(0) 推荐(0) 编辑
摘要: 目录1、什么是延时队列2、延时队列使用场景3、RabbitMQ中的TTL4、如果使用RabbitMQ来实现延时队列4.1、安装插件4.2、代码实现4.2.1、新建maven工程,pom.xml文件4.2.2、属性配置文件application.yml4.2.3、定义常量4.2.3、配置RabbitM 阅读全文
posted @ 2023-12-20 15:53 Bruce.Chang.Lee 阅读(124) 评论(0) 推荐(0) 编辑
摘要: Module语法 export命令 分别导出 export const x = 9; export const y = () => { console.log(`x = ${x}`); }; 统一导出 const x = 9; const y = () => { console.log(`x = $ 阅读全文
posted @ 2023-04-28 17:04 Bruce.Chang.Lee 阅读(15) 评论(0) 推荐(0) 编辑
摘要: 说明 下载app页面,在微信中分享,很容易被拦截。这里作了一个处理,当在微信中打开链接,自动跳转到浏览器中打开提示下载。 代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>XXX客户端</title> <style ty 阅读全文
posted @ 2023-04-20 16:30 Bruce.Chang.Lee 阅读(167) 评论(0) 推荐(0) 编辑
摘要: 1、简介 MapStruct是一个Java注释处理器,用于生成类型安全的bean映射类。 您要做的就是定义一个映射器接口,该接口声明任何必需的映射方法。在编译期间,MapStruct将生成此接口的实现。此实现使用简单的Java方法调用在源对象和目标对象之间进行映射,即没有反射或类似内容。 与手动编写 阅读全文
posted @ 2023-04-12 14:54 Bruce.Chang.Lee 阅读(1777) 评论(0) 推荐(0) 编辑
点击右上角即可分享
微信分享提示