|
|
@@ -1,131 +0,0 @@
|
|
|
-package cn.reghao.autodop.dmaster.app.service;
|
|
|
-
|
|
|
-import cn.reghao.autodop.common.message.AsyncMsg;
|
|
|
-import cn.reghao.autodop.common.amqp.RabbitProducer;
|
|
|
-import cn.reghao.autodop.common.amqp.RpcResult;
|
|
|
-import cn.reghao.autodop.common.utils.ExceptionUtil;
|
|
|
-import cn.reghao.autodop.dmaster.machine.entity.MachineInfo;
|
|
|
-import cn.reghao.autodop.dmaster.machine.entity.NetworkInfo;
|
|
|
-import cn.reghao.autodop.dmaster.machine.repository.MachineInfoRepository;
|
|
|
-import cn.reghao.autodop.dmaster.common.thread.ThreadPoolWrapper;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.concurrent.Callable;
|
|
|
-import java.util.concurrent.ExecutionException;
|
|
|
-import java.util.concurrent.ExecutorService;
|
|
|
-import java.util.concurrent.Future;
|
|
|
-
|
|
|
-/**
|
|
|
- * 远程调用服务
|
|
|
- *
|
|
|
- * @author reghao
|
|
|
- * @date 2021-02-10 02:31:12
|
|
|
- */
|
|
|
-@Slf4j
|
|
|
-@Service
|
|
|
-public class RemoteCallService {
|
|
|
- private ExecutorService threadPool = ThreadPoolWrapper.threadPool("remote-call-service");
|
|
|
- private final String exchange = "amq.direct";
|
|
|
- private RabbitProducer rabbitProducer;
|
|
|
- private MachineInfoRepository machineInfoRepository;
|
|
|
-
|
|
|
- public RemoteCallService(RabbitProducer rabbitProducer, MachineInfoRepository machineInfoRepository) {
|
|
|
- this.rabbitProducer = rabbitProducer;
|
|
|
- this.machineInfoRepository = machineInfoRepository;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 单个调用
|
|
|
- *
|
|
|
- * @param
|
|
|
- * @return
|
|
|
- * @date 2021-03-05 下午7:54
|
|
|
- */
|
|
|
- public RpcResult call(AsyncMsg asyncMsg) {
|
|
|
- Future<RpcResult> future = threadPool.submit(new RemoteCallTask(asyncMsg));
|
|
|
- while (!future.isDone() && !future.isCancelled()) {
|
|
|
- // 休眠等待异步任务结束
|
|
|
- try {
|
|
|
- Thread.sleep(1_000);
|
|
|
- } catch (InterruptedException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- String machineId = asyncMsg.getMachineId();
|
|
|
- try {
|
|
|
- RpcResult rpcResult = future.get();
|
|
|
- if (rpcResult == null) {
|
|
|
- return RpcResult.fail(machineIpv4(machineId) + " RPC 调用失败");
|
|
|
- }
|
|
|
- return rpcResult;
|
|
|
- } catch (InterruptedException | ExecutionException e) {
|
|
|
- return RpcResult.error(machineIpv4(machineId) + " " + ExceptionUtil.errorMsg(e));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 多个调用
|
|
|
- *
|
|
|
- * @param
|
|
|
- * @return
|
|
|
- * @date 2021-03-05 下午7:54
|
|
|
- */
|
|
|
- public Map<String, RpcResult> call(List<AsyncMsg> asyncMsgs) {
|
|
|
- Map<String, Future<RpcResult>> futureMap = new HashMap<>(asyncMsgs.size());
|
|
|
- asyncMsgs.forEach(mqMessage ->
|
|
|
- futureMap.put(mqMessage.getMachineId(), threadPool.submit(new RemoteCallTask(mqMessage))));
|
|
|
-
|
|
|
- Map<String, RpcResult> resultMap = new HashMap<>(asyncMsgs.size());
|
|
|
- for (Map.Entry<String, Future<RpcResult>> entry : futureMap.entrySet()) {
|
|
|
- Future<RpcResult> future = entry.getValue();
|
|
|
- while (!future.isDone() && !future.isCancelled()) {
|
|
|
- // TODO 休眠等待异步任务结束
|
|
|
- try {
|
|
|
- Thread.sleep(1_000);
|
|
|
- } catch (InterruptedException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- String machineId = entry.getKey();
|
|
|
- log.info(machineIpv4(machineId) + " -> RPC 调用返回");
|
|
|
- try {
|
|
|
- RpcResult rpcResult = future.get();
|
|
|
- if (rpcResult != null) {
|
|
|
- resultMap.put(machineId, rpcResult);
|
|
|
- } else {
|
|
|
- resultMap.put(machineId, RpcResult.fail(machineIpv4(machineId) + " RPC 调用失败"));
|
|
|
- }
|
|
|
- } catch (InterruptedException | ExecutionException e) {
|
|
|
- resultMap.put(machineId,
|
|
|
- RpcResult.error(machineIpv4(machineId) + " " + ExceptionUtil.errorMsg(e)));
|
|
|
- }
|
|
|
- }
|
|
|
- return resultMap;
|
|
|
- }
|
|
|
-
|
|
|
- private String machineIpv4(String machineId) {
|
|
|
- MachineInfo machineInfo = machineInfoRepository.findByMachineId(machineId);
|
|
|
- List<NetworkInfo> networkInfos = machineInfo.getNetworkInfo();
|
|
|
- return networkInfos.get(0).getIpv4();
|
|
|
- }
|
|
|
-
|
|
|
- class RemoteCallTask implements Callable<RpcResult> {
|
|
|
- private AsyncMsg asyncMsg;
|
|
|
-
|
|
|
- public RemoteCallTask(AsyncMsg asyncMsg) {
|
|
|
- this.asyncMsg = asyncMsg;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public RpcResult call() {
|
|
|
- log.info("RPC 调用...");
|
|
|
- return rabbitProducer.callRemote(exchange, asyncMsg.getMachineId(), asyncMsg);
|
|
|
- }
|
|
|
- }
|
|
|
-}
|