分服务器内存

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

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

bootstrap.yml配置
```java
exchange:
  coinex:
    coinlist:
      - USDC_USDT
      - BTC_USDT
```

获取该配置的币种 TradeProperties.java
```java
@Data
@Component
@ConfigurationProperties(prefix = "exchange.coinex")
public class TradeProperties {

    private List<String> coinlist;
}
```

服务启动,加载缓存 TradeDevider.java
```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
```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()实现
```java
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接口
```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));
            }
        }
    }
```


```java

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

}
```

```java
public class CoinBuyCache extends BaseCoin<CoinBuy> {

}

public class CoinSellCache extends BaseCoin<CoinBuy> {

}
```

```java
@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;
    }
}
```

实现类
```java
@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;
    }
}
```


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值