分服务器内存

系统需要处理相同逻辑但某种类型不同业务.比如商品,根据不同的分类,加载缓存到不同的服务器,一个服务器只处理某个分类的商品,从而给服务器减轻压力.

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;
    }
}

你可能感兴趣的:(分服务器内存)