美文网首页
分服务器内存

分服务器内存

作者: 陽_young | 来源:发表于2019-04-29 11:53 被阅读0次

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

SpringBoot实例(币种兑换交易,根据兑换币种的类型区分服务器):

bootstrap.yml配置

exchange:
  coinex:
    coinlist:
      - USDC_USDT
      - BTC_USDT

获取该配置的币种 TradeProperties.java

@Data
@Component
@ConfigurationProperties(prefix = "exchange.coinex")
public class TradeProperties {

    private List<String> coinlist;
}

服务启动,加载缓存 TradeDevider.java

@Component
public class TradeDevider implements AfterSpringLoaded {

    @Autowired
    private TradeProperties properties;

    private static Map<String, CoinTrade> tradeMap = new HashMap<String, CoinTrade>();

    private static Map<String, CoinTrade> tradeMapTen = new HashMap<String, CoinTrade>();

    @Override
    public void load() {
        List<String> 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<String, List<Coinex>> coinexes = impl.getAllByType(getCoinType());
        // 增加内存
        addCacheCoinex(coinexes);
    }

    public default void addCacheCoinex(Map<String, List<Coinex>> 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<T extends Coinex> {
    //缓存 coinexArray
    protected final List<T> coinexArray = new LinkedList<T>();

    /**
      获取缓存
    **/
    public List<T> getCoinex(int size) {
        int coinSize = coinexArray.size();

        List<T> coinexReturn = new ArrayList<T>();
        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<T> coinexAdd) {
        if (CollectionUtil.isNotEmpty(coinexAdd)) {
            for (T coinex : coinexAdd) {
                coinexArray.add(coinex);
            }
            Collections.sort(coinexArray);
        }
    }
    /**
      删除缓存
    **/
    public void removeCoinex(List<T> coinexRemove) {
        if (CollectionUtil.isNotEmpty(coinexRemove)) {
            for (T coinex : coinexRemove) {
                coinexArray.remove(coinex);
            }
            Collections.sort(coinexArray);
        }
    }

}
public class CoinBuyCache extends BaseCoin<CoinBuy> {

}

public class CoinSellCache extends BaseCoin<CoinBuy> {

}
@Data
public class Coinex extends BaseEntity implements Comparable<Coinex> {
......
    /**
    * 类型
    */
    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<CoinexMapper, Coinex> implements CoinexService {

......
@Override
    public int addCache(Coinex coinex) {
        try {
            CoinTrade trade = TradeDevider.getTrade(coinex.getCoinType());
            List<Coinex> 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;
    }
}

相关文章

  • 分服务器内存

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

  • 迁移测试服务 笔记

    需求:分诊算法所使用的模型占用内存较多,当前test服务器的内存不足,需要将服务迁移到新的learn服务器,重新创...

  • EOS内存为什么这么贵?

    大家都知道,电脑的内存很便宜,即使是服务器的内存,也就100元1GB,1KB是0.01分。而EOS的内存价...

  • 服务器卡顿排查方法

    一、查看服务器的内存和服务器核数大小 ①查看服务器内存大小 cat /proc/meminfo | grep Me...

  • sqlserver占用内存过高,清理办法

    SQL Server对服务器内存的使用策略是用多少内存就占用多少内存,只用在服务器内存不足时,才会释放一点占用的内...

  • Jenkins+Gitlab+Java编译部署

    服务器配置 内存建议大于6G (Gitlab需要4G内存) 服务器安装 Jdk Maven Git app 引用...

  • Linux shell-内存篇

    1.以M为单位获取服务器内存free -m 2.以G为单位获取服务器内存free -g

  • Actuator监控

    信息转于 每特教育 余老师 针对微服务服务器的监控,服务器内存变化(堆内存,线程,日志管理等)检测服务器配置连接地...

  • Web服务器内存的选择和作用!

    CPU,硬盘,内存通常是服务器选择最主要的几个标准,其中内存的选择搭配往往是一个难题,通常对于服务器来说,内存有...

  • 内存耗尽后,Redis到底会发生什么?

    前言 作为一台服务器来说,内存并不是无限的,所以总会存在内存耗尽的情况,那么当 Redis 服务器的内存耗尽后,如...

网友评论

      本文标题:分服务器内存

      本文链接:https://www.haomeiwen.com/subject/rezenqtx.html