서버별 메모리

7714 단어
시스템은 같은 논리를 처리해야 하지만 어떤 유형의 다른 업무를 처리해야 한다.예를 들어 상품은 서로 다른 분류에 따라 서로 다른 서버에 캐시를 불러오고 한 서버는 특정한 분류의 상품만 처리하여 서버에 압력을 줄인다.

SpringBoot 인스턴스(화폐 교환 거래, 화폐 교환 유형에 따라 서버 구분):


bootstrap.yml 설정
exchange:
  coinex:
    coinlist:
      - USDC_USDT
      - BTC_USDT

이 설정된 화폐의 TradeProperties를 가져옵니다.java
@Data
@Component
@ConfigurationProperties(prefix = "exchange.coinex")
public class TradeProperties {

    private List coinlist;
}

서비스 시작, 캐시 TradeDevider를 로드합니다.java
@Component
public class TradeDevider implements AfterSpringLoaded {

    @Autowired
    private TradeProperties properties;

    private static Map tradeMap = new HashMap();

    private static Map tradeMapTen = new HashMap();

    @Override
    public void load() {
        List supportCoins = properties.getCoinlist();
        for (String support : supportCoins) {
            // 
            if (SupportTradeCoin.isSupport(support)) {
                CoinTrade trade = new CoinTrade(support);
                trade.initWhenSpringloaded();
                tradeMap.put(support, trade);
            }
        }
    }

    public static CoinTrade getTrade(String tradeCoin) {
        CoinTrade trade = tradeMap.get(tradeCoin);
        if (trade != null) {
            return trade;
        }
        throw new LFException(ExchangeCode.exchange_notsupport_coin);
    }
}

화폐 종류가 SupportTradeCoin을 지원합니까?java
public enum SupportTradeCoin {

    USDC_USDT,
    BTC_USDT,
    EOS_USDT,
    ETH_USDT;

    public static boolean isSupport(String supportCoin) {
        try {
            SupportTradeCoin.valueOf(supportCoin);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

}

CoinTrade.java의 initWhenSpringloaded() 구현
public class CoinTrade implements BuyAndSellCacheOperateInterface {

    final CoinBuyCache baseCoinBuy = new CoinBuyCache();

    final CoinSellCache baseCoinSell = new CoinSellCache();


    @Override
    public BaseCoin getBaseCoinBuy() {
        return baseCoinBuy;
    }

    @Override
    public BaseCoin getBaseCoinSell() {
        return baseCoinSell;
    }
}

BuyAndSellCacheOperateInterface.java 인터페이스
public interface BuyAndSellCacheOperateInterface {
    public BaseCoin getBaseCoinBuy();

    public BaseCoin getBaseCoinSell();

   public String getCoinType();


    public default void initWhenSpringloaded() {
        CoinexServiceImpl impl = (CoinexServiceImpl) SpringApplicationContext.getBean(CoinexServiceImpl.class);
        // 
        Map> coinexes = impl.getAllByType(getCoinType());
        //  
        addCacheCoinex(coinexes);
    }

    public default void addCacheCoinex(Map> coinexesMap) {
        synchronized (this) {
            if (null != coinexesMap && coinexesMap.size() > 0) {
                getBaseCoinBuy().addCoinex(coinexesMap.get(ExchangeConstant.COINEX_TYPE.BUY));
                getBaseCoinSell().addCoinex(coinexesMap.get(ExchangeConstant.COINEX_TYPE.SELL));
            }
        }
    }

public abstract class BaseCoin {
    //  coinexArray
    protected final List coinexArray = new LinkedList();

    /**
       
    **/
    public List getCoinex(int size) {
        int coinSize = coinexArray.size();

        List coinexReturn = new ArrayList();
        if (coinSize == 0) {
            return coinexReturn;
        }
        if (coinSize < size) {
            size = coinSize;
        }
        for (int i = 0; i < size; i++) {
            coinexReturn.add(coinexArray.get(i));
        }
        return coinexReturn;
    }
    /**
       
    **/
    public void addCoinex(List coinexAdd) {
        if (CollectionUtil.isNotEmpty(coinexAdd)) {
            for (T coinex : coinexAdd) {
                coinexArray.add(coinex);
            }
            Collections.sort(coinexArray);
        }
    }
    /**
       
    **/
    public void removeCoinex(List coinexRemove) {
        if (CollectionUtil.isNotEmpty(coinexRemove)) {
            for (T coinex : coinexRemove) {
                coinexArray.remove(coinex);
            }
            Collections.sort(coinexArray);
        }
    }

}
public class CoinBuyCache extends BaseCoin {

}

public class CoinSellCache extends BaseCoin {

}
@Data
public class Coinex extends BaseEntity implements Comparable {
......
    /**
    *  
    */
    private String type;

   /**
    *  
    */
    private BigDecimal entrustPrice;

    /**
     *  
     */
    private Date createTime;
......

    @Override
    public int compareTo(Coinex o) {
        if (ExchangeConstant.COINEX_TYPE.BUY.equals(o.getType())) {
            //   ,  
            if (this.getEntrustPrice().compareTo(o.getEntrustPrice()) > 0) {
                return -1;
            } else if (this.getEntrustPrice().compareTo(o.getEntrustPrice()) < 0) {
                return 1;
            } else {
                return this.getCreateTime().compareTo(o.getCreateTime());
            }
        }
        if (ExchangeConstant.COINEX_TYPE.SELL.equals(o.getType())) {
            // , ,  
            if (this.getEntrustPrice().compareTo(o.getEntrustPrice()) < 0) {
                return -1;
            } else if (this.getEntrustPrice().compareTo(o.getEntrustPrice()) > 0) {
                return 1;
            } else {
                return this.getCreateTime().compareTo(o.getCreateTime());
            }
        }

        return 0;
    }
}

public class CoinBuy extends Coinex {

    @Override
    public int compareTo(Coinex o) {
        return 0;
    }
}

public class CoinSell extends Coinex {

    @Override
    public int compareTo(Coinex o) {
       return 0;
    }
}

실현류
@Service
@Slf4j
public class CoinexServiceImpl extends ServiceImpl implements CoinexService {

......
@Override
    public int addCache(Coinex coinex) {
        try {
            CoinTrade trade = TradeDevider.getTrade(coinex.getCoinType());
            List coinexList = new ArrayList<>();
            coinexList.add(coinex);
            if (ExchangeConstant.COINEX_TYPE.BUY.equals(coinex.getType())) {
                trade.getBaseCoinBuy().addCoinex(coinexList);
            }
            if (ExchangeConstant.COINEX_TYPE.SELL.equals(coinex.getType())) {
                trade.getBaseCoinSell().addCoinex(coinexList);
            }
        } catch (LFException e) {
            // , 
            if (e.getCode().equals(ExchangeCode.exchange_notsupport_coin.getCode())) {
                String mqEvent = ExchangeConstant.MQEvent.LOADADDCOIN;
                MQMessage message = new MQMessage(mqEvent, coinex);
                RabbitService rabbitService = (RabbitService) SpringApplicationContext.getBean(RabbitService.class);
                rabbitService.send(Excharge.brodcast, "plat-exchange", message);
                log.warn(" :  , ", e);
                return 1;
            } else {
                log.warn(e.getMessage(), e);
            }
        } catch (Exception e) {
            log.error(" ", e);
            return 0;
        }
        return 1;
    }
}

좋은 웹페이지 즐겨찾기