Skip to content

Commit 7d1a310

Browse files
committed
🎉 spring-boot-demo-websocket 基本完成
1 parent c638209 commit 7d1a310

File tree

20 files changed

+1869
-0
lines changed

20 files changed

+1869
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.xkcoding.websocket.common;
2+
3+
/**
4+
* <p>
5+
* WebSocket常量
6+
* </p>
7+
*
8+
* @package: com.xkcoding.websocket.common
9+
* @description: WebSocket常量
10+
* @author: yangkai.shen
11+
* @date: Created in 2018-12-14 16:01
12+
* @copyright: Copyright (c) 2018
13+
* @version: V1.0
14+
* @modified: yangkai.shen
15+
*/
16+
public interface WebSocketConsts {
17+
String PUSH_SERVER = "/topic/server";
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.xkcoding.websocket.config;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
5+
import org.springframework.web.socket.config.annotation.EnableWebSocket;
6+
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
7+
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
8+
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
9+
10+
/**
11+
* <p>
12+
* WebSocket配置
13+
* </p>
14+
*
15+
* @package: com.xkcoding.websocket.config
16+
* @description: WebSocket配置
17+
* @author: yangkai.shen
18+
* @date: Created in 2018-12-14 15:58
19+
* @copyright: Copyright (c) 2018
20+
* @version: V1.0
21+
* @modified: yangkai.shen
22+
*/
23+
@Configuration
24+
@EnableWebSocket
25+
@EnableWebSocketMessageBroker
26+
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
27+
28+
@Override
29+
public void registerStompEndpoints(StompEndpointRegistry registry) {
30+
// 注册一个 /notification 端点,前端通过这个端点进行连接
31+
registry.addEndpoint("/notification")
32+
//解决跨域问题
33+
.setAllowedOrigins("*")
34+
.withSockJS();
35+
}
36+
37+
@Override
38+
public void configureMessageBroker(MessageBrokerRegistry registry) {
39+
//定义了一个客户端订阅地址的前缀信息,也就是客户端接收服务端发送消息的前缀信息
40+
registry.enableSimpleBroker("/topic");
41+
}
42+
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
package com.xkcoding.websocket.model;
2+
3+
import cn.hutool.core.util.NumberUtil;
4+
import com.xkcoding.websocket.model.server.*;
5+
import com.xkcoding.websocket.util.IpUtil;
6+
import oshi.SystemInfo;
7+
import oshi.hardware.CentralProcessor;
8+
import oshi.hardware.CentralProcessor.TickType;
9+
import oshi.hardware.GlobalMemory;
10+
import oshi.hardware.HardwareAbstractionLayer;
11+
import oshi.software.os.FileSystem;
12+
import oshi.software.os.OSFileStore;
13+
import oshi.software.os.OperatingSystem;
14+
import oshi.util.Util;
15+
16+
import java.net.UnknownHostException;
17+
import java.util.LinkedList;
18+
import java.util.List;
19+
import java.util.Properties;
20+
21+
/**
22+
* <p>
23+
* 服务器相关信息实体
24+
* </p>
25+
*
26+
* @package: com.xkcoding.websocket.model
27+
* @description: 服务器相关信息实体
28+
* @author: yangkai.shen
29+
* @date: Created in 2018-12-14 16:09
30+
* @copyright: Copyright (c) 2018
31+
* @version: V1.0
32+
* @modified: yangkai.shen
33+
*/
34+
public class Server {
35+
36+
private static final int OSHI_WAIT_SECOND = 1000;
37+
38+
/**
39+
* CPU相关信息
40+
*/
41+
private Cpu cpu = new Cpu();
42+
43+
/**
44+
* 內存相关信息
45+
*/
46+
private Mem mem = new Mem();
47+
48+
/**
49+
* JVM相关信息
50+
*/
51+
private Jvm jvm = new Jvm();
52+
53+
/**
54+
* 服务器相关信息
55+
*/
56+
private Sys sys = new Sys();
57+
58+
/**
59+
* 磁盘相关信息
60+
*/
61+
private List<SysFile> sysFiles = new LinkedList<SysFile>();
62+
63+
public Cpu getCpu() {
64+
return cpu;
65+
}
66+
67+
public void setCpu(Cpu cpu) {
68+
this.cpu = cpu;
69+
}
70+
71+
public Mem getMem() {
72+
return mem;
73+
}
74+
75+
public void setMem(Mem mem) {
76+
this.mem = mem;
77+
}
78+
79+
public Jvm getJvm() {
80+
return jvm;
81+
}
82+
83+
public void setJvm(Jvm jvm) {
84+
this.jvm = jvm;
85+
}
86+
87+
public Sys getSys() {
88+
return sys;
89+
}
90+
91+
public void setSys(Sys sys) {
92+
this.sys = sys;
93+
}
94+
95+
public List<SysFile> getSysFiles() {
96+
return sysFiles;
97+
}
98+
99+
public void setSysFiles(List<SysFile> sysFiles) {
100+
this.sysFiles = sysFiles;
101+
}
102+
103+
public void copyTo() throws Exception {
104+
SystemInfo si = new SystemInfo();
105+
HardwareAbstractionLayer hal = si.getHardware();
106+
107+
setCpuInfo(hal.getProcessor());
108+
109+
setMemInfo(hal.getMemory());
110+
111+
setSysInfo();
112+
113+
setJvmInfo();
114+
115+
setSysFiles(si.getOperatingSystem());
116+
}
117+
118+
/**
119+
* 设置CPU信息
120+
*/
121+
private void setCpuInfo(CentralProcessor processor) {
122+
// CPU信息
123+
long[] prevTicks = processor.getSystemCpuLoadTicks();
124+
Util.sleep(OSHI_WAIT_SECOND);
125+
long[] ticks = processor.getSystemCpuLoadTicks();
126+
long nice = ticks[TickType.NICE.getIndex()] - prevTicks[TickType.NICE.getIndex()];
127+
long irq = ticks[TickType.IRQ.getIndex()] - prevTicks[TickType.IRQ.getIndex()];
128+
long softirq = ticks[TickType.SOFTIRQ.getIndex()] - prevTicks[TickType.SOFTIRQ.getIndex()];
129+
long steal = ticks[TickType.STEAL.getIndex()] - prevTicks[TickType.STEAL.getIndex()];
130+
long cSys = ticks[TickType.SYSTEM.getIndex()] - prevTicks[TickType.SYSTEM.getIndex()];
131+
long user = ticks[TickType.USER.getIndex()] - prevTicks[TickType.USER.getIndex()];
132+
long iowait = ticks[TickType.IOWAIT.getIndex()] - prevTicks[TickType.IOWAIT.getIndex()];
133+
long idle = ticks[TickType.IDLE.getIndex()] - prevTicks[TickType.IDLE.getIndex()];
134+
long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal;
135+
cpu.setCpuNum(processor.getLogicalProcessorCount());
136+
cpu.setTotal(totalCpu);
137+
cpu.setSys(cSys);
138+
cpu.setUsed(user);
139+
cpu.setWait(iowait);
140+
cpu.setFree(idle);
141+
}
142+
143+
/**
144+
* 设置内存信息
145+
*/
146+
private void setMemInfo(GlobalMemory memory) {
147+
mem.setTotal(memory.getTotal());
148+
mem.setUsed(memory.getTotal() - memory.getAvailable());
149+
mem.setFree(memory.getAvailable());
150+
}
151+
152+
/**
153+
* 设置服务器信息
154+
*/
155+
private void setSysInfo() {
156+
Properties props = System.getProperties();
157+
sys.setComputerName(IpUtil.getHostName());
158+
sys.setComputerIp(IpUtil.getHostIp());
159+
sys.setOsName(props.getProperty("os.name"));
160+
sys.setOsArch(props.getProperty("os.arch"));
161+
sys.setUserDir(props.getProperty("user.dir"));
162+
}
163+
164+
/**
165+
* 设置Java虚拟机
166+
*/
167+
private void setJvmInfo() throws UnknownHostException {
168+
Properties props = System.getProperties();
169+
jvm.setTotal(Runtime.getRuntime().totalMemory());
170+
jvm.setMax(Runtime.getRuntime().maxMemory());
171+
jvm.setFree(Runtime.getRuntime().freeMemory());
172+
jvm.setVersion(props.getProperty("java.version"));
173+
jvm.setHome(props.getProperty("java.home"));
174+
}
175+
176+
/**
177+
* 设置磁盘信息
178+
*/
179+
private void setSysFiles(OperatingSystem os) {
180+
FileSystem fileSystem = os.getFileSystem();
181+
OSFileStore[] fsArray = fileSystem.getFileStores();
182+
for (OSFileStore fs : fsArray) {
183+
long free = fs.getUsableSpace();
184+
long total = fs.getTotalSpace();
185+
long used = total - free;
186+
SysFile sysFile = new SysFile();
187+
sysFile.setDirName(fs.getMount());
188+
sysFile.setSysTypeName(fs.getType());
189+
sysFile.setTypeName(fs.getName());
190+
sysFile.setTotal(convertFileSize(total));
191+
sysFile.setFree(convertFileSize(free));
192+
sysFile.setUsed(convertFileSize(used));
193+
sysFile.setUsage(NumberUtil.mul(NumberUtil.div(used, total, 4), 100));
194+
sysFiles.add(sysFile);
195+
}
196+
}
197+
198+
/**
199+
* 字节转换
200+
*
201+
* @param size 字节大小
202+
* @return 转换后值
203+
*/
204+
public String convertFileSize(long size) {
205+
long kb = 1024;
206+
long mb = kb * 1024;
207+
long gb = mb * 1024;
208+
if (size >= gb) {
209+
return String.format("%.1f GB", (float) size / gb);
210+
} else if (size >= mb) {
211+
float f = (float) size / mb;
212+
return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
213+
} else if (size >= kb) {
214+
float f = (float) size / kb;
215+
return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
216+
} else {
217+
return String.format("%d B", size);
218+
}
219+
}
220+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.xkcoding.websocket.model.server;
2+
3+
import cn.hutool.core.util.NumberUtil;
4+
5+
/**
6+
* <p>
7+
* CPU相关信息实体
8+
* </p>
9+
*
10+
* @package: com.xkcoding.websocket.model.server
11+
* @description: CPU相关信息实体
12+
* @author: yangkai.shen
13+
* @date: Created in 2018-12-14 16:09
14+
* @copyright: Copyright (c) 2018
15+
* @version: V1.0
16+
* @modified: yangkai.shen
17+
*/
18+
public class Cpu {
19+
/**
20+
* 核心数
21+
*/
22+
private int cpuNum;
23+
24+
/**
25+
* CPU总的使用率
26+
*/
27+
private double total;
28+
29+
/**
30+
* CPU系统使用率
31+
*/
32+
private double sys;
33+
34+
/**
35+
* CPU用户使用率
36+
*/
37+
private double used;
38+
39+
/**
40+
* CPU当前等待率
41+
*/
42+
private double wait;
43+
44+
/**
45+
* CPU当前空闲率
46+
*/
47+
private double free;
48+
49+
public int getCpuNum() {
50+
return cpuNum;
51+
}
52+
53+
public void setCpuNum(int cpuNum) {
54+
this.cpuNum = cpuNum;
55+
}
56+
57+
public double getTotal() {
58+
return NumberUtil.round(NumberUtil.mul(total, 100), 2)
59+
.doubleValue();
60+
}
61+
62+
public void setTotal(double total) {
63+
this.total = total;
64+
}
65+
66+
public double getSys() {
67+
return NumberUtil.round(NumberUtil.mul(sys / total, 100), 2)
68+
.doubleValue();
69+
}
70+
71+
public void setSys(double sys) {
72+
this.sys = sys;
73+
}
74+
75+
public double getUsed() {
76+
return NumberUtil.round(NumberUtil.mul(used / total, 100), 2)
77+
.doubleValue();
78+
}
79+
80+
public void setUsed(double used) {
81+
this.used = used;
82+
}
83+
84+
public double getWait() {
85+
return NumberUtil.round(NumberUtil.mul(wait / total, 100), 2)
86+
.doubleValue();
87+
}
88+
89+
public void setWait(double wait) {
90+
this.wait = wait;
91+
}
92+
93+
public double getFree() {
94+
return NumberUtil.round(NumberUtil.mul(free / total, 100), 2)
95+
.doubleValue();
96+
}
97+
98+
public void setFree(double free) {
99+
this.free = free;
100+
}
101+
}

0 commit comments

Comments
 (0)