commit 5d5022b7344eb0347f2e61a8e1805b969fff7661
Author: yangpeng <3074487626@qq.com>
Date: Thu Aug 8 19:35:03 2024 +0800
初始化
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..d769462
--- /dev/null
+++ b/.gitignore
@@ -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
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..2c0f5b8
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,30 @@
+
+
+ 4.0.0
+
+
+ com.muyu
+ cloud-common
+ 3.6.3
+
+
+ cloud-common-nacos-remote
+ 1.0.0
+
+
+ 17
+ 17
+ UTF-8
+
+
+
+
+
+
+ com.muyu
+ cloud-common-core
+
+
+
\ No newline at end of file
diff --git a/src/main/java/com/muyu/common/nacos/NacosRemoteConfig.java b/src/main/java/com/muyu/common/nacos/NacosRemoteConfig.java
new file mode 100644
index 0000000..a3aeac0
--- /dev/null
+++ b/src/main/java/com/muyu/common/nacos/NacosRemoteConfig.java
@@ -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 {
+}
diff --git a/src/main/java/com/muyu/common/nacos/remote/NacosServiceRemote.java b/src/main/java/com/muyu/common/nacos/remote/NacosServiceRemote.java
new file mode 100644
index 0000000..2f53788
--- /dev/null
+++ b/src/main/java/com/muyu/common/nacos/remote/NacosServiceRemote.java
@@ -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);
+}
diff --git a/src/main/java/com/muyu/common/nacos/remote/interceptor/NacosNamespaceInterceptor.java b/src/main/java/com/muyu/common/nacos/remote/interceptor/NacosNamespaceInterceptor.java
new file mode 100644
index 0000000..cd34154
--- /dev/null
+++ b/src/main/java/com/muyu/common/nacos/remote/interceptor/NacosNamespaceInterceptor.java
@@ -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 implements Interceptor {
+
+ @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
+ }
+}
+
diff --git a/src/main/java/com/muyu/common/nacos/remote/req/BaseReq.java b/src/main/java/com/muyu/common/nacos/remote/req/BaseReq.java
new file mode 100644
index 0000000..2f0b1ff
--- /dev/null
+++ b/src/main/java/com/muyu/common/nacos/remote/req/BaseReq.java
@@ -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;
+
+
+}
diff --git a/src/main/java/com/muyu/common/nacos/remote/req/ServiceListReq.java b/src/main/java/com/muyu/common/nacos/remote/req/ServiceListReq.java
new file mode 100644
index 0000000..1fd6d68
--- /dev/null
+++ b/src/main/java/com/muyu/common/nacos/remote/req/ServiceListReq.java
@@ -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;
+}
diff --git a/src/main/java/com/muyu/common/nacos/remote/resp/ServiceListResp.java b/src/main/java/com/muyu/common/nacos/remote/resp/ServiceListResp.java
new file mode 100644
index 0000000..f2f1855
--- /dev/null
+++ b/src/main/java/com/muyu/common/nacos/remote/resp/ServiceListResp.java
@@ -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 doms;
+}
diff --git a/src/main/java/com/muyu/common/nacos/service/NacosServerService.java b/src/main/java/com/muyu/common/nacos/service/NacosServerService.java
new file mode 100644
index 0000000..d398377
--- /dev/null
+++ b/src/main/java/com/muyu/common/nacos/service/NacosServerService.java
@@ -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 nacosServerAllList() {
+ ArrayList 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;
+ }
+}
diff --git a/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
new file mode 100644
index 0000000..6dca31a
--- /dev/null
+++ b/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
@@ -0,0 +1 @@
+com.muyu.common.nacos.NacosRemoteConfig
\ No newline at end of file