Browse Source

修改配置

xieshengjie 3 years ago
parent
commit
81e3c3619b

+ 16 - 0
web/monitor-web/pom.xml

@@ -25,6 +25,10 @@
             <scope>test</scope>
         </dependency>
         <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-websocket</artifactId>
+        </dependency>
+        <dependency>
             <groupId>org.projectlombok</groupId>
             <artifactId>lombok</artifactId>
             <optional>true</optional>
@@ -34,6 +38,18 @@
             <artifactId>common</artifactId>
             <version>${project.version}</version>
         </dependency>
+        <dependency>
+            <groupId>com.fasterxml.jackson.core</groupId>
+            <artifactId>jackson-databind</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.fasterxml.jackson.core</groupId>
+            <artifactId>jackson-core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.fasterxml.jackson.core</groupId>
+            <artifactId>jackson-annotations</artifactId>
+        </dependency>
         <!--nacos 依赖-->
         <dependency>
             <groupId>com.alibaba.cloud</groupId>

+ 2 - 0
web/monitor-web/src/main/java/com/gyee/monitor/MonitorMain.java

@@ -4,6 +4,7 @@ import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
+import org.springframework.scheduling.annotation.EnableScheduling;
 
 /**
  * @ClassName : MonitorMain
@@ -12,6 +13,7 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  * @Description :
  */
 @SpringBootApplication
+@EnableScheduling
 @MapperScan("com.gyee.monitor.mapper")
 @EnableDiscoveryClient
 public class MonitorMain {

+ 14 - 0
web/monitor-web/src/main/java/com/gyee/monitor/config/WebSocketConfig.java

@@ -0,0 +1,14 @@
+package com.gyee.monitor.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.socket.server.standard.ServerEndpointExporter;
+
+@Configuration
+public class WebSocketConfig {
+    @Bean
+    public ServerEndpointExporter serverEndpointExporter() {
+        return new ServerEndpointExporter();
+    }
+
+}

+ 50 - 0
web/monitor-web/src/main/java/com/gyee/monitor/task/MonitorTask.java

@@ -0,0 +1,50 @@
+package com.gyee.monitor.task;
+
+import com.gyee.common.util.JSONUtil;
+import com.gyee.monitor.service.matrix.MatrixService;
+import com.gyee.monitor.websocket.MonitorSocket;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.scheduling.annotation.EnableScheduling;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+import java.io.IOException;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * @ClassName : MonitorTask
+ * @Author : xieshengjie
+ * @Date: 2022/3/15 16:46
+ * @Description :
+ */
+@Component
+@EnableScheduling
+public class MonitorTask {
+    private static Logger logger = LoggerFactory.getLogger(MonitorTask.class);
+    @Resource
+    private MatrixService matrixService;
+
+
+    @Scheduled(cron = "0/2 * * * * ?")   //每2s执行一次
+    public void marix() {
+        //System.err.println("*********   定时任务执行   **************");
+        ConcurrentHashMap<String, MonitorSocket> webSocketSet =
+                MonitorSocket.getWebSocketSet();
+        int i = 0;
+        webSocketSet.forEach((k, v) -> {
+            try {
+                if (webSocketSet.get(k).getSession().isOpen()) {
+                    if (k.equals("matrix"))
+                    webSocketSet.get(k).sendMessage(JSONUtil.objectToJson(matrixService.matrixDatas()));
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+
+        });
+
+        //System.err.println("*********   定时任务完成   **************");
+    }
+}

+ 120 - 0
web/monitor-web/src/main/java/com/gyee/monitor/websocket/MonitorSocket.java

@@ -0,0 +1,120 @@
+package com.gyee.monitor.websocket;
+
+import org.springframework.stereotype.Component;
+
+import javax.websocket.*;
+import javax.websocket.server.PathParam;
+import javax.websocket.server.ServerEndpoint;
+import java.io.IOException;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * @ClassName : MonitorSocket
+ * @Author : xieshengjie
+ * @Date: 2022/3/15 16:43
+ * @Description :
+ */
+@ServerEndpoint(value = "/monitor/{module}")
+@Component
+public class MonitorSocket {
+    /**
+     * 静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
+     */
+    private static int onlineCount = 0;
+
+    /**
+     * concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。
+     * 在外部可以获取此连接的所有websocket对象,并能对其触发消息发送功能,我们的定时发送核心功能的实现在与此变量
+     */
+    private static ConcurrentHashMap<String, MonitorSocket> webSocketSet = new ConcurrentHashMap<>();
+
+    /**
+     * 与某个客户端的连接会话,需要通过它来给客户端发送数据
+     */
+    private Session session;
+
+
+    private  String module;
+    /**
+     * 连接建立成功调用的方法
+     * <p>
+     * 类似dwr的onpage方法,参考之前文章中demo有
+     */
+    @OnOpen
+    public void onOpen(@PathParam("module") String module, Session session) {
+        this.session = session;
+        this.module = module;
+        webSocketSet.put(module,this);    //加入set中
+        addOnlineCount();           //在线数加1
+        System.out.println("有新连接加入!当前在线人数为" + getOnlineCount());
+        try {
+            sendMessage("连接已建立成功.");
+        } catch (Exception e) {
+            System.out.println("IO异常");
+        }
+    }
+
+    /**
+     * 连接关闭调用的方法
+     * <p>
+     * 参考dwrsession摧毁方法
+     */
+    @OnClose
+    public void onClose() {
+        webSocketSet.remove(this);  //连接关闭后,将此websocket从set中删除
+        subOnlineCount();           //在线数减1
+        System.out.println("有一连接关闭!当前在线人数为" + getOnlineCount());
+    }
+
+    /**
+     * 收到客户端消息后调用的方法
+     *
+     * @param message 客户端发送过来的消息
+     */
+    @OnMessage
+    public void onMessage(String message, Session session) {
+        System.out.println("来自客户端的消息:" + message);
+    }
+
+    // 错误提示
+    @OnError
+    public void onError(Session session, Throwable error) {
+        System.out.println("发生错误");
+        error.printStackTrace();
+    }
+
+    // 发送消息,在定时任务中会调用此方法
+    public void sendMessage(String message) throws IOException {
+        this.session.getBasicRemote().sendText(message);
+
+    }
+
+
+    public static synchronized int getOnlineCount() {
+        return onlineCount;
+    }
+
+    public static synchronized void addOnlineCount() {
+        MonitorSocket.onlineCount++;
+    }
+
+    public static synchronized void subOnlineCount() {
+        MonitorSocket.onlineCount--;
+    }
+
+
+    public Session getSession() {
+        return session;
+    }
+
+    public void setSession(Session session) {
+        this.session = session;
+    }
+
+    public static ConcurrentHashMap<String, MonitorSocket> getWebSocketSet() {
+        return webSocketSet;
+    }
+    public void setConcurrentHashMap(ConcurrentHashMap webSocketSet) {
+        this.webSocketSet = webSocketSet;
+    }
+}

+ 1 - 1
web/monitor-web/src/main/resources/application-dev.yml

@@ -1,5 +1,5 @@
 server:
-  port: 8086
+  port: 8089
   servlet:
     context-path: /