备注:我们会在文章的最后把整个项目的git地址:传送门,以供参考
//代码地址
https://github.com/HouChenggong/redis_publish_websocket
文章目录
1、maven集成websocket和redis
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
2、Socket配置websocketConfig
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
/***
* 配置ServerEndpointExporter,配置后会自动注册所有“@ServerEndpoint”注解声明的Websocket Endpoint
*/
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
3、RedisConfig配置
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
@Configuration
public class RedisConfig {
@Bean("container")
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
LettuceConnectionFactory lettuceConnectionFactory = (LettuceConnectionFactory) connectionFactory;
//设置存储的节点
lettuceConnectionFactory.setDatabase(0);
container.setConnectionFactory(lettuceConnectionFactory);
//这里要设定监听的主题是chat
container.addMessageListener(listenerAdapter, new PatternTopic("chat"));
return container;
}
@Bean
MessageListenerAdapter listenerAdapter(RedisMessageListener receiver) {
return new MessageListenerAdapter(receiver);
}
@Bean
StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
return new StringRedisTemplate(connectionFactory);
}
}
4、要实现的功能概述
4.1 实现websocket实时推送消息
4.2 实现redis发布与订阅
4.3 shiro不登陆也能访问websocket接口
filterChainDefinitionMap.put("/socket/**", "anon");
4.4 通过静态HTML访问页面,页面在resource/static/html目录下
4.5 shiro不登录也能访问resource/static/html下的页面
//socket测试入口
filterChainDefinitionMap.put("/html/**", "anon");
4.6 实现websocket在线人数统计
4.7 实现websocket在线聊天,指定发送给全部的人,或者某一个人
4.8 给没有在线但是订阅主题的人推送消息
5、开始设计方案
5.1 首先写redis发布消息的service
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
@Service
public class SendMessageService {
@Autowired
private StringRedisTemplate template;
/**
* redis发布消息,就是往指定频道发消息
*
* @param channel 订阅的频道
* @param message 发布 的内容
*/
public void sendMessage(String channel, String message) {
template.convertAndSend(channel, message);
System.out.println("您已经向频道:" + channel + "发布了一条消息,内容是: " + message);
}
}
5.2 发布消息的controller
@Autowired
SendMessageService messageService;
@GetMapping("/publish")
@ResponseBody
@ApiImplicitParams({
@ApiImplicitParam(name = "message", value = "message", required = true, paramType = "query", dataType = "String")
})
public String publicMessage(@RequestParam