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

网站后台发布了但看不见南宁seo网络推广

网站后台发布了但看不见,南宁seo网络推广,杭州互联网企业有哪些,重庆网站推广多少钱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/73357.html

相关文章:

  • 怎么做网站的推广杭州网站seo
  • 五金企业网站模板企业seo网络营销
  • 做网站的公司好坑啊网站建设排名优化
  • 网站支付页面怎么做百度一下的网址
  • 西安企业模板建站最常用的搜索引擎有哪些
  • 碧辉腾乐 网站建设营销策略手段有哪些
  • 企业网站备案材料2024年新闻摘抄
  • phpcms v9怎么做网站深圳seo优化排名推广
  • 做网站需要什么执照百度云网盘资源分享网站
  • 芜湖做网站的客户百度搜索下载安装
  • 做网站的域名武汉seo技术
  • 医院seo是什么莆田百度seo公司
  • 如何把网站程序做授权网址访问朝阳seo排名
  • 网站开发ipv6升级seo的中文含义是什么
  • 可以做黄金期权的网站重庆关键词优化服务
  • 网站伪静态好还是静态好今日头条新闻最新
  • 什么是网络营销调研?它有什么特点?seo平台怎么样
  • 手机做ppt的免费模板下载网站苏州做网站的专业公司
  • 山西太原网站建设公司哪家好网络营销理论基础
  • 寿县网站建设深圳网站建设公司
  • 北京人民政府门户网站西安seo高手
  • 铁岭做网站信息百度指数数据分析平台
  • 网站开发是固定资产吗营销推广运营
  • 阿里云做淘宝客网站短视频培训机构排名
  • 网站注册页面跳出怎么做网络营销专业是干嘛的
  • 微信做一元云购网站win10优化大师有用吗
  • 网站排名英文电脑清理软件十大排名
  • 从零学做网站seo专员工作容易学吗
  • 小程序开发平台需要网站吗成都营销型网站制作
  • 政府网站意义网络营销推广计划