redis发布订阅

本文介绍了一个使用Redis发布订阅功能来动态调整消费线程数量的项目案例。通过配置Spring的Redis连接池、哨兵集群和消息监听器,实现了一个能够响应配置变更、调整消费线程数的系统。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目中使用redis发布订阅实现了动态修改配置控制消费线程数。

1 redis配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- Redis 连接池配置 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="20"/>
        <property name="maxIdle" value="10"/>
        <property name="minIdle" value="5"/>
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false"/>
        <property name="testWhileIdle" value="true"/>
    </bean>

     <!-- redis sentinel 配置 -->
    <bean id="redisSentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
        <!-- cluster name -->
        <property name="master">
            <bean class="org.springframework.data.redis.connection.RedisNode">
                <property name="name" value="mymaster" />
            </bean>
        </property>
        <!-- sentinel信息 -->
        <property name="sentinels">
            <set>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg index="0" value="172.17.27.27"/>
                    <constructor-arg index="1" value="26379"/>
                </bean>
            </set>
        </property>
    </bean>

   <!-- 连接工厂配置 -->
   <bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">
       <!-- 使用的db编号-->
       <property name="database" value="0" />
       <!-- redis sentinel配置 -->
       <constructor-arg ref="redisSentinelConfiguration"/>
       <!-- jedis 连接池配置 -->
       <constructor-arg ref="jedisPoolConfig" />
   </bean>

   <!-- 数据模板配置 -->
   <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
       <property name="connectionFactory" ref="jedisConnFactory"/>
       <property name="keySerializer">
           <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
       </property>
       <property name="valueSerializer">
           <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
       </property>
   </bean>

    <bean id="topicContainer"
          class="org.springframework.data.redis.listener.RedisMessageListenerContainer"
          destroy-method="destroy">
        <property name="connectionFactory" ref="jedisConnFactory" />
        <property name="taskExecutor">
            <bean
                    class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler">
                <property name="poolSize" value="3"></property>
            </bean>
        </property>
        <property name="messageListeners">
            <map>
                <entry key-ref="threadNumRedisNessageListener">
                    <bean class="org.springframework.data.redis.listener.ChannelTopic">
                        <constructor-arg value="threadNumTopicName" />
                    </bean>
                </entry>
            </map>
        </property>
    </bean>

    <bean id="threadNumRedisNessageListener" class="com.bestpay.messagecenter.product.core.oss.ThreadNumRedisNessageListener"/>

    <bean id="threadNumRedisPublish" class="com.bestpay.messagecenter.product.core.oss.ThreadNumRedisPublish">
        <property name="threadNumTopicName" value="threadNumTopicName"/>
    </bean>

</beans>
2.监听类
package com.bestpay.messagecenter.product.core.oss;

import java.io.Serializable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;

import com.bestpay.messagecenter.product.core.model.oss.ConfigurationBO;

import lombok.extern.slf4j.Slf4j;

/**
 * 接收修改线程数的监听类
 * @author linxing
 * @version Id: ThreadNumRedisNessageListener.java, v 0.1 2017/7/3 9:43 linxing Exp $$
 */
@Slf4j
public class ThreadNumRedisNessageListener implements MessageListener {

    @Autowired
    private RedisTemplate<Serializable, Serializable> redisTemplate;

    @Override
    public void onMessage(Message message, byte[] bytes) {
        byte[] body = message.getBody();// 请使用valueSerializer
        byte[] channel = message.getChannel();
        ConfigurationBO configurationBO = (ConfigurationBO) redisTemplate.getValueSerializer()
            .deserialize(body);
        log.debug("关键词 在:{} 接收消息:{}", new String(channel), configurationBO);
		//TODO
	}
}


3.发送消息
package com.bestpay.messagecenter.product.core.oss;

import java.io.Serializable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import lombok.extern.slf4j.Slf4j;

/**
 * @author linxing
 * @version Id: ThreadNumRedisPublish.java, v 0.1 2017/7/3 10:04 linxing Exp $$
 */
@Slf4j
public class ThreadNumRedisPublish {

    @Autowired
    private RedisTemplate<Serializable, Serializable> redisTemplate;

    private String                                    threadNumTopicName;

    public void sendMessage(Serializable message) {
        log.debug("关键词 在{},发送消息:{}", threadNumTopicName, message);
        redisTemplate.convertAndSend(threadNumTopicName, message);
    }

    /**
     * 设置 threadNumTopicName
     * return
     */
    public void setThreadNumTopicName(String threadNumTopicName) {
        this.threadNumTopicName = threadNumTopicName;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值