初始化
commit
c010c0e776
|
@ -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,41 @@
|
|||
<?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>
|
||||
|
||||
|
||||
<groupId>com.muyu</groupId>
|
||||
|
||||
<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>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.dtflys.forest</groupId>
|
||||
<artifactId>forest-spring-boot3-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,25 @@
|
|||
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:杨鹏
|
||||
* @Package:com.muyu.common.nacos
|
||||
* @Project:cloud-common-nacos-remote
|
||||
* @name:NacosRemoteConfig
|
||||
* @Date:2024/8/9 22:15
|
||||
*/
|
||||
@Configuration
|
||||
@ForestScan(basePackages = "com.muyu.common.nacos.remote")
|
||||
@Import(
|
||||
value = {
|
||||
NacosNamespaceInterceptor.class,
|
||||
NacosServerService.class
|
||||
}
|
||||
)
|
||||
public class NacosRemoteConfig {
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.muyu.common.nacos.remote;
|
||||
|
||||
import com.dtflys.forest.annotation.Address;
|
||||
import com.dtflys.forest.annotation.BaseRequest;
|
||||
import com.dtflys.forest.annotation.GetRequest;
|
||||
import com.dtflys.forest.annotation.Query;
|
||||
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:杨鹏
|
||||
* @Package:com.muyu.common.nacos.api
|
||||
* @Project:cloud-common-nacos-remote
|
||||
* @name:NacosServiceRemote
|
||||
* @Date:2024/8/9 20:02
|
||||
*/
|
||||
|
||||
@BaseRequest(
|
||||
baseURL = Constants.HTTP +"#{nacos.addr}/nacos/v1/ns/service",
|
||||
interceptor = {NacosNamespaceInterceptor.class}
|
||||
)
|
||||
public interface NacosServerRemote {
|
||||
|
||||
@GetRequest(
|
||||
url = "/list"
|
||||
)
|
||||
public ServiceListResp serviceList(@Query ServiceListReq serviceListReq);
|
||||
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
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 lombok.extern.log4j.Log4j2;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
|
||||
/**
|
||||
* @Author:杨鹏
|
||||
* @Package:com.muyu.common.nacos.remote.interceptor
|
||||
* @Project:cloud-common-nacos-remote
|
||||
* @name:NacosNamespaceInterceptor
|
||||
* @Date:2024/8/9 22:06
|
||||
*/
|
||||
@Log4j2
|
||||
public class NacosNamespaceInterceptor<T> implements Interceptor {
|
||||
|
||||
@Value("${nacos.namespace}")
|
||||
private String namespaceId;
|
||||
|
||||
/**
|
||||
* 该方法在请求发送之前被调用, 若返回false则不会继续发送请求
|
||||
*
|
||||
* @Param request Forest请求对象
|
||||
*/
|
||||
@Override
|
||||
public boolean beforeExecute(ForestRequest req) {
|
||||
if (StringUtils.isNotEmpty(namespaceId)) {
|
||||
String reqNamespaceId = Convert.utf8Str(req.getQuery("namespaceId"));
|
||||
if (reqNamespaceId == null) {
|
||||
log.warn("本次请求nacos的namespaceId:未携带,已添加[{}]", namespaceId);
|
||||
req.addQuery("namespaceId", namespaceId);
|
||||
} else {
|
||||
if (StringUtils.equals(namespaceId, reqNamespaceId)) {
|
||||
log.warn("本次请求nacos的namespaceId和项目ID不相符,现已更改:[{}——>{}]", reqNamespaceId, namespaceId);
|
||||
req.addQuery("namespaceId", namespaceId);
|
||||
}
|
||||
}
|
||||
}
|
||||
return true; // 继续执行请求返回true
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.muyu.common.nacos.remote.req;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
/**
|
||||
* @Author:杨鹏
|
||||
* @Package:com.muyu.common.nacos.remote.req
|
||||
* @Project:cloud-common-nacos-remote
|
||||
* @name:BaseReq
|
||||
* @Date:2024/8/9 22:00
|
||||
*/
|
||||
@Data
|
||||
@SuperBuilder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class BaseReq {
|
||||
|
||||
/**
|
||||
* 命名空间
|
||||
*/
|
||||
private String namespaceId;
|
||||
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.muyu.common.nacos.remote.req;
|
||||
|
||||
import lombok.*;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
/**
|
||||
* @Author:杨鹏
|
||||
* @Package:com.muyu.common.nacos.remote.req
|
||||
* @Project:cloud-common-nacos-remote
|
||||
* @name:ServiceListReq
|
||||
* @Date:2024/8/9 22:01
|
||||
*/
|
||||
@Data
|
||||
@SuperBuilder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
public class ServiceListReq extends BaseReq{
|
||||
|
||||
/**
|
||||
* 当前页码
|
||||
*/
|
||||
private int pageNo;
|
||||
/**
|
||||
* 分页大小
|
||||
*/
|
||||
@Builder.Default
|
||||
private int pageSize=2;
|
||||
/**
|
||||
* 分组名
|
||||
*/
|
||||
private String groupName;
|
||||
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.muyu.common.nacos.remote.resp;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author:杨鹏
|
||||
* @Package:com.muyu.common.nacos.remote.resp
|
||||
* @Project:cloud-common-nacos-remote
|
||||
* @name:ServiceListResp
|
||||
* @Date:2024/8/9 22:02
|
||||
*/
|
||||
@Data
|
||||
@SuperBuilder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class ServiceListResp {
|
||||
|
||||
/**
|
||||
* 总条数
|
||||
*/
|
||||
private int count;
|
||||
|
||||
/**
|
||||
* 服务列表
|
||||
*/
|
||||
private List<String> doms;
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.muyu.common.nacos.service;
|
||||
|
||||
import com.muyu.common.nacos.remote.NacosServerRemote;
|
||||
import com.muyu.common.nacos.remote.req.ServiceListReq;
|
||||
import com.muyu.common.nacos.remote.resp.ServiceListResp;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author:杨鹏
|
||||
* @Package:com.muyu.common.nacos.service
|
||||
* @Project:cloud-common-nacos-remote
|
||||
* @name:NacosServerService
|
||||
* @Date:2024/8/9 22:10
|
||||
*/
|
||||
public class NacosServerService {
|
||||
|
||||
@Autowired
|
||||
private NacosServerRemote nacosServerRemote;
|
||||
|
||||
public List<String> nacosServerAllList(){
|
||||
ArrayList<String> serverList = new ArrayList<>();
|
||||
ServiceListResp serviceListResp = null;
|
||||
int pageNo = 0, pageSize = 10;
|
||||
do {
|
||||
serviceListResp = nacosServerRemote.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