安装
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
maven-jar-plugin
和maven-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
运行程序.
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
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
用来清除数据和表.
host
、port
、db
、用户名
、密码
等信息替换成自己的.
# 准备表和数据
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<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}}"));
使用方式:执行时传入参数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