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