目录
1、引入maven依赖
<!-- OKHttp3依赖 -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.8.1</version>
</dependency>
2、CkConfig.java
import lombok.Data;
import org.springframework.stereotype.Component;
/**
* Created By ZhuKeqian on 2020/8/11
*/
@Component
@Data
public class CkConfig {
// 在这里我写成固定值
private String url = "http://******";
private String cluster = "your_cluster_name";
private String database = "default";
private String user = "user_name";
private String password = "ck_password";
}
3、CkClient.java
import com.alibaba.fastjson.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* Created By ZhuKeqian on 2020/8/11
*/
@Slf4j
@Component
public class CkClient {
@Autowired
CkConfig ckConfig;
private static OkHttpClient client = null;
public static OkHttpClient getClient() {
if (client == null) {
client = new OkHttpClient.Builder()
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(1, TimeUnit.SECONDS).build();
}
return client;
}
private stati