当前位置: 首页 > news >正文

化工网站建设公司大众网疫情最新消息

化工网站建设公司,大众网疫情最新消息,网站建设会议议程,网站的功能建设方案1. Redis存储设计 我们需要在 Redis 中存储以下信息&#xff1a; 配置列表&#xff08;List<Config>&#xff09;&#xff1a;存储所有配置项。总权重&#xff1a;存储所有配置的总权重。当前轮询状态&#xff1a;存储当前的轮询状态&#xff08;如当前随机值或索引&am…
1. Redis存储设计

我们需要在 Redis 中存储以下信息:

  • 配置列表(List<Config>):存储所有配置项。
  • 总权重:存储所有配置的总权重。
  • 当前轮询状态:存储当前的轮询状态(如当前随机值或索引)。

2. 实现加权轮询

以下是改进后的代码,使用 Redis 来存储和管理状态。

WeightedConfigSelector
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;import java.util.List;
import java.util.Random;@Service
public class WeightedConfigSelector {@Autowiredprivate RedisTemplate<String, Object> redisTemplate;@Autowiredprivate NebulaProperties nebulaProperties;private static final String CONFIG_LIST_KEY = "nebula:config_list";private static final String TOTAL_WEIGHT_KEY = "nebula:total_weight";/*** 初始化配置到 Redis 中*/public void initialize() {List<NebulaProperties.Config> configs = nebulaProperties.getConfig();// 将配置列表存储到 Redis 中  redisTemplate.delete(CONFIG_LIST_KEY); // 清空旧数据  configs.forEach(config -> redisTemplate.opsForList().rightPush(CONFIG_LIST_KEY, config));// 计算总权重并存储  int totalWeight = configs.stream().mapToInt(NebulaProperties.Config::getWeight).sum();redisTemplate.opsForValue().set(TOTAL_WEIGHT_KEY, totalWeight);}/*** 根据权重从 Redis 中选择配置*/public NebulaProperties.Config selectConfig() {if (Boolean.FALSE.equals(redisTemplate.hasKey(CONFIG_LIST_KEY))) {initialize();}// 获取总权重  Integer totalWeight = (Integer) redisTemplate.opsForValue().get(TOTAL_WEIGHT_KEY);if (totalWeight == null || totalWeight == 0) {throw new RuntimeException("Total weight is zero or not initialized.");}// 随机生成一个值  int rand = new Random().nextInt(totalWeight);// 遍历配置列表,根据权重选择配置  List<Object> configList = redisTemplate.opsForList().range(CONFIG_LIST_KEY, 0, -1);if (configList == null || configList.isEmpty()) {throw new RuntimeException("No configs available in Redis.");}for (Object obj : configList) {NebulaProperties.Config config = (NebulaProperties.Config) obj;rand -= config.getWeight();if (rand < 0) {return config;}}return null; // 不应该到达这里  }
}

3. 控制器使用示例

在控制器中调用 WeightedConfigSelectorselectConfig 方法来获取加权选择的配置。

ConfigController
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class ConfigController {@Autowiredprivate WeightedConfigSelector weightedConfigSelector;@GetMapping("/getConfig")public NebulaProperties.Config getConfig() {return weightedConfigSelector.selectConfig();}
}

4. 注意事项

  1. 性能优化
    • 如果配置列表较大,可以将配置列表缓存到本地内存中,并定期从 Redis 同步更新。
  2. 配置变更
    • 如果配置发生变更(如新增或删除配置),需要重新调用 initialize 方法将最新配置同步到 Redis。
  3. 线程安全
    • Redis 的操作是线程安全的,因此可以放心在多线程环境中使用。

5. 总结

通过将配置列表和状态存储在 Redis 中,我们实现了一个支持分布式系统的加权轮询算法。Redis 的高性能和分布式特性确保了多个实例之间的状态一致性,同时 Spring Data Redis 提供了便捷的操作接口,简化了开发流程。

http://www.tj-hxxt.cn/news/112686.html

相关文章:

  • 中央广播电视总台央视少儿客户端seo快速排名优化公司
  • 知乎 php网站开发书籍什么是核心关键词
  • 网站建设的征求意见稿杭州网络推广外包
  • 数据系统网站建设必要性主流网站关键词排名
  • 做非法网站怎样量刑百度指数总结
  • 大良品牌网站建设网上哪里接app推广单
  • 网站建设策划实施要素seo网站快排
  • 小型企业网站的设计与实现seo推广论坛
  • 企业局域网游戏网站如何做限制百度网络营销推广
  • 岳阳商城网站建设营销咨询顾问
  • 橙子建站短信验证码安全吗软文推广渠道
  • 嵊州哪里可以做网站seo研究协会
  • 自己建一个网站需要多少钱?泉州网站建设优化
  • 网站空间多少钱抖音广告投放平台官网
  • 农业电商网站建设pp百度推广开户价格
  • 如何自已建网站推广哪个平台好
  • 网站关键词优化方法百度产品大全首页
  • 35互联网站建设怎么样今日头条军事新闻
  • 网页设计创意书seo排名资源
  • 砀山做网站东莞网络营销优化
  • 政府门户网站 建设原画培训机构哪里好
  • 石家庄建站系统广州百度seo优化排名
  • 吉安网站制作公司优化排名推广教程网站
  • 国外男女直接做的视频网站bt种子磁力搜索
  • 网站甲假发头套真头发做的假发网络流量分析工具
  • wordpress要懂php搜索引擎优化的重要性
  • 做网站实训目的和意义域名解析ip138在线查询
  • 服务器绑定网站打不开餐饮营销方案100例
  • 跨境建站服务公司保定网站建设公司哪家好
  • 新建网站费用2022年大事热点新闻