码迷,mamicode.com
首页 > 编程语言 > 详细

jmeter调用自己打的jar包

时间:2021-05-24 06:54:03      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:val   encoding   public   ext   unit   digest   代码   png   get   

1 打包jar包

1.1  创建 javaweb  maven 项目

通过md5加密签名得到 sign 值,前台接口信息头需要此参数

技术图片

技术图片

 

技术图片

技术图片

技术图片

pom文件

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.hjh</groupId>
  <artifactId>JmeterSignUtilHjh</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>JmeterSignUtilHjh Maven Webapp</name>
  <!-- FIXME change it to the project‘s website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.75</version>
    </dependency>

    <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-all</artifactId>
      <version>1.7.1</version>
      <type>pom</type>
    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <!--<scope>provided</scope>-->
    </dependency>



  </dependencies>

  <build>
    <finalName>JmeterSignUtilHjh</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>
新建一个java文件夹

技术图片

 

 技术图片

工具类代码

package com.util;

import org.apache.shiro.crypto.hash.Md5Hash;

import java.util.*;

public class JmeterSignUtilHjh {

    public static void main(String[] args) {
        Map< String, Object > signMap = new HashMap<>();
        signMap.put("parameter1" , "str1");
        signMap.put("parameter2" , "str2");
        System.out.println("str:" + JmeterSignUtilHjh.getSignStr(signMap,"str3"));
    }

    public static String getSignStr(Map < String, Object > paramValues , String secret) {
        try {
            StringBuilder sb = new StringBuilder();
            List< String > paramNames = new ArrayList<>();
            paramNames.addAll(paramValues.keySet());
            Collections.sort(paramNames);
            for (String paramName : paramNames) {
                sb.append(paramName).append(paramValues.get(paramName));
            }
            sb.append(secret);
            return MD5Digest(sb.toString());
        } catch (Exception e) {
            throw new RuntimeException("加密签名计算异常!" , e);
        }
    }

    /**
     * md5工具类
     */
    private static String MD5Digest(String src){
        return new Md5Hash(src).toString();
    }

}

 1.2 打包写好的代码

技术图片

 

 技术图片

 把jar包放到\apache-jmeter-5.2.1\lib\ext 里面,重启jmeter

2 jmeter 引入jar包

技术图片

 

技术图片

 

import com.util.JmeterSignUtilHjh;


Map  signMap = new HashMap ();

comp = vars.get("compname");
limit = vars.get("limit");
es=vars.get("es");

signMap.put("companyName" , comp);
signMap.put("limit" , limit);
signMap.put("useES" , es);
//sign=SignUtils.getSignStr(signMap,null);
sign=JmeterSignUtilHjh.getSignStr(signMap,"fe716cbb-a990-40d4-b7c7-7b9db338c6c8");

vars.put("sign",sign);
System.out.println("companyName:" +    comp);
System.out.println("limit:" +    limit);
System.out.println("es:" +    es);
System.out.println("sign:" +    sign);

 技术图片

 

 

 

 技术图片

 

jmeter调用自己打的jar包

标签:val   encoding   public   ext   unit   digest   代码   png   get   

原文地址:https://www.cnblogs.com/hujinhua/p/14759826.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!