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

Redis - 多集群数据源配置

目录

  • 前言
  • 依赖
  • yml配置
  • redis多集群数据源配置类
    • 思考
  • redis工具类

前言

工作时有一个项目配置了多个redis数据源,使用时出现了指定了使用副数据源,数据却依然使用了主数据源的情况。经过排查,发现配置流程较为繁琐易错,此处做一个记录。

依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-redis</artifactId><version>1.4.1.RELEASE</version>
</dependency>

yml配置

此处设置两个不同的redis地址,可以debug时,在redisTemplate对象中查看此时使用的到底是哪一个数据源,方便排查问题。

spring:redis:jedis:pool:maxActive: 1000minIdle: 1maxWait: 5000maxIdle: 5timeout: 6000msredis-one: # 第一个redis(主)集群配置cluster:node: localhost:6379password: 123456redis-two: # 第二个redis(其他)集群配置cluster:node: 127.0.0.1:6379password: 123456

redis多集群数据源配置类

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;@Configuration
@EnableCaching
@Slf4j
public class RedisDataSourceConfig extends CachingConfigurerSupport {@Resourceprivate Environment environment;@Resourceprivate RedisProperties redisProperties;/*** 主业务redis操作模板** @param factoryOne* @return*/@Bean(name = "redisOneTemplate")@Primarypublic RedisTemplate<String, Object> redisOneTemplate(@Autowired @Qualifier("factoryOne") JedisConnectionFactory factoryOne) {return getRedisTemplate(factoryOne);}/*** 副redis操作模板* 注意:初始化配置有误,导致实际查询的还是第一个redis(redis-one)配置* @param factoryTwo* @return*/@Bean(name = "redisTwoTemplate")public RedisTemplate<String, Object> redisTwoTemplate(@Autowired @Qualifier("factoryTwo") JedisConnectionFactory factoryTwo) {return getRedisTemplate(factoryTwo);}/*** 副redis操作模板(真)* 真实指向 redis-two配置* @param factoryTwoReal* @return*/@Bean(name = "redisTwoRealTemplate")public RedisTemplate<String, Object> redisTwoRealTemplate(@Autowired @Qualifier("factoryTwoReal") JedisConnectionFactory factoryTwoReal) {return getRedisTemplate(factoryTwoReal);}/*============================  集群模式配置 start  ===========================*//*** 指向redis-one配置*/@Bean("factoryOne")@Primarypublic JedisConnectionFactory factoryOne(RedisStandaloneConfiguration redisConfigOne, JedisClientConfiguration clientConfig) {return new JedisConnectionFactory(redisConfigOne, clientConfig);}/*** 真实指向redis-one配置*/@Bean("factoryTwo")public JedisConnectionFactory factoryTwo(RedisStandaloneConfiguration redisConfigTwo, JedisClientConfiguration clientConfig) {return new JedisConnectionFactory(redisConfigTwo, clientConfig);}/*** 真实指向redis-two配置*/@Bean("factoryTwoReal")public JedisConnectionFactory factoryTwoReal(@Autowired @Qualifier("redisConfigTwo") RedisStandaloneConfiguration redisConfigTwo, JedisClientConfiguration clientConfig) {return new JedisConnectionFactory(redisConfigTwo, clientConfig);}// 读取第一个redis配置@Primary	@Bean("redisConfigOne")public RedisStandaloneConfiguration redisConfigOne() {String nodeStrAry = environment.getProperty("spring.redis.redis-one.cluster.nodes");String password = environment.getProperty("spring.redis.redis-one.password");assert nodeStrAry != null;assert password != null;RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();String[] hostPortAry = nodeStrAry.split(":");redisStandaloneConfiguration.setHostName(hostPortAry[0]);redisStandaloneConfiguration.setPort(Integer.parseInt(hostPortAry[1]));redisStandaloneConfiguration.setPassword(password);return redisStandaloneConfiguration;}// 读取第二个redis配置@Bean("redisConfigTwo")public RedisStandaloneConfiguration redisConfigTwo() {String nodeStrAry = environment.getProperty("spring.redis.redis-two.cluster.nodes");String password = environment.getProperty("spring.redis.redis-two.password");assert nodeStrAry != null;assert password != null;RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();String[] hostPortAry = nodeStrAry.split(":");redisStandaloneConfiguration.setHostName(hostPortAry[0]);redisStandaloneConfiguration.setPort(Integer.parseInt(hostPortAry[1]));redisStandaloneConfiguration.setPassword(password);return redisStandaloneConfiguration;}/*================================集群模式 end===============================*/@Bean("clientConfig")public JedisClientConfiguration jedisClientConfiguration() {JedisPoolConfig poolConfig = new JedisPoolConfig();poolConfig.setMaxIdle(redisProperties.getJedis().getPool().getMaxIdle());poolConfig.setMinIdle(redisProperties.getJedis().getPool().getMinIdle());poolConfig.setMaxTotal(redisProperties.getJedis().getPool().getMaxActive());poolConfig.setMaxWaitMillis(redisProperties.getJedis().getPool().getMaxWait().toMillis());return JedisClientConfiguration.builder().connectTimeout(redisProperties.getTimeout()).usePooling().poolConfig(poolConfig).build();}private RedisClusterConfiguration getRedisClusterConfiguration(String nodeStrAry, String password) {RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();String[] serverArray = nodeStrAry.split(",");Set<RedisNode> nodes = new HashSet<>();for (String ipPort : serverArray) {String[] ipAndPort = ipPort.split(":");nodes.add(new RedisNode(ipAndPort[0].trim(), Integer.parseInt(ipAndPort[1])));}redisClusterConfiguration.setClusterNodes(nodes);redisClusterConfiguration.setPassword(password);return redisClusterConfiguration;}private RedisTemplate<String, Object> getRedisTemplate(JedisConnectionFactory factory) {RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setValueSerializer(jackson2JsonRedisSerializer());redisTemplate.setHashKeySerializer(new StringRedisSerializer());redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer());redisTemplate.setConnectionFactory(factory);return redisTemplate;}/*** json序列化** @return*/@Beanpublic RedisSerializer<Object> jackson2JsonRedisSerializer() {Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<Object>(Object.class);ObjectMapper mapper = new ObjectMapper();mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);serializer.setObjectMapper(mapper);return serializer;}}

注解:
@Primary :优先考虑注入的类
@Qualifier :通过控制里面的字符串来匹配类上的字符串达到控制注入的效果。

思考

factoryTwo方法与factoryTwoReal方法看起来都调用了redis-two的配置方法,为什么factoryTwo实际调用的还是redis-one的配置呢?

答:区别就在于入参@Autowired @Qualifier(“redisConfigTwo”) RedisStandaloneConfiguration redisConfigTwo,factoryTwo方法未使用@Qualifier(“redisConfigTwo”)指定redis-two的配置方法,实际上注入的是标记了@Primary的redis-one配置方法。

redis工具类

为主数据源与副数据源分别创建工具类或直接使用注解指定当前使用的数据源

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;/***  Redis主数据源工具类*/
@Component
public class RedisOneUtils {// 指向redis-one@Resource(name = "redisOneTemplate")private RedisTemplate<String, Object> redisTemplate;/*** 指定缓存失效时间** @param key  键* @param time 时间(秒)* @return*/public boolean expire(String key, long time) {try {if (time > 0) {redisTemplate.expire(key, time, TimeUnit.SECONDS);}return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 根据key 获取过期时间** @param key 键 不能为null* @return 时间(秒) 返回0代表为永久有效*/public long getExpire(String key) {return redisTemplate.getExpire(key, TimeUnit.SECONDS);}/*** 判断key是否存在** @param key 键* @return true 存在 false不存在*/public boolean hasKey(String key) {try {return redisTemplate.hasKey(key);} catch (Exception e) {e.printStackTrace();return false;}}/*** 删除缓存** @param key 可以传一个值 或多个*/@SuppressWarnings("unchecked")public void del(String... key) {if (key != null && key.length > 0) {if (key.length == 1) {redisTemplate.delete(key[0]);} else {redisTemplate.delete(CollectionUtils.arrayToList(key));}}}//============================String=============================/*** 普通缓存获取** @param key 键* @return 值*/public Object get(String key) {return key == null ? null : redisTemplate.opsForValue().get(key);}/*** 普通缓存放入** @param key   键* @param value 值* @return true成功 false失败*/public boolean set(String key, Object value) {try {redisTemplate.opsForValue().set(key, value);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 普通缓存放入并设置时间** @param key   键* @param value 值* @param time  时间(秒) time要大于0 如果time小于等于0 将设置无限期* @return true成功 false 失败*/public boolean set(String key, Object value, long time) {try {if (time > 0) {redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);} else {set(key, value);}return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 递增** @param key   键* @param delta 要增加几(大于0)* @return*/public long incr(String key, long delta) {if (delta < 0) {throw new RuntimeException("递增因子必须大于0");}return redisTemplate.opsForValue().increment(key, delta);}/*** 递减** @param key   键* @param delta 要减少几(小于0)* @return*/public long decr(String key, long delta) {if (delta < 0) {throw new RuntimeException("递减因子必须大于0");}return redisTemplate.opsForValue().increment(key, -delta);}//================================Map=================================/*** HashGet** @param key  键 不能为null* @param item 项 不能为null* @return 值*/public Object hget(String key, String item) {return redisTemplate.opsForHash().get(key, item);}/*** 获取hashKey对应的所有键值** @param key 键* @return 对应的多个键值*/public Map<Object, Object> hmget(String key) {return redisTemplate.opsForHash().entries(key);}/*** HashSet** @param key 键* @param map 对应多个键值* @return true 成功 false 失败*/public boolean hmset(String key, Map<String, Object> map) {try {redisTemplate.opsForHash().putAll(key, map);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** HashSet 并设置时间** @param key  键* @param map  对应多个键值* @param time 时间(秒)* @return true成功 false失败*/public boolean hmset(String key, Map<String, Object> map, long time) {try {redisTemplate.opsForHash().putAll(key, map);if (time > 0) {expire(key, time);}return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 向一张hash表中放入数据,如果不存在将创建** @param key   键* @param item  项* @param value 值* @return true 成功 false失败*/public boolean hset(String key, String item, Object value) {try {redisTemplate.opsForHash().put(key, item, value);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 向一张hash表中放入数据,如果不存在将创建** @param key   键* @param item  项* @param value 值* @param time  时间(秒)  注意:如果已存在的hash表有时间,这里将会替换原有的时间* @return true 成功 false失败*/public boolean hset(String key, String item, Object value, long time) {try {redisTemplate.opsForHash().put(key, item, value);if (time > 0) {expire(key, time);}return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 删除hash表中的值** @param key  键 不能为null* @param item 项 可以使多个 不能为null*/public void hdel(String key, Object... item) {redisTemplate.opsForHash().delete(key, item);}/*** 判断hash表中是否有该项的值** @param key  键 不能为null* @param item 项 不能为null* @return true 存在 false不存在*/public boolean hHasKey(String key, String item) {return redisTemplate.opsForHash().hasKey(key, item);}/*** hash递增 如果不存在,就会创建一个 并把新增后的值返回** @param key  键* @param item 项* @param by   要增加几(大于0)* @return*/public double hincr(String key, String item, double by) {return redisTemplate.opsForHash().increment(key, item, by);}/*** hash递减** @param key  键* @param item 项* @param by   要减少记(小于0)* @return*/public double hdecr(String key, String item, double by) {return redisTemplate.opsForHash().increment(key, item, -by);}//============================set=============================/*** 根据key获取Set中的所有值** @param key 键* @return*/public Set<Object> sGet(String key) {try {return redisTemplate.opsForSet().members(key);} catch (Exception e) {e.printStackTrace();return null;}}/*** 根据value从一个set中查询,是否存在** @param key   键* @param value 值* @return true 存在 false不存在*/public boolean sHasKey(String key, Object value) {try {return redisTemplate.opsForSet().isMember(key, value);} catch (Exception e) {e.printStackTrace();return false;}}/*** 将数据放入set缓存** @param key    键* @param values 值 可以是多个* @return 成功个数*/public long sSet(String key, Object... values) {try {return redisTemplate.opsForSet().add(key, values);} catch (Exception e) {e.printStackTrace();return 0;}}/*** 将set数据放入缓存** @param key    键* @param time   时间(秒)* @param values 值 可以是多个* @return 成功个数*/public long sSetAndTime(String key, long time, Object... values) {try {Long count = redisTemplate.opsForSet().add(key, values);if (time > 0) {expire(key, time);}return count;} catch (Exception e) {e.printStackTrace();return 0;}}/*** 获取set缓存的长度** @param key 键* @return*/public long sGetSetSize(String key) {try {return redisTemplate.opsForSet().size(key);} catch (Exception e) {e.printStackTrace();return 0;}}/*** 移除值为value的** @param key    键* @param values 值 可以是多个* @return 移除的个数*/public long setRemove(String key, Object... values) {try {Long count = redisTemplate.opsForSet().remove(key, values);return count;} catch (Exception e) {e.printStackTrace();return 0;}}//===============================list=================================/*** 获取list缓存的内容** @param key   键* @param start 开始* @param end   结束  0 到 -1代表所有值* @return*/public List<Object> lGet(String key, long start, long end) {try {return redisTemplate.opsForList().range(key, start, end);} catch (Exception e) {e.printStackTrace();return null;}}/*** 获取list缓存的长度** @param key 键* @return*/public long lGetListSize(String key) {try {return redisTemplate.opsForList().size(key);} catch (Exception e) {e.printStackTrace();return 0;}}/*** 通过索引 获取list中的值** @param key   键* @param index 索引  index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推* @return*/public Object lGetIndex(String key, long index) {try {return redisTemplate.opsForList().index(key, index);} catch (Exception e) {e.printStackTrace();return null;}}/*** 将list放入缓存** @param key   键* @param value 值* @return*/public boolean lSet(String key, Object value) {try {redisTemplate.opsForList().rightPush(key, value);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 将list放入缓存** @param key   键* @param value 值* @param time  时间(秒)* @return*/public boolean lSet(String key, Object value, long time) {try {redisTemplate.opsForList().rightPush(key, value);if (time > 0) {expire(key, time);}return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 将list放入缓存** @param key   键* @param value 值* @return*/public boolean lSet(String key, List<Object> value) {try {redisTemplate.opsForList().rightPushAll(key, value);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 将list放入缓存** @param key   键* @param value 值* @param time  时间(秒)* @return*/public boolean lSet(String key, List<Object> value, long time) {try {redisTemplate.opsForList().rightPushAll(key, value);if (time > 0) {expire(key, time);}return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 根据索引修改list中的某条数据** @param key   键* @param index 索引* @param value 值* @return*/public boolean lUpdateIndex(String key, long index, Object value) {try {redisTemplate.opsForList().set(key, index, value);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 移除N个值为value** @param key   键* @param count 移除多少个* @param value 值* @return 移除的个数*/public long lRemove(String key, long count, Object value) {try {Long remove = redisTemplate.opsForList().remove(key, count, value);return remove;} catch (Exception e) {e.printStackTrace();return 0;}}/*** 模糊查询获取key值** @param pattern* @return*/public Set keys(String pattern) {return redisTemplate.keys(pattern);}/*** 使用Redis的消息队列** @param channel* @param message 消息内容*/public void convertAndSend(String channel, Object message) {redisTemplate.convertAndSend(channel, message);}/*** 根据起始结束序号遍历Redis中的list** @param listKey* @param start   起始序号* @param end     结束序号* @return*/public List<Object> rangeList(String listKey, long start, long end) {//绑定操作BoundListOperations<String, Object> boundValueOperations = redisTemplate.boundListOps(listKey);//查询数据return boundValueOperations.range(start, end);}/*** 弹出右边的值 --- 并且移除这个值** @param listKey*/public Object rifhtPop(String listKey) {//绑定操作BoundListOperations<String, Object> boundValueOperations = redisTemplate.boundListOps(listKey);return boundValueOperations.rightPop();}/*** 有序集合添加** @param key* @param value* @param scoure*/public boolean zAdd(String key, Object value, double scoure) {ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();return zset.add(key, value, scoure);}/*** 移除集合中时间过期的** @param key* @return*/public boolean removeSet(String key, long max) {try {long count = redisTemplate.opsForZSet().removeRangeByScore(key, 0, max);System.out.println("count===>" + count);if (count > 0) {return true;}} catch (Exception e) {return false;}return false;}/*** 移除集合中某个成员** @param key* @param value* @return*/public Boolean delSetNum(String key, String value) {try {long cnt = redisTemplate.opsForZSet().remove(key, value);if (cnt > 0) {return true;}} catch (Exception e) {return false;}return false;}public <T> boolean setString(String key, T value) {try {//任意类型转换成StringString val = beanToString(value);if (val == null || val.length() <= 0) {return false;}redisTemplate.opsForValue().set(key, val);return true;} catch (Exception e) {return false;}}public <T> boolean setStringTime(String key, T value, long timeout) {try {//任意类型转换成StringString val = beanToString(value);if (val == null || val.length() <= 0) {return false;}redisTemplate.opsForValue().set(key, val, timeout, TimeUnit.MILLISECONDS);return true;} catch (Exception e) {return false;}}public <T> T get(String key, Class<T> clazz) {try {Object object = redisTemplate.opsForValue().get(key);if (object == null) {return null;}String value = String.valueOf(object);return stringToBean(value, clazz);} catch (Exception e) {return null;}}public <T> boolean delete(String key) {try {if (key == null || key.length() <= 0) {return false;}redisTemplate.delete(key);return true;} catch (Exception e) {return false;}}public boolean exists(String key) {try {return redisTemplate.hasKey(key);} catch (Exception e) {return false;}}@SuppressWarnings("unchecked")private <T> T stringToBean(String value, Class<T> clazz) {if (value == null || value.length() <= 0 || clazz == null) {return null;}if (clazz == int.class || clazz == Integer.class) {return (T) Integer.valueOf(value);} else if (clazz == long.class || clazz == Long.class) {return (T) Long.valueOf(value);} else if (clazz == String.class) {return (T) value;} else {return JSON.toJavaObject(JSON.parseObject(value), clazz);}}/*** @param value 任意类型* @return String*/private <T> String beanToString(T value) {if (value == null) {return null;}Class<?> clazz = value.getClass();if (clazz == int.class || clazz == Integer.class) {return "" + value;} else if (clazz == long.class || clazz == Long.class) {return "" + value;} else if (clazz == String.class) {return (String) value;} else {return JSON.toJSONString(value);}}//=========BoundListOperations 用法 End============}

相关文章:

Redis - 多集群数据源配置

目录 前言依赖yml配置redis多集群数据源配置类思考 redis工具类 前言 工作时有一个项目配置了多个redis数据源&#xff0c;使用时出现了指定了使用副数据源&#xff0c;数据却依然使用了主数据源的情况。经过排查&#xff0c;发现配置流程较为繁琐易错&#xff0c;此处做一个记…...

五大架构风格之四-虚拟机架构风格

虚拟机架构风格&#xff1a; 虚拟机架构风格是一种软件架构&#xff0c;它通过模拟完整的计算机系统&#xff08;包括硬件&#xff09;来运行程序。这种风格的核心是虚拟机监控器。如最出名的虚拟机VM&#xff0c;在使用虚拟机架构&#xff0c;一个或多个虚拟机可以在单一物理主…...

在 C# 中 checked 和 unchecked 关键字

在 C# 中&#xff0c;checked 和 unchecked 是用于控制整数运算溢出检查的关键字。它们允许我们明确指定在进行整数运算时是否要检查溢出&#xff0c;以及如何处理溢出情况。 默认情况下&#xff0c;C# 中的整数运算是未检查的&#xff0c;也就是说&#xff0c;当运算结果溢出…...

【算法分析与设计】跳跃游戏

&#x1f4dd;个人主页&#xff1a;五敷有你 &#x1f525;系列专栏&#xff1a;算法分析与设计 ⛺️稳中求进&#xff0c;晒太阳 题目 给你一个非负整数数组 nums &#xff0c;你最初位于数组的 第一个下标 。数组中的每个元素代表你在该位置可以跳跃的最大长度。 判断…...

openssl3.2 - helpdoc - P12证书操作

文章目录 openssl3.2 - helpdoc - P12证书操作概述笔记/doc/html/man1/CA.pl.htmlCA.pl -newcaCA.pl -newreqCA.pl -signCA.pl -pkcs12 "My Test Certificate"/doc/html/man1/openssl-pkcs12.html备注END openssl3.2 - helpdoc - P12证书操作 概述 D:\3rd_prj\cryp…...

【产业实践】使用YOLO V5 训练自有数据集,并且在C# Winform上通过onnx模块进行预测全流程打通

使用YOLO V5 训练自有数据集,并且在C# Winform上通过onnx模块进行预测全流程打通 效果图 背景介绍 当谈到目标检测算法时,YOLO(You Only Look Once)系列算法是一个备受关注的领域。YOLO通过将目标检测任务转化为一个回归问题,实现了快速且准确的目标检测。以下是YOLO的基…...

【操作系统】HeapByteBuffer和DirectByteBuffer的区别

DirectByteBuffer和HeapByteBuffer是Java NIO中ByteBuffer的两种实现方式。 HeapByteBuffer是在Java堆上分配的字节缓冲区&#xff0c;它使用数组来存储数据。HeapByteBuffer的优点是它具有良好的兼容性和可移植性&#xff0c;且在大多数情况下性能表现良好。它适用于大部分的…...

C++并发编程 -2.线程间共享数据

本章就以在C中进行安全的数据共享为主题。避免上述及其他潜在问题的发生的同时&#xff0c;将共享数据的优势发挥到最大。 一. 锁分类和使用 按照用途分为互斥、递归、读写、自旋、条件变量。本章节着重介绍前四种&#xff0c;条件变量后续章节单独介绍。 由于锁无法进行拷贝…...

Kubernetes-资源清单

一、k8s中的资源 什么是资源清单 我们跟kubernetes集群进行交互的时候&#xff0c;我们需要给K8S集群传输数据&#xff0c;传输信息&#xff0c;K8S才能按照我们的要求来运行&#xff0c;这个传输的文件&#xff0c;基本上都会通过资源清单进行传递。资源清单是我们跟集群进行…...

ABAP 笔记--内表结构不一致,无法更新数据库MODIFY和UPDATE

目录 ABAP 笔记内表结构不一致&#xff0c;无法更新数据库MODIFY和UPDATE ABAP 笔记 内表结构不一致&#xff0c;无法更新数据库 MODIFY和UPDATE 如果是使用MODIFY或者UPDATE...

机器学习-3降低损失(Reducing Loss)

机器学习-3降低损失(Reducing Loss) 学习内容来自&#xff1a;谷歌ai学习 https://developers.google.cn/machine-learning/crash-course/framing/check-your-understanding?hlzh-cn 本文作为学习记录1.降低损失&#xff1a;迭代方法 迭代学习 下图展示了机器学习算法用于训…...

蓝桥杯备战(AcWing算法基础课)-高精度-减-高精度

目录 前言 1 题目描述 2 分析 2.1 第一步 2.2 第二步 3 代码 前言 详细的代码里面有自己的理解注释 1 题目描述 给定两个正整数&#xff08;不含前导 00&#xff09;&#xff0c;计算它们的差&#xff0c;计算结果可能为负数。 输入格式 共两行&#xff0c;每行包含一…...

AspNet web api 和mvc 过滤器差异

最近在维护老项目。定义个拦截器记录接口日志。但是发现不生效 最后发现因为继承的 ApiController不是Controller 只能用 System.Web.Http下的拦截器生效。所以现在总结归纳一下 Web Api: System.Web.Http.Filters.ActionFilterAttribute 继承该类 Mvc: System.Web.Mvc.Ac…...

HarmonyOS应用/服务发布:打造多设备生态的关键一步

目前 前言HarmonyOS 应用/服务发布的重要性使用HarmonyOS 构建跨设备的应用生态前期准备工作简述发布流程生成签名文件配置签名信息编译构建.app文件上架.app文件到AGC结束语 前言 随着智能设备的快速普及和多样化&#xff0c;以及编程语言的迅猛发展&#xff0c;构建一个无缝…...

【数据结构】双向带头循环链表实现及总结

简单不先于复杂&#xff0c;而是在复杂之后。 文章目录 1. 双向带头循环链表的实现2. 顺序表和链表的区别 1. 双向带头循环链表的实现 List.h #pragma once #include <stdio.h> #include <assert.h> #include <stdlib.h> #include <stdbool.h>typede…...

创建自己的Hexo博客

目录 一、Github新建仓库二、支持环境安装Git安装Node.js安装Hexo安装 三、博客本地运行本地hexo文件初始化本地启动Hexo服务 四、博客与Github绑定建立SSH密钥&#xff0c;并将公钥配置到github配置Hexo与Github的联系检查github链接访问hexo生成的博客 一、Github新建仓库 登…...

音箱、功放播放HDMI音频解决方案之HDMI音频分离器HHA

HDMI音频分离器HHA简介 HDMI音频分离器HHA具有一路HDMI信号输入&#xff0c;转换成一路HDMI信号、一路5.1光纤音频信号、一路5.1 SPDIF/同轴音频信号和一路模拟左右声道立体声信号输出&#xff0c;同时还支持EDID存储及兼容HDCP功能&#xff1b;分辨率最高支持1920*1080p&#…...

天猫数据分析:2023年坚果炒货市场年销额超71亿,混合坚果成多数消费者首选

近年来&#xff0c;随着人们生活水平和健康意识的提升&#xff0c;在休闲零食市场中&#xff0c;消费者们也越来越关注食品的营养价值&#xff0c;消费者这一消费偏好的转变也为坚果炒货食品行业带来了发展契机。 整体来看&#xff0c;坚果炒货市场的体量较大。根据鲸参谋电商…...

YouTrack 用户登录提示 JIRA 错误

就算输入正确的用户名和密码&#xff0c;我们也得到了下面的错误信息&#xff1a; youtrack Cannot retrieve JIRA user profile details. 解决办法 出现这个问题是因为 YouTrack 在当前的系统重有 JIRA 的导入关联。 需要把这个导入关联取消掉。 找到后台配置的导入关联&a…...

题目 1163: 排队买票

题目描述: 有M个小孩到公园玩&#xff0c;门票是1元。其中N个小孩带的钱为1元&#xff0c;K个小孩带的钱为2元。售票员没有零钱&#xff0c;问这些小孩共有多少种排队方法&#xff0c;使得售票员总能找得开零钱。注意&#xff1a;两个拿一元零钱的小孩&#xff0c;他们的位置互…...

【计算机网络】子网划分

文章目录 【计算机网络】子网划分&#xff08;知识点详细&#xff09;一、子网划分基础概念1. **为什么需要子网划分&#xff1f;**2. **关键术语** 二、子网划分核心原理1. **借位规则**2. **子网划分步骤** 三、子网划分实战案例案例1&#xff1a;标准C类网划分&#xff08;等…...

Redis7底层数据结构解析

redisObject 在 Redis 的源码中&#xff0c;Redis 会将底层数据结构&#xff08;如 SDS、hash table、skiplist 等&#xff09;统一封装成一个对象&#xff0c;这个对象叫做 redisObject&#xff0c;也简称 robj。 typedef struct redisObject {unsigned type : 4; // 数…...

大语言模型值ollama使用(1)

ollama为本地调用大语言模型提供了便捷的方式。下面列举如何在windows系统中快捷调用ollama。 winR打开运行框&#xff0c;输入cmd 1、输入ollama list 显示已下载模型 2、输入ollama pull llama3 下载llama3模型 3、 输入 ollama run llama3 运行模型 4、其他 ollama li…...

第三十七天打卡

过拟合的判断&#xff1a;测试集和训练集同步打印指标模型的保存和加载 仅保存权重保存权重和模型保存全部信息checkpoint&#xff0c;还包含训练状态 早停策略 过拟合判断 import torch import torch.nn as nn import torch.optim as optim from sklearn.datasets import load…...

数据结构-代码总结

下面代码自己上完课写着玩的,除了克鲁斯卡尔那里完全ai&#xff0c;其他基本上都是自己写的&#xff0c;具体请参考书本,同时也欢迎各位大佬来纠错 线性表 //线性表--顺序存储结构 #include<iostream> using namespace std; template<typename T> …...

力扣每日一题——找到离给定两个节点最近的节点

目录 题目链接&#xff1a;2359. 找到离给定两个节点最近的节点 - 力扣&#xff08;LeetCode&#xff09; 题目描述 解法一&#xff1a;双指针路径交汇法​ 基本思路 关键步骤 为什么这样可行呢我请问了&#xff1f; 举个例子 特殊情况 Java写法&#xff1a; C写法&a…...

论爱情《态度》

我犹记得&#xff0c;当吴军的《态度》到手之后&#xff0c;从中间翻开的第一页&#xff0c;便是此。 “合适的人&#xff0c;会让你看到&#xff0c;和得到全世界” -- 第22封 其实在我初中、高中的时候&#xff0c;我便产生一个问题&#xff0c;为什么学校要禁止谈恋爱。 …...

多台电脑共用一个ip地址可以吗?会怎么样

在互联网使用日益普及的今天&#xff0c;许多人都面临着多台设备共享网络的需求。一个常见的问题随之而来&#xff1a;多台电脑共用一个IP地址可以吗&#xff1f;这样做会带来哪些影响&#xff1f;本文将深入探讨这一话题。 一、多台电脑共用一个‌IP地址可以吗&#xff1f; 多…...

LG P4119 [Ynoi2018] 未来日记 Solution

Description 给定序列 a ( a 1 , a 2 , ⋯ , a n ) a(a_1,a_2,\cdots,a_n) a(a1​,a2​,⋯,an​)&#xff0c;有 m m m 个操作分两种&#xff1a; replace ⁡ ( l , r , x , y ) \operatorname{replace}(l,r,x,y) replace(l,r,x,y)&#xff1a;将 a l ∼ a r a_l\sim a_r …...

python实战项目71:基于Python的US News世界大学排名数据爬取

python实战项目71:基于Python的US News世界大学排名数据爬取 一、项目背景1.1 研究意义1.2 技术背景1.3 应用场景二、爬虫系统设计与实现2.1 分析页面、寻找数据真实接口2.2 发送请求,获取响应内容2.3 提取数据2.4 保存数据三、完整代码四、总结与展望一、项目背景 1.1 研究…...