dubbo接口如何测试
1、一:首先我们看服务端代码代码架构为:
2、新建一个maven工程,pom文件为: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/xsd/maven-4.0.0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 5 <groupId>com.ustc.demo</groupId> 6 <artifactId>dubbo-provider</artifactId> 7 <version>0.0.1-SNAPSHOT</version> 8 <packaging>jar</packaging> 9 10 <name>dubbo-provider</name>11 <url>http://maven.apache.org</url>12 13 <properties>14 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>15 </properties>16 17 <dependencies>18 <dependency>19 <groupId>junit</groupId>20 <artifactId>junit</artifactId>21 <version>3.8.1</version>22 <scope>test</scope>23 </dependency>24 <dependency>25 <groupId>com.alibaba</groupId>26 <artifactId>dubbo</artifactId>27 <version>2.4.9</version>28 </dependency>29 <dependency>30 <groupId>com.github.sgroschupf</groupId>31 <artifactId>zkclient</artifactId>32 <version>0.1</version>33 </dependency>34 </dependencies>35 <build>36 <plugins>37 <plugin>38 <artifactId>maven-dependency-plugin</artifactId>39 <executions>40 <execution>41 <id>unpack</id>42 <phase>package</phase>43 <goals>44 <goal>unpack</goal>45 </goals>46 <configuration>47 <artifactItems>48 <artifactItem>49 <groupId>com.alibaba</groupId>50 <artifactId>dubbo</artifactId>51 <version>${project.parent.version}</version>52 <outputDirectory>${project.build.directory}/dubbo</outputDirectory>53 <includes>META-INF/assembly/**</includes>54 </artifactItem>55 </artifactItems>56 </configuration>57 </execution>58 </executions>59 </plugin>60 <plugin>61 <artifactId>maven-assembly-plugin</artifactId>62 <configuration>63 <descriptor>src/main/assembly/assembly.xml</descriptor>64 </configuration>65 <executions>66 <execution>67 <id>make-assembly</id>68 <phase>package</phase>69 <goals>70 <goal>single</goal>71 </goals>72 </execution>73 </executions>74 </plugin>75 </plugins>76 </build>77 </project>
3、在src/main下新建文件夹assembly,然后在assembly文件夹下新建assembly.xml文件1 <assembly> 2 <id>assembly</id> 3 <formats> 4 <format>tar.gz</format> 5 </formats> 6 <includeBaseDirectory>true</includeBaseDirectory> 7 <fileSets> 8 <fileSet> 9 <directory>${project.build.directory}/dubbo/META-INF/assembly/bin10 </directory>11 <outputDirectory>bin</outputDirectory>12 <fileMode>0755</fileMode>13 </fileSet>14 <fileSet>15 <directory>src/main/assembly/conf</directory>16 <outputDirectory>conf</outputDirectory>17 <fileMode>0644</fileMode>18 </fileSet>19 </fileSets>20 <dependencySets>21 <dependencySet>22 <outputDirectory>lib</outputDirectory>23 </dependencySet>24 </dependencySets>25 </assembly>
4、在src/main/assembly文件夹下新建conf文件夹,然后在conf文件夹下新建dubbo.properties文件,此处的zookeeper的地址根据实际进行修改1 dubbo.container=log4j,spring 2 dubbo.application.name=demo-caiya 3 dubbo.application.owner=william 4 #dubbo.registry.address=multicast://224.5.x.7:1234 5 dubbo.registry.address=zookeeper://134.xx.xx.xx:2181 6 #dubbo.registry.address=redis://127.0.0.1:6379 7 #dubbo.registry.address=dubbo://127.0.0.1:9090 8 #dubbo.monitor.protocol=registry 9 dubbo.protocol.name=dubbo10 dubbo.protocol.port=2088011 #dubbo.service.loadbalance=roundrobin12 #dubbo.log4j.file=logs/dubbo-demo-consumer.log13 #dubbo.log4j.level=WARN
5、在src/test/resources包路径下,新建dubbo.properties文件,内容和上面的3中dubbo.properties文件内容相同
6、编写provider的接口sayHello,新建DemoService.java类
7、编写sayHello接口的实现类,新建DemoServiceImpl.java类package com.ustc.demo.provider;import java.text.SimpleDateFormat;import java.util.Date;public class DemoServiceImpl implements DemoService{ public String sayHello(String name) { String time = new SimpleDateFormat("HH:mm:ss").format(new Date()); System.out.println("from consumer:"+name); return "The current time is:"+time; }
8、编写spring的配置文件,在META-INF/spring文件夹下的demo-provider.xml文件1 <?xml version="1.0" encoding="UTF-8" ?> 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">3 <bean id="demoService" class="com.ustc.demo.provider.DemoServiceImpl" /> 4 <dubbo:service interface="com.ustc.demo.provider.DemoService" ref="demoService"/> 5 </beans>
9、编写main方法,新建DemoServiceMain.java类1 package com.ustc.demo.provider; 2 public class DemoServiceMain {3 public static void main(String[] args) {4 com.alibaba.dubbo.container.Main.main(args);5 }6 }这样服务端的代码就写好了,实现的功能是当消费者来询问当前时间是几点的时候,返回当前时间
10、然后我们看消费端代码