晨曦's Blog

This is a window to the soul

说在前面

压缩页面无非就是减小页面大小加快博客访问时间

实现

安装依赖

1
npm i gulp gulp-htmlclean gulp-htmlmin gulp-minify-css gulp-uglify -s

gulp 配置

在根目录下创建 gulpfile.js 文件

gulpfile.js

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
var gulp = require('gulp');
var minifycss = require('gulp-minify-css');
var uglify = require('gulp-uglify');
var htmlmin = require('gulp-htmlmin');
var htmlclean = require('gulp-htmlclean');
// 压缩 public 目录 css
gulp.task('minify-css', function () {
return gulp.src('./public/**/*.css')
.pipe(minifycss())
.pipe(gulp.dest('./public'));
});
// 压缩 public 目录 html
gulp.task('minify-html', function () {
return gulp.src('./public/**/*.html')
.pipe(htmlclean())
.pipe(htmlmin({
removeComments: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
}))
.pipe(gulp.dest('./public'))
});
// 压缩 public/js 目录 js
gulp.task('minify-js', function () {
return gulp.src('./public/**/*.js')
.pipe(uglify())
.pipe(gulp.dest('./public'));
});
// 执行 gulp 命令时执行的任务
gulp.task('default', [
'minify-html', 'minify-css', 'minify-js'
]);

一键部署

package.json 中加入如下 script

1
2
3
"scripts": {
"push": "hexo cl && hexo g && gulp && hexo d"
}

然后在部署的时候只需要运行

1
npm run push

前因

版本:NexT.Pisces v6.6.0
Error: 阅读次数:Counter not initialized! See more at console err msg.

解决

出现上述之错误,遂查看配置文件之说明

1
2
3
4
5
6
7
8
9
10
11
# Show number of visitors to each article.
# You can visit https://leancloud.cn get AppID and AppKey.
leancloud_visitors:
enable: true
app_id: <<your app id>>
app_key: <<your app key>>
# Dependencies: https://github.com/theme-next/hexo-leancloud-counter-security
# If you don't care about security in lc counter and just want to use it directly
# (without hexo-leancloud-counter-security plugin), set the `security` to `false`.
security: true
betterPerformance: false

如上述两个解决办法:

  1. 安装 hexo-leancloud-counter-security 并设置 leancloudapp_idapp_key

  2. security 设置为 security: false

因为懒所以选择了第二方案。

简介

Beanstalkd - 一个高性能、轻量级的分布式内存队列系统。

英文协议

中文协议

安装

这里我们用 Docker 来运行。

1
2
docker pull schickling/beanstalkd
docker run -d -p 11300:11300 schickling/beanstalkd

beanstalkd 管理 WEB 平台,主要用来看看以后的任务详情。

1
2
docker pull schickling/beanstalkd-console
docker run -d -p 2080:2080 --link beanstalkd:beanstalkd schickling/beanstalkd-console

通过 127.0.0.1:2080 访问

使用

官方客户端推荐列表 Client

下面我们主要介绍 Nodejs 客户端 bsw

客户端安装

1
npm i bsw --save

客户端示例

这里我们用阿里开源框架 eggjs 做测试。
app.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
'use strict';
const { Consumer } = require('bsw');
module.exports = app => {
app.beforeStart(async () => {
const consumer = new Consumer({
host: 'localhost',
port: '11300',
tube: 'node',
async handler(payload, job_info) {
console.log('processing job: ', payload);
console.log('processing job_info: ', job_info);
// 这里进行业务操作
return 'success';
},
});
consumer.on('error', e => {
console.log(e);
});
console.log('beanstalkd启动了');
await consumer.start();
});
};

home.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
'use strict';
const Controller = require('egg').Controller;
const { Producer } = require('bsw');
class HomeController extends Controller {
async index() {
const producer = new Producer({
host: 'localhost',
port: '11300',
tube: 'node',
});
await producer.start();
await producer.putJob({
payload: JSON.stringify({ throw: true, result: 'success' }),
priority: 0, // 优先级
delay: 30, // 延时单位(s)
ttr: 60, // 允许worker执行的最大秒数
});
producer.stop();
this.ctx.body = 'hi, egg';
}
}
module.exports = HomeController;

天若有情天亦老,月如无恨月长圆

commons-lang3

概述

Apache Commons-Lang3 库提供了 JavaAPI 的核心类的操作支持。此支持包括处理字符串,数字,日期,并发,对象反射等的方法。

Maven 依赖

要使用 commons-lang3 库,只需使用以下依赖项从中央 Maven 存储库中提取它

1
2
3
4
5
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>

示例、文档参考

文档参考:StringUtils

Hutool

概述

Hutool 是一个 Java 工具包,也只是一个工具包,它帮助我们简化每一行代码,减少每一个方法,让 Java 语言也可以 “甜甜的”。

Maven 依赖

1
2
3
4
5
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.2.1</version>
</dependency>

示例、文档参考

文档参考:Hutool

easy-okhttp

概述

easy-okhttp 是对 OkHttp 网络框架封装,提供文件上传和下载,表单 (含文件) 提交,链式调用,支持 HTTPS 和自定义签名证书等特性。

Maven 依赖

1
2
3
4
5
<dependency>
<groupId>com.mzlion</groupId>
<artifactId>easy-okhttp</artifactId>
<version>1.1.0</version>
</dependency>

示例、文档参考

文档参考:easy-okhttp

查看 docker 磁盘使用情况

1
docker system df

kill 掉所有容器

1
docker kill $(docker ps -a -q)

删除所有停止容器

1
docker rm $(docker ps -a -q)

删除所有镜像

1
docker rmi $(docker images -q)

强制删除镜像

当同一镜像被多个储存库引用时,就需要强制删除镜像

1
docker rmi -f 镜像ID

删除 tag 为 <none> 的所有镜像

1
2
3
docker images|grep none|awk '{print $3}'|xargs docker rmi

docker rmi $(docker images -f "dangling=true" -q)

删除已经 PUSH 带有 DIGEST 参数镜像

1
2
3
4
5
$ docker images --digests
REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
localhost:5000/nodeservice <none> sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf 4986bf8c1536 9 weeks ago 2.43 MB

$ docker rmi localhost:5000/nodeservice@sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf

参考:[Docker 命令](https://docs.docker.com/engine/reference/commandline/cli/)

前记

公司部分业务使用的是阿里的 docker swarm 服务,在配置 nodejs 应用时发现时区不对。

处理

记录下自己所选择的方式。

我选择在 Dockerfile 文件中做处理,这样利于以后所有引用此镜像的容器时区的正确性。

1
2
RUN echo "Asia/Shanghai" > /etc/timezone \
&& dpkg-reconfigure -f noninteractive tzdata

我们不生产 k8s 管理平台,我们只是开源项目的搬运工

Rancher

Rancher 是一个开源的企业级容器管理平台。通过 Rancher,企业再也不必自己使用一系列的开源软件去从头搭建容器服务平台。Rancher 提供了在生产环境中使用的管理 Docker 和 Kubernetes 的全栈化容器部署与管理平台。

Kubernetes Dashboard

Kubernetes Dashboard 是 Kubernetes 集群的基于 Web 的通用 UI。它允许用户管理在群集中运行的应用程序并对其进行故障排除,以及管理群集本身。

Wayne

Wayne 是一个通用的、基于 Web 的 Kubernetes 多集群管理平台。通过可视化 Kubernetes 对象模板编辑的方式,降低业务接入成本, 拥有完整的权限管理系统,适应多租户场景,是一款适合企业级集群使用的发布平台。

Naftis

Naftis 是一个基于 web 的 Istio dashboard,通过任务模板的方式来帮助用户更方便地执行 Istio 任务。 用户可以在 Naftis 中定义自己的任务模板,并填充变量来构造单个或多个构造任务实例,从而完成各种服务治理功能。

Breeze

Breeze 是睿云智合提供的图形化 Kubernetes 部署工具。

Nacos

Nacos 是阿里巴巴的新开源项目,其核心定位是 “一个更易于帮助构建云原生应用的动态服务发现、配置和服务管理平台”。

Nacos 可能更多的是为了支持一整套微服务。包括 DubboSpring Cloud 以及 Kubernetes 等。


最后相对来说,我还是比较喜欢用 Rancher

前记

毫无疑问,Mybatis-Plus 是优秀的。
Mybatis-Plus 官网的文档中,自定义分页只能只支持 xml 方式,但是鉴于本人对 xml 并不是太喜欢,而是喜欢用注解的方式。

于是,记录了在 Mybatis-Plus 中注解分页方式实现。

实现

首先讲一下我的请求:

1
controller->service->mapper->entity

UserService.java

1
2
3
4
5
6
7
8
9
10
public Object getAll() {
Page userList = new Page<>(1, 5);
Map<String, Object> param = new HashMap<>();
param.put("name", "测试788");
param.put("page", userList);
List<Map<String, Object>> userListArr = mapper.getAllUserGoodsSQLPage(param);
userList.setRecords(userListArr);

return userList;
}

UserMapper.java

1
2
3
4
5
6
7
8
9
@Select("<script>SELECT u.id,u.name from " + TABLE + " as u " +
"left join " + G_TABLE + " as g " +
"on u.id = g.uid " +
"where u.name = #{name}" +
"<if test = 'id != null'>" +
" and u.id = #{id}" +
"</if>" +
"</script>")
List<Map<String, Object>> getAllUserGoodsSQLPage(Map<String, Object> param);

JSON

1、查询数组中对象

JSON_CONTAINS 用法

1
2
3
4
5
select * from circulation
where JSON_CONTAINS(goods->'$[*].name', '["海带结(香辣味)150克/袋"]', '$')

select * from circulation
where JSON_CONTAINS(goods->'$[*].gid', '[20]', '$')

2、Json 字符串转化为 Json 对象

CONVERT 用法

1
select CONVERT('{"mail": "amy@gmail.com", "name": "Amy"}',JSON)

3、元素转化为数组

JSON_ARRAY 用法

1
SELECT JSON_ARRAY(1, "abc", NULL, TRUE, CURTIME())

4、数组中查询元素

JSON_CONTAINS + JSON_ARRAY 用法

1
select JSON_CONTAINS(JSON_ARRAY(1,2,3,4,5,6),'6')

5、数组模糊查询

JSON_SEARCH用法

1
select * from table where JSON_SEARCH(profiles, 'all', 'sales%') is not null

6、JSON_SET 修改数据

1
2
3
4
5
6
--- 数组
set id = JSON_SET(id,'$[0]',1)

--- Json对象
set info ='{}'
set info =JSON_SET(info,'$."1"',"{}")

7、利用 JSON_CONTAINS 实现 On

1
" LEFT JOIN `ag_user_group` as ug  on JSON_CONTAINS(u.user_group_id, JSON_ARRAY(ug.id), '$')"

user_group_id 为数组,ug.idint 格式

常规

1、查询重复记录

1
2
select * from merchant
where num in (select num from synk.merchant group by num having count(num) > 1)

统计

1、count 与 distinct 结合使用,限制条件的情况下去除重复数据

1
2
SELECT count(distinct id, if(id is not null,true,null)) 
FROM flow_view;
0%