初始化
commit
5d5022b734
|
@ -0,0 +1,35 @@
|
||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
!**/src/main/**/target/
|
||||||
|
!**/src/test/**/target/
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### Eclipse ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
### Mac OS ###
|
||||||
|
.DS_Store
|
|
@ -0,0 +1,30 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.muyu</groupId>
|
||||||
|
<artifactId>cloud-common</artifactId>
|
||||||
|
<version>3.6.3</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>cloud-common-nacos-remote</artifactId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
|
<maven.compiler.target>17</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<!-- 公共核心包依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.muyu</groupId>
|
||||||
|
<artifactId>cloud-common-core</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.muyu.common.nacos;
|
||||||
|
|
||||||
|
import com.dtflys.forest.springboot.annotation.ForestScan;
|
||||||
|
import com.muyu.common.nacos.remote.interceptor.NacosNamespaceInterceptor;
|
||||||
|
import com.muyu.common.nacos.service.NacosServerService;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: by 杨旭飞
|
||||||
|
* @Date 2024/8/7 22:07
|
||||||
|
* @Description nacos远程调用启动类
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@ForestScan(basePackages = "com.muyu.common.nacos.remote")
|
||||||
|
@Import(value = {
|
||||||
|
NacosNamespaceInterceptor.class,
|
||||||
|
NacosServerService.class
|
||||||
|
})
|
||||||
|
public class NacosRemoteConfig {
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package com.muyu.common.nacos.remote;
|
||||||
|
|
||||||
|
import com.dtflys.forest.annotation.BaseRequest;
|
||||||
|
import com.dtflys.forest.annotation.Body;
|
||||||
|
import com.dtflys.forest.annotation.GetRequest;
|
||||||
|
import com.muyu.common.core.constant.Constants;
|
||||||
|
import com.muyu.common.nacos.remote.interceptor.NacosNamespaceInterceptor;
|
||||||
|
import com.muyu.common.nacos.remote.req.ServiceListReq;
|
||||||
|
import com.muyu.common.nacos.remote.resp.ServiceListResp;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: by 杨旭飞
|
||||||
|
* @Date 2024/8/6 21:19
|
||||||
|
* @Description nacos服务api
|
||||||
|
*/
|
||||||
|
@BaseRequest(
|
||||||
|
baseURL = Constants.HTTP + "#{nacos.addr}/nacos/v1/ns/service",
|
||||||
|
interceptor = {NacosNamespaceInterceptor.class}
|
||||||
|
)
|
||||||
|
public interface NacosServiceRemote {
|
||||||
|
|
||||||
|
@GetRequest(url = "/list")
|
||||||
|
public ServiceListResp serviceList(@Body ServiceListReq serviceListReq);
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
package com.muyu.common.nacos.remote.interceptor;
|
||||||
|
|
||||||
|
import com.dtflys.forest.http.ForestRequest;
|
||||||
|
import com.dtflys.forest.interceptor.Interceptor;
|
||||||
|
import com.muyu.common.core.text.Convert;
|
||||||
|
import com.muyu.common.core.utils.StringUtils;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: by 杨旭飞
|
||||||
|
* @Date 2024/8/10 8:03
|
||||||
|
* @Description nacos命名空间拦截器
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Log4j2
|
||||||
|
public class NacosNamespaceInterceptor<T> implements Interceptor<T> {
|
||||||
|
|
||||||
|
@Value("${nacos.namespace}")
|
||||||
|
private String namespaceId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 该方法在请求发送之前被调用, 若返回false则不会继续发送请求
|
||||||
|
* @Param request Forest请求对象
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean beforeExecute(ForestRequest req) {
|
||||||
|
if (StringUtils.isNotBlank(namespaceId)) {
|
||||||
|
String reqNamespaceId = Convert.utf8Str(req.getQuery("namespaceId"));
|
||||||
|
if (reqNamespaceId == null) {
|
||||||
|
log.warn("本次请求nacos的namespaceId未携带,已添加:[{}]", namespaceId);
|
||||||
|
req.addQuery("namespaceId", namespaceId); // 添加URL的Query参数
|
||||||
|
} else {
|
||||||
|
if(StringUtils.equals(reqNamespaceId, namespaceId)) {
|
||||||
|
log.warn("本次请求nacos的namespaceId和项目ID不相符,现已更改:[{}->{}]", reqNamespaceId, namespaceId);
|
||||||
|
req.addQuery("namespaceId", namespaceId); // 添加URL的Query参数
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true; // 继续执行请求返回true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
package com.muyu.common.nacos.remote.req;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.experimental.SuperBuilder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: by 杨旭飞
|
||||||
|
* @Date 2024/8/6 23:47
|
||||||
|
* @Description 基础请求
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@SuperBuilder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class BaseReq {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 命名空间ID
|
||||||
|
*/
|
||||||
|
private String namespaceId;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
package com.muyu.common.nacos.remote.req;
|
||||||
|
|
||||||
|
import lombok.*;
|
||||||
|
import lombok.experimental.SuperBuilder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: by 杨旭飞
|
||||||
|
* @Date 2024/8/6 23:48
|
||||||
|
* @Description 服务列表请求参数
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@SuperBuilder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
public class ServiceListReq extends BaseReq {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当前页码
|
||||||
|
*/
|
||||||
|
private int pageNo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分页大小
|
||||||
|
*/
|
||||||
|
@Builder.Default
|
||||||
|
private int pageSize = 10;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分组名
|
||||||
|
*/
|
||||||
|
private String groupName;
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
package com.muyu.common.nacos.remote.resp;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: by 杨旭飞
|
||||||
|
* @Date 2024/8/6 23:55
|
||||||
|
* @Description nacos服务列表响应
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class ServiceListResp {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 总条数
|
||||||
|
*/
|
||||||
|
private int count;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 服务列表
|
||||||
|
*/
|
||||||
|
private List<String> doms;
|
||||||
|
}
|
|
@ -0,0 +1,37 @@
|
||||||
|
package com.muyu.common.nacos.service;
|
||||||
|
|
||||||
|
import com.muyu.common.nacos.remote.NacosServiceRemote;
|
||||||
|
import com.muyu.common.nacos.remote.req.ServiceListReq;
|
||||||
|
import com.muyu.common.nacos.remote.resp.ServiceListResp;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: by 杨旭飞
|
||||||
|
* @Date 2024/8/7 0:49
|
||||||
|
* @Description TODO nacos服务器业务层
|
||||||
|
*/
|
||||||
|
public class NacosServerService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private NacosServiceRemote nacosServiceRemote;
|
||||||
|
|
||||||
|
public List<String> nacosServerAllList() {
|
||||||
|
ArrayList<String> serverList = new ArrayList<>();
|
||||||
|
ServiceListResp serviceListResp = null;
|
||||||
|
int pageNo = 1, pageSize = 2;
|
||||||
|
do {
|
||||||
|
nacosServiceRemote.serviceList(
|
||||||
|
ServiceListReq.builder()
|
||||||
|
.pageNo(++pageNo)
|
||||||
|
.pageSize(pageSize)
|
||||||
|
.build()
|
||||||
|
);
|
||||||
|
serverList.addAll(serviceListResp.getDoms());
|
||||||
|
} while (serviceListResp.getCount() > pageNo * pageSize);
|
||||||
|
return serverList;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
com.muyu.common.nacos.NacosRemoteConfig
|
Loading…
Reference in New Issue