maven 引用http工具类
<!--hutool 的http工具类-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-http</artifactId>
</dependency>
Java 通过 http请求,操作nacos
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import lombok.SneakyThrows;
import org.gitlab4j.api.GitLabApiException;
import org.springframework.util.StringUtils;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Nacos 工具类
*/
public class test3 {
private static final String serverAddr = "11.11.111.111:8848";
private static final String namespaceName = "project_dev_name"; //命名空间名 默认项目编码_环境,比如:
private static final String namespaceDesc = "描述";
private static final String customNamespaceId = "project_dev_id"; //命名空间ID 默认namespaceName
private static final String service_name = "service01"; //服务名称
//项目
private static final String project_name="zcnewproject06";
@SneakyThrows
public static void main(String[] args) {
// createNacosNamespace();
createOrUpdateProfile(null,null,"application.yml");
getProfile();
}
/**
* 查询配置文件
* @param
* @return
* @throws GitLabApiException
*/
public static void getProfile() {
String requestBodyStr = "show=all&group=DEFAULT_GROUP&dataId=" + project_name+"-"+ service_name + ".yml&tenant="+customNamespaceId+"&namespaceId="+customNamespaceId;
String url =serverAddr+"/nacos/v1/cs/configs?"+requestBodyStr;
HttpRequest request = HttpRequest.get(url)
.header("Content-Type", "application/x-www-form-urlencoded")
;
HttpResponse response = request.execute();
//response 返回有值 配置已存在, 试试别的dataid和group的组合吧
if (response.getStatus() == 200) {
System.out.println("创建命名空间成功,返回结构为:" +response.body());
} else {
System.out.println("创建命名空间失败,状态码:" + response.getStatus());
System.out.println("错误信息:" + response.body());
}
}
/**
* 创建或者更新配置文件
* @param
* @return
* @throws GitLabApiException
*/
public static void createOrUpdateProfile(String id, String md5,String fileName) {
FileReader fileReader = new FileReader(fileName);
String content = fileReader.readString();
System.out.println("文件内容:" + content);
// 创建一个Map存储请求参数
Map<String, String> params = new HashMap<>();
params.put("type","yaml");
params.put("group","DEFAULT_GROUP");
params.put("dataId",project_name+"-"+ service_name );
params.put("tenant",customNamespaceId);
params.put("namespaceId",customNamespaceId);
params.put("content",content);
if (StrUtil.isBlank(id)) {
params.put("id",id);
}
if (StrUtil.isBlank(md5)) {
params.put("md5",md5);
}
// 将Map转换为URL编码格式的字符串
String requestBodyStr = params.entrySet().stream()
.map(entry -> {
try {
return entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
})
.collect(Collectors.joining("&"));
if(requestBodyStr == null){
System.out.println("创建配置文件失败,请求参数解析有误");
}
String url =serverAddr+"/nacos/v1/cs/configs?";
HttpRequest request = HttpRequest.post(url)
.header("Content-Type", "application/x-www-form-urlencoded")
.body(requestBodyStr)
;
HttpResponse response = request.execute();
if (response.getStatus() == 200) {
System.out.println("创建命名空间成功,返回结构为:" +response.body());
} else {
System.out.println("创建命名空间失败,状态码:" + response.getStatus());
System.out.println("错误信息:" + response.body());
}
}
/**
* 创建命名空间
* @param
* @return
* @throws GitLabApiException
*/
public static void createNacosNamespace() {
String requestBodyStr = "namespaceName=" + namespaceName + "&namespaceDesc="+namespaceDesc+"&customNamespaceId="+customNamespaceId;
String url =serverAddr+"/nacos/v1/console/namespaces?"+requestBodyStr;
HttpRequest request = HttpRequest.post(url)
.header("Content-Type", "application/x-www-form-urlencoded")
;
HttpResponse response = request.execute();
if (response.getStatus() == 200) {
System.out.println("创建命名空间成功,返回结构为:" +response.body());
} else {
System.out.println("创建命名空间失败,状态码:" + response.getStatus());
System.out.println("错误信息:" + response.body());
}
}
}