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

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

我这里的是在192.168.2.168上面

需要用到的jar包

Dubbo入门实例_第1张图片

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

项目结构图:

Dubbo入门实例_第2张图片

服务端:

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

    

    public interface UserService {
	public void daoGet();
}

    

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

    

public class UserServiceImpl implements UserService, Serializable {

	public void daoGet() {
		System.out.println("This is UserServiceImpl Method");
	}

}


    Dao层实现类:

    

public class UserDao {
public void testDao() throws Exception {
System.out.println("This is testDao Method");
}
}



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

    


    
      
        
        
        
      


在pom.xml中的相关属性,列出来只是为了方便理解客户端引用服务端:
 per.lx
  dubbo-Service
  0.0.1-SNAPSHOT
  jar
  dubbo-Service

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

启动Service主函数:

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


   	per.lx
   	dubbo-Service
   	0.0.1-SNAPSHOT
   

这样是完成了对Service的引用

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


    
    
    
    
        
        
        
    


客户端完成服务端方法:

public class ConsumerThd {
	public void daoService(){
		ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(	new String[] {"applicationConsumer.xml"});
		context.start();
		System.out.println("Client Success");
		UserService userService = (UserService) context.getBean("daoService");
		userService.daoGet();
	}
}


主函数:启动客户端的方法:
public class AppTest {
	public static void main(String[] args) throws Exception{
		ConsumerThd thd=new ConsumerThd();
		thd.daoService();
		System.in.read();
	}
}
---------------------------------------------------------------------
到这里会发现在服务器的Console打印下面出来了我们在服务端打印的信息,到这里,Dubbo入门基本就完了。