现在用到了分布式框架达博,随笔谢谢达博入门的实例
解释:注册中心,服务注册的地方,通俗的说就是服务所在的位置
我这里的是在192.168.2.168上面
需要用到的jar包
这是客服端和服务端都需要的jar包,我们新建Maven工程。
项目结构图:
服务端:
,,,,一个接口(接口中的方法在实现时方法名开始不能以得到开头,莫名报错):
,,,,
,,,,public interface UserService { public void  daoGet (); }
,,,,
,,,,实现类(这里必须要实现Seriealizable接口,否则会出现一个错误):
,,,,
public class UserServiceImpl implements UserService, Serializable { public void  daoGet (), { System.out.println(“却;能够is UserServiceImpl 法”); } }
,,,, Dao层实现类:
,,,,
public class UserDao { public void  testDao (), throws Exception { System.out.println(“却;能够is testDao 法”); } }
配置文件applicationPrvider.xml配置文件:
,,,,
& lt; ? xml version=" 1.0 ",编码=" utf - 8 " ?比; & lt; beans , xmlns=" http://www.springframework.org/schema/beans " ,,,xmlns: xsi=" http://www.w3.org/2001/XMLSchema-instance " ,,,xmlns:达博=" 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 “比; ,,,& lt;达博:application name=" hello world ",/比; ,,,,,& lt;达博:registry 地址=肮芾碓?//192.168.2.168:2181”,/比; ,,,,,,,& lt; !——, Service interface Concurrent Control ——比; ,,,,,,,& lt;达博:service 界面=皃er.lx.service.UserService ref=癲aoService”,执行=" 10 ",/比; ,,,,,,,& lt; !——, designate implementation ——比; ,,,,,& lt; bean id=癲aoService”,类=皃er.lx.service.UserServiceImpl”,/比; & lt;/beans> 在pom.xml中的相关属性,列出来只是为了方便理解客户端引用服务端: ,& lt; groupId> per.lx & lt;才能artifactId> dubbo-Service</artifactId> & lt;才能version> 0.0.1-SNAPSHOT</version> & lt;才能packaging> jar</packaging> & lt;才能name> dubbo-Service</name>
我的服务端pom里面没有导入的包,我的MAVEN仓库出了点错误,所有用导包的方式。
启动服务主函数:
public class Main { public static  void main (String [], args), throws IOException { ClassPathXmlApplicationContext , ctx =, new ClassPathXmlApplicationContext (new String [], {applicationProvider.xml "}); System.out.println (“kaishi”); ctx.start (); System.out.println(“任意键退出! _____by____lx”); System.in.read (); } }
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - -服务端写完了,客户端更简单了- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
客服端需要的jar包和项目结构与服务端一致,不过有一点很重要,需要在客户端的砰的一声。xml中加入以下代码方便对服务的引用:
& lt; dependency> ,,& lt; groupId> per.lx ,,& lt; artifactId> dubbo-Service ,,& lt; version> 0.0.1-SNAPSHOT ,,& lt;/dependency>
这样是完成了对服务的引用
客户端的配置文件applicationConsumer。xml:
& lt; ? xml version=" 1.0 ",编码=" utf - 8 " ?比; & lt;豆类 ,,,xmlns=" http://www.springframework.org/schema/beans " ,,,xmlns: xsi=" http://www.w3.org/2001/XMLSchema-instance " ,,,xmlns:达博=" 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 ,“比; ,,,& lt; !——, consumer application name ——比; null null null null null null null null null null null null null null null null null null null null null null null null null null null达博入门实例