0%

打包成可执行的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

依赖

<!--mqtt相关依赖-->
<dependency>
    <groupId>org.springframework.integration</groupId>
    <artifactId>spring-integration-stream</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-integration</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.integration</groupId>
    <artifactId>spring-integration-mqtt</artifactId>
</dependency>

配置

  • application.yml
spring:
  mqtt:
    #tcp://127.0.0.1:1883,tcp://192.168.60.133:1883
    url: tcp://192.168.3.27:1883
    username: root
    password: 123456
    clientId: client-test
    cleanSession: false
    qos: 2
    retained: false
    connectionTimeout: 60
    keepAliveInterval: 30
    subscribeTopics:
      - test/#
      - topic/+
      - top/+/iic
  • MqttConfig.java
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @ClassName: MqttConfig
 * @Description: TODO
 * @Author: wuzhiyong
 * @Time: 2023/4/13 14:37
 * @Version: v1.0
 **/
@ConfigurationProperties(prefix = "spring.mqtt")
@Data
@Configuration
public class MqttConfig {
    private String url;
    private String username;
    private String password;
    private String clientId;
    ///是否清空session,设置false表示服务器会保留客户端的连接记录(订阅主题,qos),
    // 客户端重连之后能获取到服务器在客户端断开连接期间推送的消息
    //设置为true表示每次连接服务器都是以新的身份
    private boolean cleanSession = false;
    //QoS 0,最多交付一次。
    //QoS 1,至少交付一次。
    //QoS 2,只交付一次。
    private int qos = 2;
    //发布者发布消息时,如果 Retained 标记被设置为 true,则该消息即是 MQTT 中的保留消息(Retained Message)。
    // MQTT 服务器会为每个主题存储最新一条保留消息,以方便消息发布后才上线的客户端在订阅主题时仍可以接收到该消息。
    private boolean retained = true;
    // 连接超时
    private int connectionTimeout = 60;
    // 设置心跳时间 单位为秒
    private int keepAliveInterval = 30;
    // 默认订阅的主题
    private List<String> subscribeTopics;
}

连接/发布/订阅/重连

  • MqttService.java
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

/**
 * @ClassName: MqttService
 * @Description: TODO
 * @Author: wuzhiyong
 * @Time: 2023/4/13 14:39
 * @Version: v1.0
 **/
@Slf4j
@Service
public class MqttService {
    @Autowired
    MqttConfig config;
    private MqttClient client;

    @PostConstruct
    public void init() {
        connect();
    }

    public void connect() {
        try {
            client = new MqttClient(config.getUrl(), config.getClientId(), new MemoryPersistence());
            MqttConnectOptions options = new MqttConnectOptions();
            options.setCleanSession(config.isCleanSession());
            // 设置自动重连
            options.setAutomaticReconnect(true);
            options.setUserName(config.getUsername());
            options.setPassword(config.getPassword().toCharArray());
            options.setConnectionTimeout(config.getConnectionTimeout());
            options.setKeepAliveInterval(config.getKeepAliveInterval());
            //设置遗嘱消息的话题,若客户端和服务器之间的连接意外断开,服务器将发布客户端的遗嘱信息
//            options.setWill("willTopic", (config.getClientId() + "与服务器断开连接").getBytes(), 0, false);
            client.setCallback(new MqttCallBack(config, client));
            client.connect(options);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void send(String topic, String message) {
        MqttMessage mqttMessage = new MqttMessage();
        mqttMessage.setQos(config.getQos());
        mqttMessage.setRetained(config.isRetained());
        mqttMessage.setPayload(message.getBytes());
        MqttTopic mqttTopic = client.getTopic(topic);
        MqttDeliveryToken token;
        try {
            token = mqttTopic.publish(mqttMessage);
            token.waitForCompletion();
            log.info("消息ID: {}, topic: {}, message {} 已发送.", token.getMessageId(), topic, message);
        } catch (MqttException e) {
            log.info("topic: {} message {} 发送异常.", topic, message);
            e.printStackTrace();
        }
    }
}
  • MqttCallBack.java
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallbackExtended;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttMessage;

/**
 * @ClassName: MqttCallBack
 * @Description: TODO
 * @Author: wuzhiyong
 * @Time: 2023/4/13 15:04
 * @Version: v1.0
 **/
@Slf4j
@Data
public class MqttCallBack implements MqttCallbackExtended {
    private MqttConfig config;
    private MqttClient client;

    public MqttCallBack(MqttConfig config, MqttClient client) {
        this.config = config;
        this.client = client;
    }

    @Override
    public void connectionLost(Throwable throwable) {
        log.error("{} 断开连接.", config.getClientId());
    }

    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        String msg = new String(message.getPayload());
        log.info("收到 {} 消息 {}.", topic, msg);
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {
        log.info("消息ID: {} 发送成功.", token.getMessageId());
    }

    @Override
    public void connectComplete(boolean b, String url) {
        log.info("MQTT {} 连接 {} 成功.", config.getClientId(), url);
        try {
            if (client.isConnected()) {
                for (String topic : config.getSubscribeTopics()) {
                    client.subscribe(topic, config.getQos());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}