0%

安装

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

依赖

Apache StrSubstitutor使用占位符.

 <dependency>
    <groupId>org.apache.commons</groupId>
     <artifactId>commons-lang3</artifactId>
     <version>3.4</version>
 </dependency>

使用

  • 直接获取系统属性
System.out.println(StrSubstitutor.replaceSystemProperties("You are running with java.version = ${java.version} and os.name = ${os.name}."));
  • 使用map
Map<String, String> params = new HashMap<>();
params.put("name", "happywzy");
params.put("age", "15");
String temp = "你好,${name}! 你今年${age}岁啦!";
System.out.println(StrSubstitutor.replace(temp, params));
  • 递归替换变量
Map<String, Object> params = new HashMap<>();
params.put("name", "${x}");
params.put("x", "happywzy");
StrSubstitutor strSubstitutor = new StrSubstitutor(params);
String name = "${name}";
System.out.println(strSubstitutor.replace(name));
  • 嵌套替换变量
Map<String, Object> params = new HashMap<>();
params.put("jre-1.8", "java-version-1.8");
params.put("java.specification.version", "1.8");
StrSubstitutor strSubstitutor = new StrSubstitutor(params);
strSubstitutor.setEnableSubstitutionInVariables(true);
System.out.println(strSubstitutor.replace("${jre-${java.specification.version}}"));

区别

在 SpringBoot 项目中,启动时,通过 -D-- 添加参数,都可以修改 yml 或 properties 配置文件中的同名配置,如果不存在则相当于添加了一个配置。

  • -D: 虚拟机参数, 修改 jvm 属性
  • --: 命令行参数, 修改操作系统环境变量
  • 同时存在, 以命令行参数优先。

用法

# 放在jar前
java -Dserver.port=1234 -jar test.jar
# 放在jar后
java -jar test.jar --server.port=1234

# 都可以在代码里通过 System.getProperty("server.port") 获取到.

使用方式:执行时传入参数namespace,则会将该namespace下所有资源通过yaml方式备份

NAMESPACE=$1

deployments=$(kubectl get deployments -n "$NAMESPACE" |awk 'NR>1  {print $1}')

for deployment in $deployments; do
    mkdir -p $NAMESPACE/deploy
    output_file="$NAMESPACE/deploy/$deployment.yaml"
    kubectl get deployment "$deployment" -n "$NAMESPACE" -o yaml > "$output_file"
    echo "Downloaded $deployment.yaml"
done


services=$(kubectl get service -n "$NAMESPACE" |awk 'NR>1  {print $1}')

for service in $services; do
    mkdir -p $NAMESPACE/service
    output_file="$NAMESPACE/service/$service.yaml"
    kubectl get service "$service" -n "$NAMESPACE" -o yaml > "$output_file"
    echo "Downloaded $service.yaml"
done


services=$(kubectl get ConfigMap -n "$NAMESPACE" |awk 'NR>1  {print $1}')

for service in $services; do
    mkdir -p $NAMESPACE/cm 
    output_file="$NAMESPACE/cm/$service.yaml"
    kubectl get ConfigMap "$service" -n "$NAMESPACE" -o yaml > "$output_file"
    echo "Downloaded $service.yaml"
done



services=$(kubectl get Secret -n "$NAMESPACE" |awk 'NR>1  {print $1}')

for service in $services; do
    mkdir -p $NAMESPACE/secret
    output_file="$NAMESPACE/secret/$service.yaml"
    kubectl get Secret "$service" -n "$NAMESPACE" -o yaml > "$output_file"
    echo "Downloaded $service.yaml"
done


services=$(kubectl get ingress -n "$NAMESPACE" |awk 'NR>1  {print $1}')

for service in $services; do
    mkdir -p $NAMESPACE/ingress
    output_file="$NAMESPACE/ingress/$service.yaml"
    kubectl get Secret "$service" -n "$NAMESPACE" -o yaml > "$output_file"
    echo "Downloaded $service.yaml"
done