java+redis+spring mvc实现发布订阅(不同项目间)

项目1:利用redis做消息队列发布消息

项目2:利用redis订阅项目1发布的消息


项目1(发布):

properties配置文件中redis配置:

redis.hostName=192.168.1.1
redis.port=6379
redis.timeout=15000
redis.usePool=true
redis.maxIdle=6
redis.minEvictableIdleTimeMillis=300000
redis.numTestsPerEvictionRun=3
redis.timeBetweenEvictionRunsMillis=60000

spring配置文件添加redis配置(只列出了redis相关配置):


xmlns:redis="http://www.springframework.org/schema/redis"


http://www.springframework.org/schema/redis
http://www.springframework.org/schema/redis/spring-redis-1.0.xsd


<util:properties id="config" location="classpath:../conf/config.properties"/>

<!-- redis配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> 
        <property name="maxActive" value="3000"/> 
       <property name="maxIdle" value="100"/> 
        <property name="maxWait" value="10000"/> 
        <property name="testOnBorrow" value="true"/> 
</bean> 
<bean id="jdkSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />  

<bean  id='jedisConnectionFactory'  
        class='org.springframework.data.redis.connection.jedis.JedisConnectionFactory' >
  <property name="hostName" value="${redis.hostName}"></property>
  <property name="port" value="${redis.port}"></property>
  <property name="usePool" value="${redis.usePool}"></property>
  <property name="poolConfig" ref="jedisPoolConfig"></property> 
      </bean>
       
     <bean id='redisTemplate' class='org.springframework.data.redis.core.RedisTemplate'>  
         <property name="connectionFactory" ref="jedisConnectionFactory"></property>
        <!-- 使用string主要是key 在redis端用命令好读 不然默认的序列化没办法读 -->    
        <property name="keySerializer">    
           <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />    
        </property>    
        <property name="hashKeySerializer">    
           <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />    
        </property>  
        <property name="valueSerializer">  
            <bean  
                class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
        </property>
     </bean>

RedisDao接口:

import java.util.Map;
import org.springframework.data.redis.core.ValueOperations;
public interface RedisDao {
/*
 * 设置频道
 */
public static final String TQCHANNEL = "channel_message";
public void sendMessage(String channel, String message);
}

RedisDaoImpl实现类:

public class RedisDaoImpl implements RedisDao {
@Autowired
public RedisTemplate<String, Object> redisTemplate;
@Override
public void sendMessage(String channel, String message) {
System.out.println("开始发布消息。。。");
redisTemplate.convertAndSend(channel, message); 
System.out.println("发布成功!");
}
}

程序中调用RedisDaoImpl发布消息:

redisDao.sendMessage(“channel_message”, "要发布的字符串");


项目2(订阅):

properties配置文件中redis配置:

redis.hostName=192.168.1.1
redis.port=6379
redis.timeout=15000
redis.usePool=true
redis.maxIdle=6
redis.minEvictableIdleTimeMillis=300000
redis.numTestsPerEvictionRun=3
redis.timeBetweenEvictionRunsMillis=60000

spring配置文件添加redis配置(只列出了redis相关配置):


xmlns:redis="http://www.springframework.org/schema/redis"


http://www.springframework.org/schema/redis
http://www.springframework.org/schema/redis/spring-redis-1.0.xsd


<util:properties id="config" location="classpath:../conf/config.properties"/>

<!-- redis配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> 
        <property name="maxActive" value="3000"/> 
       <property name="maxIdle" value="100"/> 
        <property name="maxWait" value="10000"/> 
        <property name="testOnBorrow" value="true"/> 
</bean> 
<bean  
        id='jedisConnectionFactory'  
        class='org.springframework.data.redis.connection.jedis.JedisConnectionFactory' >
  <property name="hostName" value="${redis.hostName}"></property>
  <property name="port" value="${redis.port}"></property>
  <property name="usePool" value="${redis.usePool}"></property>
  <property name="poolConfig" ref="jedisPoolConfig"></property> 
      </bean>
       
     <bean id='redisTemplate' class='org.springframework.data.redis.core.RedisTemplate'>  
         <property name="connectionFactory" ref="jedisConnectionFactory"></property>
        <!-- 使用string主要是key 在redis端用命令好读 不然默认的序列化没办法读 -->    
        <property name="keySerializer">    
           <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />    
        </property>    
        <property name="hashKeySerializer">    
           <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />    
        </property>  
        <property name="valueSerializer">  
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
        </property>
        <property name="hashValueSerializer">  
         <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
    </property>
     </bean>
     
     <!-- redis发布订阅配置 -->
   <bean id="serialization" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />  
   
  <!-- 消息监听适配器  delegate属性指定真正的目标处理器-->  
    <bean id="smsMessageListener"
        class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter">
        <property name="delegate" ref="tQMessageDelegateListener" />
       <!--  <property name="serializer" ref="serialization" />   -->
    </bean>
    <bean id="tQMessageDelegateListener" class="com.cn.listener.TQMessageDelegateListener"/>
    <!-- 消息监听适配器对应的监听容器 -->
     <redis:listener-container  connection-factory="jedisConnectionFactory">
        <redis:listener ref="smsMessageListener" method="handleMessage"
            topic="tq_message" /> 
    </redis:listener-container>

监听器TQMessageDelegateListener代码:

public class TQMessageDelegateListener  {
@Autowired
protected JdbcTemplate jdbcTemplate ;
@Autowired
private JdbcTemplate jdbc;
//private UserController user = new UserController();
    public void  handleMessage(String message){

System.out.println("监听到的消息为:"+message);

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 是一个用于构建微服务的开源框架,它能够快速搭建项目并且提供了许多便捷的功能和特性。Spring Security 是一个用于处理认证和授权的框架,可以保护我们的应用程序免受恶意攻击。JWT(JSON Web Token)是一种用于身份验证的开放标准,可以被用于安全地传输信息。Spring MVC 是一个用于构建 Web 应用程序的框架,它能够处理 HTTP 请求和响应。MyBatis 是一个用于操作数据库的框架,可以简化数据库操作和提高效率。Redis 是一种高性能的键值存储系统,可以用于缓存与数据存储。 基于这些技术,可以搭建一个商城项目Spring Boot 可以用于构建商城项目的后端服务Spring Security 可以确保用户信息的安全性,JWT 可以用于用户的身份验证,Spring MVC 可以处理前端请求,MyBatis 可以操作数据库,Redis 可以用于缓存用户信息和商品信息。 商城项目的后端可以使用 Spring Boot 和 Spring Security 来搭建,通过 JWT 来处理用户的身份验证和授权。数据库操作可以使用 MyBatis 来简化与提高效率,同时可以利用 Redis 来缓存一些常用的数据和信息,提升系统的性能。前端请求则可以通过 Spring MVC 来处理,实现商城项目的整体功能。 综上所述,借助于 Spring Boot、Spring Security、JWT、Spring MVC、MyBatis 和 Redis 这些技术,可以构建出一个高性能、安全可靠的商城项目,为用户提供良好的购物体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值