• springBoot 整合redisson实现消息订阅详细教程


     maven 引入依赖

    
    
        org.redisson
        redisson-spring-boot-starter
        3.17.5
    

     yml配置文件

    1. spring:
    2. redis:
    3. # 地址
    4. host: localhost
    5. # 端口,默认为6379
    6. port: 6379
    7. # 数据库索引
    8. database: 0
    9. # 密码(如没有密码请注释掉)
    10. # password:
    11. # 连接超时时间
    12. timeout: 10s
    13. # 是否开启ssl
    14. ssl: false
    15. redisson:
    16. # redis key前缀
    17. keyPrefix:
    18. # 线程池数量
    19. threads: 4
    20. # Netty线程池数量
    21. nettyThreads: 8
    22. # 单节点配置
    23. singleServerConfig:
    24. # 客户端名称
    25. clientName: clientName
    26. # 最小空闲连接数
    27. connectionMinimumIdleSize: 8
    28. # 连接池大小
    29. connectionPoolSize: 32
    30. # 连接空闲超时,单位:毫秒
    31. idleConnectionTimeout: 10000
    32. # 命令等待超时,单位:毫秒
    33. timeout: 3000
    34. # 发布和订阅连接池大小
    35. subscriptionConnectionPoolSize: 50
    RedissonProperties #映射yml配置文件
    1. package com.example.demo.redisson;
    2. import lombok.Data;
    3. import lombok.NoArgsConstructor;
    4. import org.redisson.config.ReadMode;
    5. import org.redisson.config.SubscriptionMode;
    6. import org.springframework.boot.context.properties.ConfigurationProperties;
    7. import org.springframework.stereotype.Component;
    8. /**
    9. * @author yueF_L
    10. * @version 1.0
    11. * @date 2022-09-02 15:38
    12. * redisson 配置文件
    13. */
    14. @Data
    15. @Component
    16. @ConfigurationProperties(prefix = "redisson")
    17. public class RedissonProperties {
    18. /**
    19. * redis缓存key前缀
    20. */
    21. private String keyPrefix;
    22. /**
    23. * 线程池数量,默认值 = 当前处理核数量 * 2
    24. */
    25. private int threads;
    26. /**
    27. * Netty线程池数量,默认值 = 当前处理核数量 * 2
    28. */
    29. private int nettyThreads;
    30. /**
    31. * 单机服务配置
    32. */
    33. private SingleServerConfig singleServerConfig;
    34. /**
    35. * 集群服务配置
    36. */
    37. private ClusterServersConfig clusterServersConfig;
    38. @Data
    39. @NoArgsConstructor
    40. public static class SingleServerConfig {
    41. /**
    42. * 客户端名称
    43. */
    44. private String clientName;
    45. /**
    46. * 最小空闲连接数
    47. */
    48. private int connectionMinimumIdleSize;
    49. /**
    50. * 连接池大小
    51. */
    52. private int connectionPoolSize;
    53. /**
    54. * 连接空闲超时,单位:毫秒
    55. */
    56. private int idleConnectionTimeout;
    57. /**
    58. * 命令等待超时,单位:毫秒
    59. */
    60. private int timeout;
    61. /**
    62. * 发布和订阅连接池大小
    63. */
    64. private int subscriptionConnectionPoolSize;
    65. }
    66. @Data
    67. @NoArgsConstructor
    68. public static class ClusterServersConfig {
    69. /**
    70. * 客户端名称
    71. */
    72. private String clientName;
    73. /**
    74. * master最小空闲连接数
    75. */
    76. private int masterConnectionMinimumIdleSize;
    77. /**
    78. * master连接池大小
    79. */
    80. private int masterConnectionPoolSize;
    81. /**
    82. * slave最小空闲连接数
    83. */
    84. private int slaveConnectionMinimumIdleSize;
    85. /**
    86. * slave连接池大小
    87. */
    88. private int slaveConnectionPoolSize;
    89. /**
    90. * 连接空闲超时,单位:毫秒
    91. */
    92. private int idleConnectionTimeout;
    93. /**
    94. * 命令等待超时,单位:毫秒
    95. */
    96. private int timeout;
    97. /**
    98. * 发布和订阅连接池大小
    99. */
    100. private int subscriptionConnectionPoolSize;
    101. /**
    102. * 读取模式
    103. */
    104. private ReadMode readMode;
    105. /**
    106. * 订阅模式
    107. */
    108. private SubscriptionMode subscriptionMode;
    109. }
    110. }
    RedisConfig #redis配置
    1. package com.example.demo.redisson;
    2. /**
    3. * @author yueF_L
    4. * @version 1.0
    5. * @date 2022-09-02 15:37
    6. * redis配置
    7. */
    8. import cn.hutool.core.util.ObjectUtil;
    9. import com.fasterxml.jackson.databind.ObjectMapper;
    10. import lombok.extern.slf4j.Slf4j;
    11. import org.redisson.codec.JsonJacksonCodec;
    12. import org.redisson.spring.starter.RedissonAutoConfigurationCustomizer;
    13. import org.springframework.beans.factory.annotation.Autowired;
    14. import org.springframework.boot.context.properties.EnableConfigurationProperties;
    15. import org.springframework.cache.CacheManager;
    16. import org.springframework.cache.annotation.CachingConfigurerSupport;
    17. import org.springframework.cache.annotation.EnableCaching;
    18. import org.springframework.context.annotation.Bean;
    19. import org.springframework.context.annotation.Configuration;
    20. @Slf4j
    21. @Configuration
    22. @EnableCaching
    23. @EnableConfigurationProperties(RedissonProperties.class)
    24. public class RedisConfig extends CachingConfigurerSupport {
    25. @Autowired
    26. private RedissonProperties redissonProperties;
    27. @Autowired
    28. private ObjectMapper objectMapper;
    29. @Bean
    30. public RedissonAutoConfigurationCustomizer redissonCustomizer() {
    31. return config -> {
    32. config.setThreads(redissonProperties.getThreads())
    33. .setNettyThreads(redissonProperties.getNettyThreads())
    34. .setCodec(new JsonJacksonCodec(objectMapper));
    35. RedissonProperties.SingleServerConfig singleServerConfig = redissonProperties.getSingleServerConfig();
    36. if (ObjectUtil.isNotNull(singleServerConfig)) {
    37. // 使用单机模式
    38. config.useSingleServer()
    39. //设置redis key前缀
    40. .setNameMapper(new KeyPrefixHandler(redissonProperties.getKeyPrefix()))
    41. .setTimeout(singleServerConfig.getTimeout())
    42. .setClientName(singleServerConfig.getClientName())
    43. .setIdleConnectionTimeout(singleServerConfig.getIdleConnectionTimeout())
    44. .setSubscriptionConnectionPoolSize(singleServerConfig.getSubscriptionConnectionPoolSize())
    45. .setConnectionMinimumIdleSize(singleServerConfig.getConnectionMinimumIdleSize())
    46. .setConnectionPoolSize(singleServerConfig.getConnectionPoolSize());
    47. }
    48. // 集群配置方式 参考下方注释
    49. RedissonProperties.ClusterServersConfig clusterServersConfig = redissonProperties.getClusterServersConfig();
    50. if (ObjectUtil.isNotNull(clusterServersConfig)) {
    51. config.useClusterServers()
    52. //设置redis key前缀
    53. .setNameMapper(new KeyPrefixHandler(redissonProperties.getKeyPrefix()))
    54. .setTimeout(clusterServersConfig.getTimeout())
    55. .setClientName(clusterServersConfig.getClientName())
    56. .setIdleConnectionTimeout(clusterServersConfig.getIdleConnectionTimeout())
    57. .setSubscriptionConnectionPoolSize(clusterServersConfig.getSubscriptionConnectionPoolSize())
    58. .setMasterConnectionMinimumIdleSize(clusterServersConfig.getMasterConnectionMinimumIdleSize())
    59. .setMasterConnectionPoolSize(clusterServersConfig.getMasterConnectionPoolSize())
    60. .setSlaveConnectionMinimumIdleSize(clusterServersConfig.getSlaveConnectionMinimumIdleSize())
    61. .setSlaveConnectionPoolSize(clusterServersConfig.getSlaveConnectionPoolSize())
    62. .setReadMode(clusterServersConfig.getReadMode())
    63. .setSubscriptionMode(clusterServersConfig.getSubscriptionMode());
    64. }
    65. log.info("初始化 redis 配置");
    66. };
    67. }
    68. /**
    69. * 自定义缓存管理器 整合spring-cache
    70. */
    71. @Bean
    72. public CacheManager cacheManager() {
    73. return new PlusSpringCacheManager();
    74. }
    75. }
    KeyPrefixHandler #redis缓存key前缀处理
    1. package com.example.demo.redisson;
    2. import com.example.demo.util.StringUtils;
    3. import org.redisson.api.NameMapper;
    4. /**
    5. * @author yueF_L
    6. * @version 1.0
    7. * @date 2022-09-02 15:40
    8. * redis缓存key前缀处理
    9. */
    10. public class KeyPrefixHandler implements NameMapper {
    11. private final String keyPrefix;
    12. public KeyPrefixHandler(String keyPrefix) {
    13. //前缀为空 则返回空前缀
    14. this.keyPrefix = StringUtils.isBlank(keyPrefix) ? "" : keyPrefix + ":";
    15. }
    16. /**
    17. * 增加前缀
    18. */
    19. @Override
    20. public String map(String name) {
    21. if (StringUtils.isBlank(name)) {
    22. return null;
    23. }
    24. if (StringUtils.isNotBlank(keyPrefix) && !name.startsWith(keyPrefix)) {
    25. return keyPrefix + name;
    26. }
    27. return name;
    28. }
    29. /**
    30. * 去除前缀
    31. */
    32. @Override
    33. public String unmap(String name) {
    34. if (StringUtils.isBlank(name)) {
    35. return null;
    36. }
    37. if (StringUtils.isNotBlank(keyPrefix) && name.startsWith(keyPrefix)) {
    38. return name.substring(keyPrefix.length());
    39. }
    40. return name;
    41. }
    42. }

     

    PlusSpringCacheManager # 修改 RedissonSpringCacheManager 源码、 重写 cacheName 处理方法 支持多参数

     

    1. package com.example.demo.redisson;
    2. import org.redisson.api.RMap;
    3. import org.redisson.api.RMapCache;
    4. import org.redisson.spring.cache.CacheConfig;
    5. import org.redisson.spring.cache.RedissonCache;
    6. import org.springframework.boot.convert.DurationStyle;
    7. import org.springframework.cache.Cache;
    8. import org.springframework.cache.CacheManager;
    9. import org.springframework.cache.transaction.TransactionAwareCacheDecorator;
    10. import org.springframework.util.StringUtils;
    11. import java.util.Collection;
    12. import java.util.Collections;
    13. import java.util.Map;
    14. import java.util.concurrent.ConcurrentHashMap;
    15. import java.util.concurrent.ConcurrentMap;
    16. /**
    17. * @author yueF_L
    18. * @version 1.0
    19. * @date 2022-09-02 15:43
    20. * 修改 RedissonSpringCacheManager 源码
    21. * 重写 cacheName 处理方法 支持多参数
    22. */
    23. public class PlusSpringCacheManager implements CacheManager {
    24. private boolean dynamic = true;
    25. private boolean allowNullValues = true;
    26. private boolean transactionAware = true;
    27. Map configMap = new ConcurrentHashMap<>();
    28. ConcurrentMap instanceMap = new ConcurrentHashMap<>();
    29. /**
    30. * Creates CacheManager supplied by Redisson instance
    31. */
    32. public PlusSpringCacheManager() {
    33. }
    34. /**
    35. * Defines possibility of storing {@code null} values.
    36. *

    37. * Default is true
    38. *
    39. * @param allowNullValues stores if true
    40. */
    41. public void setAllowNullValues(boolean allowNullValues) {
    42. this.allowNullValues = allowNullValues;
    43. }
    44. /**
    45. * Defines if cache aware of Spring-managed transactions.
    46. * If {@code true} put/evict operations are executed only for successful transaction in after-commit phase.
    47. *

    48. * Default is false
    49. *
    50. * @param transactionAware cache is transaction aware if true
    51. */
    52. public void setTransactionAware(boolean transactionAware) {
    53. this.transactionAware = transactionAware;
    54. }
    55. /**
    56. * Defines 'fixed' cache names.
    57. * A new cache instance will not be created in dynamic for non-defined names.
    58. *

    59. * `null` parameter setups dynamic mode
    60. *
    61. * @param names of caches
    62. */
    63. public void setCacheNames(Collection names) {
    64. if (names != null) {
    65. for (String name : names) {
    66. getCache(name);
    67. }
    68. dynamic = false;
    69. } else {
    70. dynamic = true;
    71. }
    72. }
    73. /**
    74. * Set cache config mapped by cache name
    75. *
    76. * @param config object
    77. */
    78. public void setConfig(Map config) {
    79. this.configMap = (Map) config;
    80. }
    81. protected CacheConfig createDefaultConfig() {
    82. return new CacheConfig();
    83. }
    84. @Override
    85. public Cache getCache(String name) {
    86. Cache cache = instanceMap.get(name);
    87. if (cache != null) {
    88. return cache;
    89. }
    90. if (!dynamic) {
    91. return cache;
    92. }
    93. CacheConfig config = configMap.get(name);
    94. if (config == null) {
    95. config = createDefaultConfig();
    96. configMap.put(name, config);
    97. }
    98. // 重写 cacheName 支持多参数
    99. String[] array = StringUtils.delimitedListToStringArray(name, "#");
    100. name = array[0];
    101. if (array.length > 1) {
    102. config.setTTL(DurationStyle.detectAndParse(array[1]).toMillis());
    103. }
    104. if (array.length > 2) {
    105. config.setMaxIdleTime(DurationStyle.detectAndParse(array[2]).toMillis());
    106. }
    107. if (array.length > 3) {
    108. config.setMaxSize(Integer.parseInt(array[3]));
    109. }
    110. if (config.getMaxIdleTime() == 0 && config.getTTL() == 0 && config.getMaxSize() == 0) {
    111. return createMap(name, config);
    112. }
    113. return createMapCache(name, config);
    114. }
    115. private Cache createMap(String name, CacheConfig config) {
    116. RMap map = RedisUtils.getClient().getMap(name);
    117. Cache cache = new RedissonCache(map, allowNullValues);
    118. if (transactionAware) {
    119. cache = new TransactionAwareCacheDecorator(cache);
    120. }
    121. Cache oldCache = instanceMap.putIfAbsent(name, cache);
    122. if (oldCache != null) {
    123. cache = oldCache;
    124. }
    125. return cache;
    126. }
    127. private Cache createMapCache(String name, CacheConfig config) {
    128. RMapCache map = RedisUtils.getClient().getMapCache(name);
    129. Cache cache = new RedissonCache(map, config, allowNullValues);
    130. if (transactionAware) {
    131. cache = new TransactionAwareCacheDecorator(cache);
    132. }
    133. Cache oldCache = instanceMap.putIfAbsent(name, cache);
    134. if (oldCache != null) {
    135. cache = oldCache;
    136. } else {
    137. map.setMaxSize(config.getMaxSize());
    138. }
    139. return cache;
    140. }
    141. @Override
    142. public Collection getCacheNames() {
    143. return Collections.unmodifiableSet(configMap.keySet());
    144. }
    145. }
    SpringUtils #spring工具类
    1. package com.example.demo.util;
    2. import cn.hutool.extra.spring.SpringUtil;
    3. import org.springframework.aop.framework.AopContext;
    4. import org.springframework.beans.factory.NoSuchBeanDefinitionException;
    5. import org.springframework.stereotype.Component;
    6. /**
    7. * @author yueF_L
    8. * @version 1.0
    9. * @date 2022-09-02 15:45
    10. * spring 工具类
    11. */
    12. @Component
    13. public final class SpringUtils extends SpringUtil {
    14. /**
    15. * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
    16. *
    17. * @param name
    18. * @return boolean
    19. */
    20. public static boolean containsBean(String name) {
    21. return getBeanFactory().containsBean(name);
    22. }
    23. /**
    24. * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。
    25. * 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
    26. *
    27. * @param name
    28. * @return boolean
    29. */
    30. public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
    31. return getBeanFactory().isSingleton(name);
    32. }
    33. /**
    34. * @param name
    35. * @return Class 注册对象的类型
    36. */
    37. public static Class getType(String name) throws NoSuchBeanDefinitionException {
    38. return getBeanFactory().getType(name);
    39. }
    40. /**
    41. * 如果给定的bean名字在bean定义中有别名,则返回这些别名
    42. *
    43. * @param name
    44. */
    45. public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
    46. return getBeanFactory().getAliases(name);
    47. }
    48. /**
    49. * 获取aop代理对象
    50. *
    51. * @param invoker
    52. * @return
    53. */
    54. @SuppressWarnings("unchecked")
    55. public static T getAopProxy(T invoker) {
    56. return (T) AopContext.currentProxy();
    57. }
    58. }
    StringUtils #字符串工具类
    1. package com.example.demo.util;
    2. import cn.hutool.core.collection.CollUtil;
    3. import cn.hutool.core.lang.Validator;
    4. import cn.hutool.core.util.StrUtil;
    5. import lombok.AccessLevel;
    6. import lombok.NoArgsConstructor;
    7. import org.springframework.util.AntPathMatcher;
    8. import java.util.ArrayList;
    9. import java.util.HashSet;
    10. import java.util.List;
    11. import java.util.Set;
    12. /**
    13. * @author yueF_L
    14. * @version 1.0
    15. * @date 2022-09-02 15:41
    16. */
    17. @NoArgsConstructor(access = AccessLevel.PRIVATE)
    18. public class StringUtils extends org.apache.commons.lang3.StringUtils {
    19. /**
    20. * 获取参数不为空值
    21. *
    22. * @param str defaultValue 要判断的value
    23. * @return value 返回值
    24. */
    25. public static String blankToDefault(String str, String defaultValue) {
    26. return StrUtil.blankToDefault(str, defaultValue);
    27. }
    28. /**
    29. * * 判断一个字符串是否为空串
    30. *
    31. * @param str String
    32. * @return true:为空 false:非空
    33. */
    34. public static boolean isEmpty(String str) {
    35. return StrUtil.isEmpty(str);
    36. }
    37. /**
    38. * * 判断一个字符串是否为非空串
    39. *
    40. * @param str String
    41. * @return true:非空串 false:空串
    42. */
    43. public static boolean isNotEmpty(String str) {
    44. return !isEmpty(str);
    45. }
    46. /**
    47. * 去空格
    48. */
    49. public static String trim(String str) {
    50. return StrUtil.trim(str);
    51. }
    52. /**
    53. * 截取字符串
    54. *
    55. * @param str 字符串
    56. * @param start 开始
    57. * @return 结果
    58. */
    59. public static String substring(final String str, int start) {
    60. return substring(str, start, str.length());
    61. }
    62. /**
    63. * 截取字符串
    64. *
    65. * @param str 字符串
    66. * @param start 开始
    67. * @param end 结束
    68. * @return 结果
    69. */
    70. public static String substring(final String str, int start, int end) {
    71. return StrUtil.sub(str, start, end);
    72. }
    73. /**
    74. * 格式化文本, {} 表示占位符
    75. * 此方法只是简单将占位符 {} 按照顺序替换为参数
    76. * 如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可
    77. * 例:
    78. * 通常使用:format("this is {} for {}", "a", "b") -> this is a for b
    79. * 转义{}: format("this is \\{} for {}", "a", "b") -> this is {} for a
    80. * 转义\: format("this is \\\\{} for {}", "a", "b") -> this is \a for b
    81. *
    82. * @param template 文本模板,被替换的部分用 {} 表示
    83. * @param params 参数值
    84. * @return 格式化后的文本
    85. */
    86. public static String format(String template, Object... params) {
    87. return StrUtil.format(template, params);
    88. }
    89. /**
    90. * 是否为http(s)://开头
    91. *
    92. * @param link 链接
    93. * @return 结果
    94. */
    95. public static boolean ishttp(String link) {
    96. return Validator.isUrl(link);
    97. }
    98. /**
    99. * 字符串转set
    100. *
    101. * @param str 字符串
    102. * @param sep 分隔符
    103. * @return set集合
    104. */
    105. public static Set str2Set(String str, String sep) {
    106. return new HashSet<>(str2List(str, sep, true, false));
    107. }
    108. /**
    109. * 字符串转list
    110. *
    111. * @param str 字符串
    112. * @param sep 分隔符
    113. * @param filterBlank 过滤纯空白
    114. * @param trim 去掉首尾空白
    115. * @return list集合
    116. */
    117. public static List str2List(String str, String sep, boolean filterBlank, boolean trim) {
    118. List list = new ArrayList<>();
    119. if (isEmpty(str)) {
    120. return list;
    121. }
    122. // 过滤空白字符串
    123. if (filterBlank && isBlank(str)) {
    124. return list;
    125. }
    126. String[] split = str.split(sep);
    127. for (String string : split) {
    128. if (filterBlank && isBlank(string)) {
    129. continue;
    130. }
    131. if (trim) {
    132. string = trim(string);
    133. }
    134. list.add(string);
    135. }
    136. return list;
    137. }
    138. /**
    139. * 查找指定字符串是否包含指定字符串列表中的任意一个字符串同时串忽略大小写
    140. *
    141. * @param cs 指定字符串
    142. * @param searchCharSequences 需要检查的字符串数组
    143. * @return 是否包含任意一个字符串
    144. */
    145. public static boolean containsAnyIgnoreCase(CharSequence cs, CharSequence... searchCharSequences) {
    146. return StrUtil.containsAnyIgnoreCase(cs, searchCharSequences);
    147. }
    148. /**
    149. * 驼峰转下划线命名
    150. */
    151. public static String toUnderScoreCase(String str) {
    152. return StrUtil.toUnderlineCase(str);
    153. }
    154. /**
    155. * 是否包含字符串
    156. *
    157. * @param str 验证字符串
    158. * @param strs 字符串组
    159. * @return 包含返回true
    160. */
    161. public static boolean inStringIgnoreCase(String str, String... strs) {
    162. return StrUtil.equalsAnyIgnoreCase(str, strs);
    163. }
    164. /**
    165. * 将下划线大写方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。 例如:HELLO_WORLD->HelloWorld
    166. *
    167. * @param name 转换前的下划线大写方式命名的字符串
    168. * @return 转换后的驼峰式命名的字符串
    169. */
    170. public static String convertToCamelCase(String name) {
    171. return StrUtil.upperFirst(StrUtil.toCamelCase(name));
    172. }
    173. /**
    174. * 驼峰式命名法 例如:user_name->userName
    175. */
    176. public static String toCamelCase(String s) {
    177. return StrUtil.toCamelCase(s);
    178. }
    179. /**
    180. * 查找指定字符串是否匹配指定字符串列表中的任意一个字符串
    181. *
    182. * @param str 指定字符串
    183. * @param strs 需要检查的字符串数组
    184. * @return 是否匹配
    185. */
    186. public static boolean matches(String str, List strs) {
    187. if (isEmpty(str) || CollUtil.isEmpty(strs)) {
    188. return false;
    189. }
    190. for (String pattern : strs) {
    191. if (isMatch(pattern, str)) {
    192. return true;
    193. }
    194. }
    195. return false;
    196. }
    197. /**
    198. * 判断url是否与规则配置:
    199. * ? 表示单个字符;
    200. * * 表示一层路径内的任意字符串,不可跨层级;
    201. * ** 表示任意层路径;
    202. *
    203. * @param pattern 匹配规则
    204. * @param url 需要匹配的url
    205. * @return
    206. */
    207. public static boolean isMatch(String pattern, String url) {
    208. AntPathMatcher matcher = new AntPathMatcher();
    209. return matcher.match(pattern, url);
    210. }
    211. /**
    212. * 数字左边补齐0,使之达到指定长度。注意,如果数字转换为字符串后,长度大于size,则只保留 最后size个字符。
    213. *
    214. * @param num 数字对象
    215. * @param size 字符串指定长度
    216. * @return 返回数字的字符串格式,该字符串为指定长度。
    217. */
    218. public static final String padl(final Number num, final int size) {
    219. return padl(num.toString(), size, '0');
    220. }
    221. /**
    222. * 字符串左补齐。如果原始字符串s长度大于size,则只保留最后size个字符。
    223. *
    224. * @param s 原始字符串
    225. * @param size 字符串指定长度
    226. * @param c 用于补齐的字符
    227. * @return 返回指定长度的字符串,由原字符串左补齐或截取得到。
    228. */
    229. public static final String padl(final String s, final int size, final char c) {
    230. final StringBuilder sb = new StringBuilder(size);
    231. if (s != null) {
    232. final int len = s.length();
    233. if (s.length() <= size) {
    234. for (int i = size - len; i > 0; i--) {
    235. sb.append(c);
    236. }
    237. sb.append(s);
    238. } else {
    239. return s.substring(len - size, len);
    240. }
    241. } else {
    242. for (int i = size; i > 0; i--) {
    243. sb.append(c);
    244. }
    245. }
    246. return sb.toString();
    247. }
    248. }
    RedisUtils #redis工具类
    1. /**
    2. * @author yueF_L
    3. * @version 1.0
    4. * @date 2022-09-02 15:44
    5. */
    6. @NoArgsConstructor(access = AccessLevel.PRIVATE)
    7. @SuppressWarnings(value = {"unchecked", "rawtypes"})
    8. public class RedisUtils {
    9. private static final RedissonClient CLIENT = SpringUtils.getBean(RedissonClient.class);
    10. /**
    11. * 发布通道消息
    12. *
    13. * @param channelKey 通道key
    14. * @param msg 发送数据
    15. * @param consumer 自定义处理
    16. */
    17. public static void publish(String channelKey, T msg, Consumer consumer) {
    18. RTopic topic = CLIENT.getTopic(channelKey);
    19. topic.publish(msg);
    20. consumer.accept(msg);
    21. }
    22. /**
    23. * 订阅通道接收消息
    24. *
    25. * @param channelKey 通道key
    26. * @param clazz 消息类型
    27. * @param consumer 自定义处理
    28. */
    29. public static void subscribe(String channelKey, Class clazz, Consumer consumer) {
    30. RTopic topic = CLIENT.getTopic(channelKey);
    31. topic.addListener(clazz, (channel, msg) -> consumer.accept(msg));
    32. }
    33. }
    TestRedisson #测试控制器
    1. package com.example.demo.redisson;
    2. import com.example.demo.base.R;
    3. import org.springframework.web.bind.annotation.GetMapping;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import org.springframework.web.bind.annotation.RestController;
    6. /**
    7. * @author yueF_L
    8. * @version 1.0
    9. * @date 2022-09-02 15:50
    10. */
    11. @RestController
    12. @RequestMapping("/redisson")
    13. public class TestRedisson {
    14. /**
    15. * 发布消息
    16. *
    17. * @param key 通道Key
    18. * @param value 发送内容
    19. */
    20. @GetMapping("/pub")
    21. public R pub(String key, String value) {
    22. RedisUtils.publish(key, value, consumer -> {
    23. System.out.println("发布通道 => " + key + ", 发送值 => " + value);
    24. });
    25. return R.ok("操作成功");
    26. }
    27. /**
    28. * 订阅消息
    29. *
    30. * @param key 通道Key
    31. */
    32. @GetMapping("/sub")
    33. public R sub(String key) {
    34. RedisUtils.subscribe(key, String.class, msg -> {
    35. System.out.println("订阅通道 => " + key + ", 接收值 => " + msg);
    36. });
    37. return R.ok("操作成功");
    38. }
    39. }

    postman #断点测试

    订阅消息:

     发布消息

     测试成功:

     

  • 相关阅读:
    生成式AI模型量化简明教程
    使用mfuzz进行时间序列转录组数据聚类,划分相似表达模式基因群
    说说求职者和面试官的那些事
    Web前端大作业、基于HTML+CSS+JavaScript响应式个人相册博客网站
    【JavaDoc注释风格详解】
    11 SpringMVC之拦截器
    面向对象编程——类与对象(C#)(未写完)
    【Python大数据笔记_day09_hive函数和调优】
    视频监控/视频云存储EasyCVR平台接入华为ivs3800平台提示400报错,如何解决?
    ​力扣解法汇总1656-设计有序流
  • 原文地址:https://blog.csdn.net/weixin_38982591/article/details/126665266