现在用到了分布式框架Dubbo,随笔谢谢Dubbo入门的实例

解释:注册中心,服务注册的地方,通俗的说就是服务所在的位置

我这里的是在192.168.2.168上面

需要用到的jar包

这是客服端和服务端都需要的jar包,我们新建Maven工程。

项目结构图:

服务端:

一个接口(接口中的方法在实现时方法名开始不能以get开头,莫名报错):

publicinterfaceUserService{publicvoiddaoGet();}


实现类(这里必须要实现Seriealizable接口,否则会出现一个错误):

publicclassUserServiceImplimplementsUserService,Serializable{publicvoiddaoGet(){System.out.println("ThisisUserServiceImplMethod");}}


Dao层实现类:

publicclassUserDao{publicvoidtestDao()throwsException{System.out.println("ThisistestDaoMethod");}}



配置文件applicationPrvider.xml配置文件:

<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsd"><dubbo:applicationname="hello-world"/><dubbo:registryaddress="zookeeper://192.168.2.168:2181"/><!--ServiceinterfaceConcurrentControl--><dubbo:serviceinterface="per.lx.service.UserService"ref="daoService"executes="10"/><!--designateimplementation--><beanid="daoService"class="per.lx.service.UserServiceImpl"/></beans>在pom.xml中的相关属性,列出来只是为了方便理解客户端引用服务端:<groupId>per.lx</groupId><artifactId>dubbo-Service</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>dubbo-Service</name>

我的服务端pom里面没有导入的包,我的MAVEN仓库出了点错误,所有用导包的方式。

启动Service主函数:

publicclassMain{publicstaticvoidmain(String[]args)throwsIOException{ClassPathXmlApplicationContextctx=newClassPathXmlApplicationContext(newString[]{"applicationProvider.xml"});System.out.println("kaishi");ctx.start();System.out.println("任意键退出!_____by____lx");System.in.read();}}

---------------------------------------------------------------------------

-------------------服务端写完了,客户端更简单了----------------------------

---------------------------------------------------------------------------


客服端需要的jar包和项目结构与服务端一致,不过有一点很重要,需要在客户端的pom.xml中加入以下代码方便对Service的引用:

<dependency><groupId>per.lx</groupId><artifactId>dubbo-Service</artifactId><version>0.0.1-SNAPSHOT</version></dependency>

这样是完成了对Service的引用

客户端的配置文件applicationConsumer.xml:

<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsd"><!--consumerapplicationname--><dubbo:applicationname="Client-Test"/><!--这是注册中心地址--><dubbo:registryaddress="zookeeper://192.168.2.168:2181"/><dubbo:consumertimeout="5000"/><!--whichservicetoconsume?--><dubbo:referenceid="daoService"interface="per.lx.service.UserService"/></beans>


客户端完成服务端方法:

publicclassConsumerThd{publicvoiddaoService(){ClassPathXmlApplicationContextcontext=newClassPathXmlApplicationContext(newString[]{"applicationConsumer.xml"});context.start();System.out.println("ClientSuccess");UserServiceuserService=(UserService)context.getBean("daoService");userService.daoGet();}}主函数:启动客户端的方法:publicclassAppTest{publicstaticvoidmain(String[]args)throwsException{ConsumerThdthd=newConsumerThd();thd.daoService();System.in.read();}}---------------------------------------------------------------------到这里会发现在服务器的Console打印下面出来了我们在服务端打印的信息,到这里,Dubbo入门基本就完了。