乙巳🐍年

acc8226 的博客

er (实体联系)图

属于概念模型

实体 椭圆
属性 矩形
关系 菱形

E-R 图也称实体-联系图(Entity Relationship Diagram),提供了表示实体类型、属性和联系的方法,用来描述现实世界的概念模型

阅读全文 »

sql where 1 = 1 和 0 = 1 的作用

where 1=1; 这个条件始终为 True,在不定数量查询条件情况下,1=1 可以很方便的规范语句。
where 1=0; 这个条件始终为 false,结果不会返回任何数据,只有表结构,可用于快速建表

“SELECT * FROM strName WHERE 1 = 0”; 该 select 语句主要用于读取表的结构而不考虑表中的数据,这样节省了内存,因为可以不用保存结果集。

用法,可用于创建一个新表,而新表的结构与查询的表的结构是一样的。

阅读全文 »

环境的文档
测试的文档
进行压测,把数据库搞崩

什么是 Citus

Citus 数据库是 Postgres 的一个开源扩展,可以让你在任何规模上都感受到 Postgres 的伟大ーー从一个节点到一个庞大的分布式数据库集群。由于 Citus 是 Postgres 的一个扩展(而非分支) ,因此当您使用 Citus 时,也在使用 Postgres。您可以利用最新的 Postgres 特性、工具和生态系统。

使用 Citus,您可以获得分布式 Postgres 功能,比如分片、分布式表、引用表、分布式查询引擎、柱状存储—到 Citus 11.0 为止,您可以从任何节点进行查询。Citus 将并行性、在内存中保存更多数据以及更高的 I/O 带宽结合在一起,可以显著提高多租户 SaaS 应用程序、面向客户的实时分析仪表板以及时间序列工作负载的性能。

获得 Citus 的两种方法:

  • 开源: Citus 是 100% 开源的。您可以下载 Citus 开放源码,或者访问 GitHub 上的 Citus repo 来查看源代码并自己构建它。(分为社区版和商业版)
  • 托管服务: Citus 数据库可以作为云中的托管服务使用,Azure Cosmos DB 用于 PostgreSQL,以前在 Azure 数据库中称为 Hyperscale (Citus)用于 PostgreSQL。
阅读全文 »

springboot 的内置 tomcat 时,就不需要引入 javaee-api 了,spring-boot 已经包含了。使用 springboot 的 websocket 功能首先引入 springboot 组件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
plugins {
java
id("org.springframework.boot") version "2.7.9"
id("io.spring.dependency-management") version "1.0.15.RELEASE"
}

group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_17

repositories {
mavenCentral()
}

dependencies {
implementation("org.springframework.boot:spring-boot-starter-websocket")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}

tasks.withType<Test> {
useJUnitPlatform()
}

使用 @ServerEndpoint创立websocket endpoint

首先要注入 ServerEndpointExporter,这个 bean 会自动注册使用了 @ServerEndpoint 注解声明的 Websocket endpoint。要注意,如果使用独立的 servlet 容器,而不是直接使用 springboot 的内置容器,就不要注入 ServerEndpointExporter,因为它将由容器自己提供和管理。

1
2
3
4
5
6
7
8
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}

}

接下来就是写 websocket 的具体实现类,很简单,直接上代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.example.demo;

import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;

@ServerEndpoint(value = "/websocket")
@Component
public class MyWebSocket {
//静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
private static int onlineCount = 0;

//concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。
private static CopyOnWriteArraySet<MyWebSocket> webSocketSet = new CopyOnWriteArraySet<MyWebSocket>();

//与某个客户端的连接会话,需要通过它来给客户端发送数据
private Session session;

/**
* 连接建立成功调用的方法
*/
@OnOpen
public void onOpen(Session session) {
this.session = session;
webSocketSet.add(this); //加入set中
addOnlineCount(); //在线数加1
System.out.println("有新连接加入!当前在线人数为" + getOnlineCount());
try {
sendMessage("on open------------------");
} catch (IOException e) {
System.out.println("IO异常");
}
}

/**
* 连接关闭调用的方法
*/
@OnClose
public void onClose() {
webSocketSet.remove(this); //从set中删除
subOnlineCount(); //在线数减1
System.out.println("有一连接关闭!当前在线人数为" + getOnlineCount());
}

/**
* 收到客户端消息后调用的方法
*
* @param message 客户端发送过来的消息
*/
@OnMessage
public void onMessage(String message, Session session) {
System.out.println("来自客户端的消息:" + message);

//群发消息
for (MyWebSocket item : webSocketSet) {
try {
item.sendMessage(message);
} catch (IOException e) {
e.printStackTrace();
}
}
}


@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);
//this.session.getAsyncRemote().sendText(message);
}


/**
* 群发自定义消息
*/
public static void sendInfo(String message) throws IOException {
for (MyWebSocket item : webSocketSet) {
try {
item.sendMessage(message);
} catch (IOException e) {
continue;
}
}
}

public static synchronized int getOnlineCount() {
return onlineCount;
}

public static synchronized void addOnlineCount() {
MyWebSocket.onlineCount++;
}

public static synchronized void subOnlineCount() {
MyWebSocket.onlineCount--;
}
}

3、前端代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<!DOCTYPE HTML>
<html>

<head>
<title>My WebSocket</title>
</head>

<body>
Welcome<br />
<input id="text" type="text" /><button onclick="send()">Send</button> <button
onclick="closeWebSocket()">Close</button>
<div id="message">
</div>
</body>

<script type="text/javascript">
var websocket = null;
// 判断当前浏览器是否支持WebSocket
if ('WebSocket' in window) {
websocket = new WebSocket("ws://localhost:8080/websocket");
}
else {
alert('Not support websocket')
}

// 连接发生错误的回调方法
websocket.onerror = function () {
setMessageInnerHTML("error");
};

// 连接成功建立的回调方法
websocket.onopen = function (event) {
setMessageInnerHTML("open");
}

// 接收到消息的回调方法
websocket.onmessage = function (event) {
setMessageInnerHTML(event.data);
}

// 连接关闭的回调方法
websocket.onclose = function () {
setMessageInnerHTML("close");
}

// 监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
window.onbeforeunload = function () {
websocket.close();
}

// 将消息显示在网页上
function setMessageInnerHTML(innerHTML) {
document.getElementById('message').innerHTML += innerHTML + '<br/>';
}

// 关闭连接
function closeWebSocket() {
websocket.close();
}

// 发送消息
function send() {
var message = document.getElementById('text').value;
websocket.send(message);
}
</script>

</html>

总结
springboot 已经做了深度的集成和优化,要注意是否添加了不需要的依赖、配置或声明。由于很多讲解组件使用的文章是和 spring 集成的,会有一些配置,在使用 springboot 时,由于 springboot 已经有了自己的配置,再这些配置有可能导致各种各样的异常。

参考

spring boot Websocket(使用笔记) - 长乐忘忧 - 博客园
https://www.cnblogs.com/bianzy/p/5822426.html

全平台

  1. 【全平台】每日英语听力 可用于练习听力
  2. 【全平台】欧路词典

Bob(Mac)

Bob 是一款 Mac 端翻译软件,支持划词翻译、截图翻译以及手动输入翻译

目前支持

  • 划词翻译
  • 截图翻译
  • 输入翻译
  • 翻译多开
  • 自定义插件
  • 自动识别语种
  • 驼峰拆分、蛇形拆分
  • AppleScript 调用
  • PopClip 调用
阅读全文 »
0%