Selaa lähdekoodia

file-service 在 oss-store 首次连接时会自动初始化 UserNode 和 UploadChannel

reghao 2 kuukautta sitten
vanhempi
commit
e8ecf39d3f

+ 1 - 10
file/file-service/src/main/java/cn/reghao/tnb/file/app/rpc/StoreServiceWrapperRouter.java

@@ -14,7 +14,6 @@ import cn.reghao.tnb.file.app.zoss.db.repository.StoreRepository;
 import cn.reghao.tnb.file.app.model.constant.OssType;
 import cn.reghao.tnb.file.app.zoss.model.po.UploadChannel;
 import cn.reghao.tnb.file.app.service.StoreConfigService;
-import cn.reghao.tnb.file.app.zoss.service.UploadChannelService;
 import org.apache.dubbo.config.annotation.DubboService;
 import org.springframework.stereotype.Service;
 
@@ -30,27 +29,19 @@ public class StoreServiceWrapperRouter implements OssService {
     private final StoreServiceWrapper storeServiceWrapper;
     private final StoreConfigService storeConfigService;
     private final StoreRepository storeRepository;
-    private final UploadChannelService uploadChannelService;
 
     public StoreServiceWrapperRouter(ConsoleService consoleService, StoreServiceWrapper storeServiceWrapper,
-                                     StoreConfigService storeConfigService, StoreRepository storeRepository,
-                                     UploadChannelService uploadChannelService) {
+                                     StoreConfigService storeConfigService, StoreRepository storeRepository) {
         this.consoleService = consoleService;
         this.storeServiceWrapper = storeServiceWrapper;
         this.storeConfigService = storeConfigService;
         this.storeRepository = storeRepository;
-        this.uploadChannelService = uploadChannelService;
     }
 
     @Override
     public ServerInfo getUploadStore(String channelName) throws Exception {
         int ossUser = storeConfigService.getLocalOssUser();
         UploadChannel uploadChannel = storeRepository.getUploadChannel(ossUser, channelName);
-        if (uploadChannel == null) {
-            uploadChannelService.initUploadChannel();
-            uploadChannel = storeRepository.getUploadChannel(ossUser, channelName);
-        }
-
         int channelCode = uploadChannel.getChannelCode();
         return consoleService.getUploadStore(channelCode);
     }

+ 19 - 7
file/file-service/src/main/java/cn/reghao/tnb/file/app/zoss/service/StoreNodeService.java

@@ -15,6 +15,7 @@ import cn.reghao.tnb.file.app.zoss.model.po.StoreVolume;
 import cn.reghao.tnb.common.db.SelectOption;
 import cn.reghao.tnb.file.app.zoss.model.vo.StoreNodeInfo;
 import cn.reghao.tnb.file.app.zoss.rpc.StoreServiceWrapper;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 
 import java.util.List;
@@ -24,6 +25,7 @@ import java.util.stream.Collectors;
  * @author reghao
  * @date 2024-02-23 13:01:38
  */
+@Slf4j
 @Service
 public class StoreNodeService {
     private final ByteConverter byteConverter;
@@ -33,11 +35,12 @@ public class StoreNodeService {
     private final StoreServiceWrapper storeServiceWrapper;
     private final StoreRepository storeRepository;
     private final UserNodeService userNodeService;
+    private final UploadChannelService uploadChannelService;
 
     public StoreNodeService(ByteConverter byteConverter, StoreNodeMapper storeNodeMapper,
                             StoreVolumeMapper storeVolumeMapper, UserNodeMapper userNodeMapper,
                             StoreServiceWrapper storeServiceWrapper, StoreRepository storeRepository,
-                            UserNodeService userNodeService) {
+                            UserNodeService userNodeService, UploadChannelService uploadChannelService) {
         this.byteConverter = byteConverter;
         this.storeNodeMapper = storeNodeMapper;
         this.storeVolumeMapper = storeVolumeMapper;
@@ -45,6 +48,7 @@ public class StoreNodeService {
         this.storeServiceWrapper = storeServiceWrapper;
         this.storeRepository = storeRepository;
         this.userNodeService = userNodeService;
+        this.uploadChannelService = uploadChannelService;
     }
 
     public void addOrUpdate(StoreNodeDto storeNodeDto) {
@@ -53,12 +57,7 @@ public class StoreNodeService {
         StoreNode storeNode = storeNodeMapper.findByNodeAddrAndHttpPort(nodeAddr, httpPort);
         if (storeNode == null) {
             storeRepository.saveStoreNode(storeNodeDto);
-
-            // 没有 UserNode
-            int total = userNodeMapper.findAll().size();
-            if (total == 0) {
-                userNodeService.initUserNode();
-            }
+            init();
         } else {
             int storeNodeId = storeNode.getId();
             List<StoreVolume> storeVolumes = storeNodeDto.getDiskVolumes().stream()
@@ -68,6 +67,19 @@ public class StoreNodeService {
         }
     }
 
+    private void init() {
+        // 没有 UserNode
+        int total = userNodeMapper.findAll().size();
+        if (total == 0) {
+            userNodeService.initUserNode();
+        }
+        try {
+            uploadChannelService.initUploadChannel();
+        } catch (Exception e) {
+            log.error("初始化 UploadChannel 异常: {}", e.getMessage());
+        }
+    }
+
     public Result updateStatus(int storeNodeId) {
         StoreNode storeNode = storeNodeMapper.findById(storeNodeId);
         if (storeNode != null) {

+ 0 - 5
file/file-service/src/main/java/cn/reghao/tnb/file/app/zoss/service/UploadChannelService.java

@@ -74,11 +74,6 @@ public class UploadChannelService {
     public void initUploadChannel() throws Exception {
         int ossUser = storeConfigService.getLocalOssUser();
         List<UserNode> list = userNodeService.getUserNodes(ossUser);
-        if (list.isEmpty()) {
-            userNodeService.initUserNode();
-            list = userNodeService.getUserNodes(ossUser);
-        }
-
         if (list.size() == 1 && uploadChannelMapper.findByCreateBy(ossUser).isEmpty()) {
             int userNodeId = list.get(0).getId();
             for (UploadChannelType uploadChannelType : UploadChannelType.values()) {