0%

IDEA快捷键

快捷键 说明
Shift + Ctrl + V 选择要粘贴的内容
Ctrl + Alt + L 重新格式化代码
Ctrl + F12 显示文件结构
Ctrl + ` 更改视图模式
Ctrl + G 转到行:列
Ctrl -/+ 收起和展开代码块
Alt + Shift + ,/. 缩小/放大字体
  • https://blog.jetbrains.com/zh-hans/idea/2022/11/intellij-idea-3/

VS Code快捷键

快捷键 说明
Shift + Ctrl + K 删除一行
Shift + Alt + ↑/↓ 向上/向下复制一行
Alt + ↑/↓ 向上/向下移动一行
Shift + Alt + f 代码格式化
Ctrl + Shift + n 新建一个窗口
Ctrl + ]/[ 行增加/减少缩进
Ctrl -/+ 字体放大/减小
Ctrl + b 显示/隐藏左侧目录栏
Ctrl + ~ 打开/关闭终端
Ctrl + Shift + ~ 打开新的终端
Ctrl + Shift + c 当前目录打开cmd窗口

说明

什么是准入 Webhook?

准入 Webhook 是一种用于接收准入请求并对其进行处理的 HTTP 回调机制。 可以定义两种类型的准入 Webhook, 即验证性质的准入 Webhook 和变更性质的准入 Webhook。 变更性质的准入 Webhook 会先被调用。它们可以修改发送到 API 服务器的对象以执行自定义的设置默认值操作。

Admission Webhook

包涵两种 CRD:mutatingwebhookconfigurationvalidatingwebhookconfiguration.

MutatingWebhookConfiguration 描述准入 Webhook 的配置,该 Webhook 可接受或拒绝对象请求,并且可能变更对象。

ValidatingWebhookConfiguration 描述准入 Webhook 的配置,该 Webhook 可在不更改对象的情况下接受或拒绝对象请求。

引用

说明

RestTemplate是Spring提供的用于访问Rest服务的客户端,可以通过ClientHttpRequestFactory创建。默认采用的是SimpleClientHttpRequestFactory,底层是HttpURLConnection(java.net包提供的方式),不具备连接池的功能,默认超时为-1。可以引入httpClient连接池

使用

  • 添加依赖
 <dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.13</version> <!-- 版本号可能会不同,根据实际情况选择 -->
</dependency>
  • 配置
@Configuration
public class RestTemplateConfig {
    @Bean
    public RestTemplate restTemplate() {
        ClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient());
        return new RestTemplate(factory);
    }

    private CloseableHttpClient httpClient() {
        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", SSLConnectionSocketFactory.getSocketFactory())
                .build();
        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
        connectionManager.setMaxTotal(50);
        connectionManager.setDefaultMaxPerRoute(50);
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(30000)
                .setConnectTimeout(10000)
                .setConnectionRequestTimeout(10000)
                .build();
        return HttpClientBuilder.create()
                .setDefaultRequestConfig(requestConfig)
                .setConnectionManager(connectionManager)
                .build();
    }
}

安装

yum -y install httpd-tools

使用

# -n 请求10000次
# -c 100个线程并发
# -T -p post请求,请求body放在了data.txt中
ab -n 10000 -c 100 -T application/json -p data.txt http://172.16.20.35:8080/v1/get_token

打包成可执行的jar

方法一:使用maven-jar-pluginmaven-dependency-plugin

<build>
    <plugins>
        <!-- maven-jar-plugin的作用是配置mainClass和指定classpath -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.2.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>libs/</classpathPrefix>
                        <mainClass> <!-- mainClass替换成实际的 -->
                            com.test.App
                        </mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <!-- maven-dependency-plugin会把所依赖的jar包copy到指定目录 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>
                            ${project.build.directory}/libs
                        </outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

方法二:使用maven-assembly-plugin

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.3.0</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass> <!-- mainClass替换成实际的 -->
                                    com.test.App
                                </mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

方法三:使用spring-boot-maven-plugin

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.7.9</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                    <configuration>
                        <classifier>spring-boot</classifier>
                        <mainClass>
                            com.deri.App
                        </mainClass>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

总结

使用上述任一方法,通过mvn clean package命令打包后, 可以直接java -jar xxxx.jar运行程序.

sysbench安装

官方文档

  • 安装依赖
yum -y install make automake libtool pkgconfig libaio-devel
# For MySQL support, replace with mysql-devel on RHEL/CentOS 5
yum -y install mariadb-devel openssl-devel
# For PostgreSQL support
yum -y install postgresql-devel
  • RHEL/CentOS 安装
curl -s https://packagecloud.io/install/repositories/akopytov/sysbench/script.rpm.sh | sudo bash
sudo yum -y install sysbench

压测

  • 只读示例
./bin/sysbench --test=./share/tests/db/oltp.lua \
--mysql-host=10.0.201.36 --mysql-port=8066 --mysql-user=ecuser --mysql-password=ecuser \
--mysql-db=dbtest1a --oltp-tables-count=10 --oltp-table-size=500000 \
--report-interval=10 --oltp-dist-type=uniform --rand-init=on --max-requests=0 \
--oltp-test-mode=nontrx --oltp-nontrx-mode=select \
--oltp-read-only=on --oltp-skip-trx=on \
--max-time=120 --num-threads=12 \
[prepare|run|cleanup]

注意最后一行,一项测试开始前需要用prepare来准备好表和数据,run执行真正的压测,cleanup用来清除数据和表.

测试命令

hostportdb用户名密码等信息替换成自己的.

# 准备表和数据
sysbench --db-driver=mysql /usr/share/sysbench/oltp_read_only.lua --mysql-host=127.0.0.1 --mysql-port=3306  --mysql-db=sbtest --mysql-user=root --mysql-password=123456 --tables=16 --table_size=500000 --threads=128 --time=60 --report-interval=1 prepare
# 压测
sysbench --db-driver=mysql /usr/share/sysbench/oltp_read_only.lua --mysql-host=127.0.0.1 --mysql-port=3306  --mysql-db=sbtest --mysql-user=root --mysql-password=123456 --tables=16 --table_size=500000 --threads=128 --time=60 --report-interval=1 run
# 清理数据
sysbench --db-driver=mysql /usr/share/sysbench/oltp_read_only.lua --mysql-host=127.0.0.1 --mysql-port=3306  --mysql-db=sbtest --mysql-user=root --mysql-password=123456 --tables=16 --table_size=500000 --threads=128 --time=60 --report-interval=1 cleanup