当前位置: 首页 > 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/94358.html

相关文章:

  • 小型的b2c网站seo研究中心南宁线下
  • 找做网站公司需要注意什么谷歌官方网站登录入口
  • 南京专业做网站公司优化百度seo
  • 赣州建网站谷歌seo站内优化
  • 海西高端网站建设公司足球比赛直播2021欧冠决赛
  • 西部数码网站管理助手 数据库sem是什么品牌
  • 郑州企业网站价格网页制作与设计
  • 网站前端改版涉及到的问题网站建设公司哪家好
  • 云速成美站做网站好吗网站seo的内容是什么
  • 日本最新新闻新手seo入门教程
  • ui参考网站长沙seo
  • 网站 默认首页天津网站建设开发
  • 网站建设0基础学起济南seo整站优化厂家
  • 营销型网站的推广站长工具百度
  • 中国建筑招聘2022网站权重优化
  • 建设造价信息网站seo免费课程视频
  • 设计网站建设书南昌石家庄seo报价
  • 网店推广计划怎么写免费的seo快排公司哪家好
  • 计算机培训中心网站活动推广软文范例
  • 网站建设费用 业务宣传费山西网站seo
  • 企业内部系统网站制作百度快照关键词推广
  • 做跨境电商有没推荐的网站论文收录网站
  • 威客网站建设指数函数公式
  • wordpress后台数据库改密码林云seo博客
  • 国际专线网络怎么申请seo 百度网盘
  • 网站更换服务器怎么做独立站怎么建站
  • 网站开发用什么软件有哪些百度在全国有哪些代理商
  • 电商网站运营策划厦门人才网最新招聘信息网
  • WordPress局域网开通嘉兴优化公司
  • 网站后台管理页面下载东莞seo网站管理