kafka:
vue_financial
webSocket:学习引用
SpringBoot2.0集成WebSocket,实现后台向前端推送信息_★【World Of Moshow 郑锴】★-CSDN博客_springboot集成websocket
项目结构:
kafka消费者service:
package com.xx.demo.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import kafka.consumer.ConsumerConfig;
import kafka.consumer.ConsumerIterator;
import kafka.consumer.KafkaStream;
import kafka.javaapi.consumer.ConsumerConnector;
import kafka.serializer.StringDecoder;
import kafka.utils.VerifiableProperties;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
* Created by csn on 2020/1/7.
@Service
public class KafkaComsumer {
// 接收主题消费者
private static ConsumerConnector backconsumer;
// 接收主题
public static String BACKTOPIC = "websocketTopic";
boolean starttype = false;
public void initkafka() {
if (starttype) {
return;
try {
initBackConsumer();
startBackConsumer();
starttype = true;
} catch (Exception e) {
starttype = false;
backconsumer.shutdown();
* 初始化接收主题消费者
private static void initBackConsumer() {
Properties props = new Properties();
// zookeeper 集群地址
props.put("zookeeper.connect",
"xx.xx.xx.xx:2181,2xx.xx.xx.xx:2181,xx.xx.xx.xx:2181");
// group 代表一个消费组
props.put("group.id", "csnWebSocket-group"); //组名任意写
// zk连接超时
props.put("zookeeper.session.timeout.ms", "4000");
props.put("zookeeper.sync.time.ms", "200");
props.put("auto.commit.interval.ms", "5000");
props.put("auto.offset.reset", "largest");
props.put("enable.auto.commit", "false");
// 序列化类
props.put("serializer.class", "kafka.serializer.StringEncoder");
ConsumerConfig config = new ConsumerConfig(props);
backconsumer = kafka.consumer.Consumer.createJavaConsumerConnector(config);
* 开启接收主题消费者线程
public static void startBackConsumer() {
new Thread(new Runnable() {
@Override
public void run() {
Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
topicCountMap.put(BACKTOPIC, new Integer(1));
StringDecoder keyDecoder = new StringDecoder(
new VerifiableProperties());
StringDecoder valueDecoder = new StringDecoder(
new VerifiableProperties());
Map<String, List<KafkaStream<String, String>>> consumerMap = backconsumer
.createMessageStreams(topicCountMap, keyDecoder,
valueDecoder);
KafkaStream<String, String> stream = consumerMap.get(BACKTOPIC)
.get(0);
ConsumerIterator<String, String> it = stream.iterator();
String message = "";
JSONObject obj = new JSONObject();
while (true) {
while (it.hasNext()) {
message = it.next().message();
try {
obj = JSON.parseObject(message);
for (WebSocket socket: WebSocket.webSocketSet
try {
//webSocket推送消息
socket.sendMessage(obj.toString());
} catch (Exception e) {
e.printStackTrace();
} catch (JSONException e) {
continue;
}, "BackThread").start();
webSocket服务端service:
package com.xx.demo.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.util.concurrent.CopyOnWriteArraySet;
* Created by csn on 2020/1/8.
@ServerEndpoint("/webSocket")
@Component
public class WebSocket {
// 用来记录当前连接数的变量
private static volatile int onlineCount = 0;
// concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象
public static CopyOnWriteArraySet<WebSocket> webSocketSet = new CopyOnWriteArraySet<WebSocket>();
//接收人
//private String userId = "";
// 与某个客户端的连接会话,需要通过它来与客户端进行数据收发
private Session session;
private static final Logger LOGGER = LoggerFactory.getLogger(WebSocket.class);
@OnOpen
public void onOpen(Session session) throws Exception {
this.session = session;
webSocketSet.add(this);
LOGGER.info("当前在线人数为:" + webSocketSet.size());
@OnClose
public void onClose() {
webSocketSet.remove(this);
LOGGER.info("Close a websocket. ");
* 收到客户端消息后调用的方法
* @param message 客户端发送过来的消息
@OnMessage
public void onMessage(String message, Session session) {
LOGGER.info("Receive a message from client: " + message);
@OnError
public void onError(Session session, Throwable error) {
LOGGER.error("Error while websocket. ", error);
* 实现服务器主动推送
public void sendMessage(String message) throws Exception {
if (this.session.isOpen()) {
this.session.getBasicRemote().sendText(message);
public static synchronized int getOnlineCount() {
return onlineCount;
public static synchronized void addOnlineCount() {
WebSocket.onlineCount++;
public static synchronized void subOnlineCount() {
WebSocket.onlineCount--;
webSocket配置类:
package com.xx.demo.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
* Created by csn on 2020/1/8.
@Configuration
@ConditionalOnWebApplication
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
@Bean
public MySpringConfigurator mySpringConfigurator() {
return new MySpringConfigurator();
Spring配置ServerEndpointConfig类:
package com.xx.demo.config;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import javax.websocket.server.ServerEndpointConfig;
* Created by csn on 2020/1/8.
public class MySpringConfigurator extends ServerEndpointConfig.Configurator implements ApplicationContextAware {
private static volatile BeanFactory context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
MySpringConfigurator.context = applicationContext;
@Override
public <T> T getEndpointInstance(Class<T> clazz) throws InstantiationException {
return context.getBean(clazz);
简单的前端页面(webSocket客户端):
<!DOCTYPE html>
<title>WebSocket示例</title>
<meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport' />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<input id="text" type="text"/>
<button οnclick="send()">发送消息</button>
<button οnclick="closeWebSocket()">关闭WebSocket连接</button>
<div id="message"></div>
</body>
<script type="text/javascript">
var websocket = null;
//判断当前浏览器是否支持WebSocket
if ('WebSocket' in window) {
// 不带参数的写法
//websocket = new WebSocket("ws://127.0.0.1:18080/webSocket");
// 通过路径传递参数的方法(服务端采用第一种方法"@ServerEndpoint"实现)
websocket = new WebSocket("ws://" + window.location.host + "/webSocket");
//websocket = new WebSocket("ws://127.0.0.1:18080/webSocket/23/Lebron");
// 通过类似GET请求方式传递参数的方法(服务端采用第二种方法"WebSocketHandler"实现)
//websocket = new WebSocket("ws://127.0.0.1:18080/webSocket?id=23&name=Lebron");
else {
alert('当前浏览器 Not support websocket')
//连接发生错误的回调方法
websocket.onerror = function () {
setMessageInnerHTML("WebSocket连接发生错误");
//连接成功建立的回调方法
websocket.onopen = function () {
setMessageInnerHTML("WebSocket连接成功");
//接收到消息的回调方法
websocket.onmessage = function (event) {
setMessageInnerHTML(event.data);
//连接关闭的回调方法
websocket.onclose = function () {
setMessageInnerHTML("WebSocket连接关闭");
//监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
window.onbeforeunload = function () {
closeWebSocket();
//将消息显示在网页上
function setMessageInnerHTML(innerHTML) {
document.getElementById('message').innerHTML += innerHTML + '<br/>';
//关闭WebSocket连接
function closeWebSocket() {
websocket.close();
//发送消息
function send() {
var message = document.getElementById('text').value;
websocket.send(message);
</script>
</html>
亲测可用...
逻辑:kafka订阅消费者主题 → 消费后通过webSocket推送到前端kafka:http://kafka.apachecn.org/webSocket:学习引用https://blog.csdn.net/moshowgame/article/details/80275084项目结构:kafka消费者service:package com.xx.demo.se...
rabbitmq+websocket(SpringBoot版)实现分布式消息推送
本来想用websocket做一个消息推送 可是分布式环境下不支持session共享因为服务器不同
所以采用 rabbitMQ+webSocket实现分布式消息推送
生产者将消息 发送给 rabbitMQ 的 virtual-host:/(顶极路由) 再由它路由到交换机 最终由交换机通过路由键指定具体的管道
消费者监听指定的管道获取消息
最终将获取的消息 交给 webSocket 被@OnMessage注解标识的方法
每次消费一条消息交给 被@OnMessage注解标识的方法 返回给前台
实现分布式实时推送
前端实时展现:使用websocket技术,实现服务器端数据推送到前端展现
通过Java的kafka client端获取数据,并且通过websock推送到前端。
websocket
websocket是HTML5开始...
WebSocket + Vue 简单聊天的实现1. 后端大体结构2. 前提练习3. 监听 Netty 启动4. WebSocket 服务类4.1 WebSocketServer4.2 初始化器4.3 助手类4.3.1 POJO 类4.3.2 枚举类4.3.3 关联类5. 前端信息6. 浏览器展示
1. 后端大体结构
一些固定的 util 类:
https://blog.csdn.net/YKenan/article/details/106319712
<dependen
框架:springcloud
角色:设备、大数据平台、后端、前端
场景:设备采集数据通过MQTT协议传输到平台,平台将数据以文件的形式存储到大数据平台中,并将数据写入kafka生产者中。前端以设备id做为唯一标识通过websocket与后端建立一对一连接,java后端的kafka消费者监听到有消息进来后,通过消息中的设备id做为标识向前端推送对应设备的数据,达到实时采集显示的效果。
:此场景在只有一台服务器的情况下是可以正常实现的,但是如果后端服务部署到多台服务器则会出现问题:前端通过网关
const ws = new WebSocket('ws://localhost:8080');
ws.onmessage = (event) => {
this.dataList.push(event.data);
</script>
在这个示例中,我们首先创建了一个 WebSocket 服务器,并在连接时打印出“Client connected”消息。每秒钟,服务器会向客户端发送当前时间。
在前端,我们通过 Vue 的 created 生命周期创建了一个 WebSocket 连接,并在每次接收到服务器发来的消息时将其添加到一个数据列表中,然后在模板中通过 v-for 渲染出来。