dubbo接口测试-查找店铺可领取的劵
查找店铺可领取的劵这个dubbo接口的Java脚本编辑,开发提供的内容:
1、具体的方法及参数
接口类:com.gome.coupon.interfaces.dubbo.IPromotionAndCouponManager
方法:queryAvailableDarwCoupons(String userId, Set<String> shopNo)
参数:参数1:"100021145163", 参数2:{"81012772","80007143"}
2、服务消费配置
<dubbo:application name="npop_logistics_query" owner="dongchao" />
<dubbo:protocol name="dubbo" port="-1"/>
<dubbo:registry address="zookeeper://10.58.57.58:2181?backup=10.58.57.54:2181,10.58.57.48:2181" id="defaultZookeeperAddress" />
<dubbo:reference id="promotionAndCouponManager" interface="com.gome.coupon.interfaces.dubbo.IPromotionAndCouponManager" registry="defaultZookeeperAddress " group="npop-coupon-front" check="false" timeout="6000"/>
<dubbo:reference id="couponAndBatchManager" interface="com.gome.coupon.interfaces.dubbo.ICouponAndBatchManager" registry="defaultZookeeperAddress " group="npop-coupon-front" check="false" timeout="6000"/>
3、服务引用Jar包
<dependency>
<artifactId>promotion-interfaces</artifactId>
<version>1.0.9-SNAPSHOT</version>
<name>promotion-interfaces</name>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
操作步骤:
1、spring-dubbo-consumer.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://code.alibabatech.com/schema/dubbo 8 http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> 9 <!--提供方应用信息,用于计算依赖关系--> 10 <dubbo:application name="npop_logistics_query" owner="dongchao" /> 11 <!-- 用dubbo协议在-1端口暴露服务 --> 12 <dubbo:protocol name="dubbo" port="-1"/> 13 <!-- 使用zookeeper注册中心暴露服务地址 -即zookeeper的所在服务器ip地址和端口号 --> 14 <dubbo:registry address="zookeeper://10.58.57.58:2181?backup=10.58.57.54:2181,10.58.57.48:2181" id="defaultZookeeperAddress" /> 15 <!-- 声明需要暴露的服务接口 --> 16 <dubbo:reference id="promotionAndCouponManager" interface="com.gome.coupon.interfaces.dubbo.IPromotionAndCouponManager" registry="defaultZookeeperAddress " group="npop-coupon-front" check="false" timeout="6000"/> 17 <dubbo:reference id="couponAndBatchManager" interface="com.gome.coupon.interfaces.dubbo.ICouponAndBatchManager" registry="defaultZookeeperAddress " group="npop-coupon-front" check="false" timeout="6000"/> 18 </beans>
2、pop.xml
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <!-- <groupId>com.gome.pop</groupId> 5 <artifactId>pop-external-commom</artifactId> 6 <packaging>war</packaging> 7 <version>1.0.4-SNAPSHOT</version> 8 <name>pop-external-commom Maven Webapp</name> 9 <url>http://maven.apache.org</url>--> 10 11 <groupId>load.coo8.coupon</groupId> 12 <artifactId>coo8.coupon</artifactId> 13 <version>1.0-SNAPSHOT</version> 14 15 <properties> 16 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 17 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 18 <spring.version>4.1.0.RELEASE</spring.version> 19 </properties> 20 21 22 <dependencies> 23 24 <dependency> 25 <groupId>com.gome.pop</groupId> 26 <artifactId>pop-common</artifactId> 27 <version>STABLE-1.0.18</version> 28 <exclusions> 29 <exclusion> 30 <groupId>com.101tec</groupId> 31 <artifactId>zkclient</artifactId> 32 </exclusion> 33 <exclusion> 34 <groupId>org.jboss.netty</groupId> 35 <artifactId>netty</artifactId> 36 </exclusion> 37 <exclusion> 38 <groupId>org.slf4j</groupId> 39 <artifactId>jcl-over-slf4j</artifactId> 40 </exclusion> 41 <exclusion> 42 <groupId>cglib</groupId> 43 <artifactId>cglib</artifactId> 44 </exclusion> 45 <exclusion> 46 <artifactId>coupon-interfaces-simple</artifactId> 47 <groupId>coupon-interfaces</groupId> 48 </exclusion> 49 </exclusions> 50 </dependency> 51 <dependency> 52 <groupId>com.gome.npop</groupId> 53 <artifactId>promotion-interfaces</artifactId> 54 <version>1.0.9-SNAPSHOT</version> 55 <exclusions> 56 <exclusion> 57 <artifactId>*</artifactId> 58 <groupId>*</groupId> 59 </exclusion> 60 </exclusions> 61 </dependency> 62 63 64 65 <!--spring相关配置,spring需要的jar包--> 66 <dependency> 67 <groupId>org.springframework</groupId> 68 <artifactId>spring-core</artifactId> 69 <version>${spring.version}</version> 70 </dependency> 71 <dependency> 72 <groupId>org.springframework</groupId> 73 <artifactId>spring-aop</artifactId> 74 <version>${spring.version}</version> 75 </dependency> 76 <dependency> 77 <groupId>org.springframework</groupId> 78 <artifactId>spring-beans</artifactId> 79 <version>${spring.version}</version> 80 </dependency> 81 <dependency> 82 <groupId>org.springframework</groupId> 83 <artifactId>spring-context</artifactId> 84 <version>${spring.version}</version> 85 </dependency> 86 <dependency> 87 <groupId>org.springframework</groupId> 88 <artifactId>spring-expression</artifactId> 89 <version>${spring.version}</version> 90 </dependency> 91 <dependency> 92 <groupId>org.springframework</groupId> 93 <artifactId>spring-tx</artifactId> 94 <version>${spring.version}</version> 95 </dependency> 96 97 <!--zookeeper--> 98 <dependency> 99 <groupId>com.github.sgroschupf</groupId> 100 <artifactId>zkclient</artifactId> 101 <version>0.1</version> 102 </dependency> 103 104 <dependency> 105 <groupId>org.apache.zookeeper</groupId> 106 <artifactId>zookeeper</artifactId> 107 <version>3.4.6</version> 108 </dependency> 109 110 <dependency> 111 <groupId>com.alibaba</groupId> 112 <artifactId>dubbo</artifactId> 113 <version>2.5.5.cat-SNAPSHOT</version> 114 <exclusions> 115 <exclusion> 116 <groupId>org.springframework</groupId> 117 <artifactId>spring</artifactId> 118 </exclusion> 119 <exclusion> 120 <groupId>org.glassfish.grizzly</groupId> 121 <artifactId>grizzly-framework</artifactId> 122 </exclusion> 123 </exclusions> 124 </dependency> 125 126 <!--打印日志使用slf4j 而不是log4j--> 127 <dependency> 128 <groupId>org.slf4j</groupId> 129 <artifactId>slf4j-api</artifactId> 130 <version>1.6.1</version> 131 </dependency> 132 133 134 135 <!--junit--> 136 <dependency> 137 <groupId>junit</groupId> 138 <artifactId>junit</artifactId> 139 <version>4.10</version> 140 <scope>test</scope> 141 </dependency> 142 143 144 145 <dependency> 146 <groupId>junit</groupId> 147 <artifactId>junit</artifactId> 148 <version>3.8.1</version> 149 <scope>test</scope> 150 </dependency> 151 152 <!--fastjson 阿里巴巴公司开源的速度最快的Json和对象转换工具--> 153 <dependency> 154 <groupId>com.alibaba</groupId> 155 <artifactId>fastjson</artifactId> 156 <version>1.2.6</version> 157 </dependency> 158 159 <!--jmeter--> 160 <dependency> 161 <groupId>org.apache.jmeter</groupId> 162 <artifactId>ApacheJMeter_java</artifactId> 163 <version>2.13</version> 164 <exclusions> 165 <exclusion> 166 <groupId>d-haven-managed-pool</groupId> 167 <artifactId>d-haven-managed-pool</artifactId> 168 </exclusion> 169 170 <exclusion> 171 <groupId>commons-math3</groupId> 172 <artifactId>commons-math3</artifactId> 173 </exclusion> 174 175 <exclusion> 176 <groupId>commons-pool2</groupId> 177 <artifactId>commons-pool2</artifactId> 178 </exclusion> 179 180 <exclusion> 181 <groupId>commons-collections</groupId> 182 <artifactId>commons-collections</artifactId> 183 </exclusion> 184 </exclusions> 185 <!--打包时注释放开--> 186 <!--<scope>provided</scope>--> 187 </dependency> 188 189 </dependencies> 190 191 <!--构建项目--> 192 <build> 193 <plugins> 194 <plugin> 195 <groupId>org.apache.maven.plugins</groupId> 196 <artifactId>maven-compiler-plugin</artifactId> 197 <version>2.4</version> 198 <configuration> 199 <source>1.5</source> 200 <target>1.5</target> 201 </configuration> 202 </plugin> 203 <plugin> 204 <groupId>org.apache.maven.plugins</groupId> 205 <artifactId>maven-shade-plugin</artifactId> 206 <version>2.3</version> 207 <executions> 208 <execution> 209 <phase>package</phase> 210 <goals> 211 <goal>shade</goal> 212 </goals> 213 <configuration> 214 <filters> 215 <filter> 216 <artifact>*:*</artifact> 217 <excludes> 218 <exclude>META-INF/*.SF</exclude> 219 <exclude>META-INF/*.DSA</exclude> 220 <exclude>META-INF/*.RSA</exclude> 221 </excludes> 222 </filter> 223 </filters> 224 <transformers> 225 <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> 226 <resource>META-INF/spring.handlers</resource> 227 </transformer> 228 <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> 229 <resource>META-INF/spring.schemas</resource> 230 </transformer> 231 </transformers> 232 </configuration> 233 </execution> 234 </executions> 235 </plugin> 236 </plugins> 237 </build> 238 239 240 </project>
打包时候四个注意点:
a>以下的过滤不要忘记加
1 <plugins> 2 <plugin> 3 <groupId>org.apache.maven.plugins</groupId> 4 <artifactId>maven-compiler-plugin</artifactId> 5 <version>2.4</version> 6 <configuration> 7 <source>1.5</source> 8 <target>1.5</target> 9 </configuration> 10 </plugin> 11 <plugin> 12 <groupId>org.apache.maven.plugins</groupId> 13 <artifactId>maven-shade-plugin</artifactId> 14 <version>2.3</version> 15 <executions> 16 <execution> 17 <phase>package</phase> 18 <goals> 19 <goal>shade</goal> 20 </goals> 21 <configuration> 22 <filters> 23 <filter> 24 <artifact>*:*</artifact> 25 <excludes> 26 <exclude>META-INF/*.SF</exclude> 27 <exclude>META-INF/*.DSA</exclude> 28 <exclude>META-INF/*.RSA</exclude> 29 </excludes> 30 </filter> 31 </filters> 32 <transformers> 33 <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> 34 <resource>META-INF/spring.handlers</resource> 35 </transformer> 36 <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> 37 <resource>META-INF/spring.schemas</resource> 38 </transformer> 39 </transformers> 40 </configuration> 41 </execution> 42 </executions> 43 </plugin> 44 </plugins>
b>加上如下内容
1 <exclusions> 2 <exclusion> 3 <artifactId>coupon-interfaces-simple</artifactId>
4 <groupId>coupon-interfaces</groupId> 5 </exclusion> 6 </exclusions>
c>过滤以下包里面和依赖无关的其他所有文件
1 <dependency> 2 <groupId>com.gome.npop</groupId> 3 <artifactId>promotion-interfaces</artifactId> 4 <version>1.0.9-SNAPSHOT</version> 5 <exclusions> 6 <exclusion> 7 <artifactId>*</artifactId> 8 <groupId>*</groupId> 9 </exclusion> 10 </exclusions> 11 </dependency>
d>打包的时候注意将下面的注释放开,是jmeter里面的。
1 <!--打包时注释放开--> 2 <scope>provided</scope>
3、ContextGetBean.class
1 package com.gome.test.load; 2 3 import org.springframework.context.support.ClassPathXmlApplicationContext; 4 5 import java.lang.Object; 6 import java.lang.String; 7 8 public class ContextGetBean{ 9 //获取spring中定义的bean实例(对象),加载spring配置 10 //通过ApplicationContext获取spring配置文件中的bean 11 //要获取XML中配置的Bean,最关键的是获取org.springframework.context.ApplicationContext 12 //获取 ApplicationContext 的方法如下 13 private static ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:spring-dubbo-consumer.xml"}); 14 //"包名(或者是包的完整路径)/配置文件名字(也就是xml文件)"); 15 //ApplicationContext的主要实现类是ClassPathXmlApplicationContext和FileSystemXmlApplicationContext,前者默认从类路径加载配置文件,后者默认从文件系统中装载配置文件 16 public static void ContextGetBean() 17 { 18 context.start(); 19 } 20 21 public static Object getBean(String classpath, String beanName) { 22 return context.getBean(beanName); 23 //context.getBean("配置文件中定义的bean实例") 获得 bean 实例。 24 } 25 }
4、QueryCoupons.class
1 package com.gome.test.load; 2 3 import com.gome.coupon.interfaces.dubbo.IPromotionAndCouponManager; 4 import com.gome.coupon.model.cartBean.CommitCouponsAndPromotionParam; 5 import com.gome.coupon.model.cartBean.CouponBatchResult; 6 import com.gome.coupon.model.cartBean.ResultDO; 7 import com.googlecode.mp4parser.authoring.Sample; 8 import org.apache.jmeter.config.Arguments; 9 import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient; 10 import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; 11 import org.apache.jmeter.samplers.SampleResult; 12 13 import java.util.*; 14 15 /** 16 * Created by zhaichao on 2017/6/15. 17 */ 18 public class QueryCoupons extends AbstractJavaSamplerClient { 19 private IPromotionAndCouponManager iPromotionAndCouponManager; 20 //IPromotionAndCouponManager这个类是定义要测试的接口的类,类+名称 21 private Arguments params; 22 //定义这个接口所需要传的参数 23 public void setupTest(JavaSamplerContext args){ 24 iPromotionAndCouponManager=(IPromotionAndCouponManager)ContextGetBean.getBean("classpath:spring-dubbo-consumer.xml", "promotionAndCouponManager"); 25 //setupTest类用于实现测试前置,连接那些服务器,调用那些方法等 26 //IPromotionAndCouponManager是被测接口所在的类, 27 //getBean(文件名称,暴露的接口的id) 因为可以配置很多接口,所以需要id来区分要使用哪个 28 } 29 public Arguments getDefaultParameters(){ 30 //getDefaultParameterslei类用于从外部( Jmeter界面)获取参数 31 this.params = new Arguments(); 32 this.params.addArgument("userId","100021145163"); 33 this.params.addArgument("shopNo","\"81012772\",\"80007143\""); 34 //这里的shopNo是Set<String>类型的,但是可以定义成字符串类型,然后下面进行转化 35 return params; 36 } 37 // public void setParam(JavaSamplerContext args){ 38 // 39 // } 40 public SampleResult runTest(JavaSamplerContext args) 41 { 42 //runTest类为调用测试接口方法部分,使用传递进来的参数 43 String userId = args.getParameter("userId", "100021145163"); 44 String shopNo = args.getParameter("shopNo"); 45 shopNo=shopNo.replaceAll("\"",""); 46 //首先把shopNo值里面的\"用空格替换 47 String[] shopNoLi=shopNo.split(","); 48 //创建一个数组 49 Set<String> setShopNo= new HashSet<String>(); 50 //set本身是接口,所以需要实现,可以如上定义: 51 for(int i=0;i<shopNoLi.length;i++){ 52 setShopNo.add(shopNoLi[i]); 53 //list转set 54 } 55 ResultDO<Map<String, List<CouponBatchResult>>> rd = iPromotionAndCouponManager.queryAvailableDarwCoupons(userId, setShopNo); 56 //调用iPromotionAndCouponManager类中的方法queryAvailableDarwCoupons 57 SampleResult sr = new SampleResult(); 58 sr.sampleStart(); 59 try{ 60 if(rd != null && rd.isSuccess()==true){ 61 this.getLogger().info("一切正常"); 62 sr.setSuccessful(true); 63 }else{ 64 this.getLogger().info(rd.getErrorMsg()); 65 this.getLogger().info("没有抛异常啊!"+"dafdsaf"); 66 sr.setSuccessful(false); 67 } 68 }catch (Exception e){ 69 sr.setSuccessful(false); 70 e.getStackTrace(); 71 this.getLogger().error("抛出异常如下:"+e); 72 } 73 sr.sampleEnd(); 74 return sr; 75 } 76 public void teardownTest(JavaSamplerContext args){ 77 //teardownTest类用于调用接口完成之后的收尾工作,输出调用服务处理时间,释放相关资源等 78 } 79 /** 80 在java中,main()方法是java应用程序的入口方法。 81 java虚拟机通过main方法找到需要启动的运行程序,并且检查main函数所在类是否被java虚拟机装载。 82 如果没有装载,那么就装载该类,并且装载所有相关的其他类。 83 因此程序在运行的时候,第一个执行的方法就是main()方法。 84 通常情况下, 如果要运行一个类的方法,必须首先实例化出来这个类的一个对象, 85 然后通过"对象名.方法名()"的方式来运行方法,但是因为main是程序的入口,这时候还没有实例化对象, 86 因此将main方法声明为static的,这样这个方法就可以直接通过“类名.方法名()”的方式来调用。 87 */ 88 public static void main(String[] args){ 89 QueryCoupons querycoupons = new QueryCoupons(); 90 //new一个对象 91 Arguments param = querycoupons.getDefaultParameters(); 92 //获取参数 93 JavaSamplerContext javaSamplerContext = new JavaSamplerContext(param); 94 querycoupons.setupTest(javaSamplerContext); 95 SampleResult result = querycoupons.runTest(javaSamplerContext); 96 querycoupons.teardownTest(javaSamplerContext); 97 } 98 99 100 }

浙公网安备 33010602011771号