温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Dubbo入门实例

发布时间:2020-07-27 16:11:15 来源:网络 阅读:630 作者:wow_xiang 栏目:开发技术

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

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

我这里的是在192.168.2.168上面

需要用到的jar包

Dubbo入门实例

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

项目结构图:

Dubbo入门实例

服务端:

    一个接口(接口中的方法在实现时方法名开始不能以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配置文件:

    

<?xml version="1.0" encoding="UTF-8"?>
<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 ">
    <dubbo:application name="hello-world" />
      <dubbo:registry address="zookeeper://192.168.2.168:2181" />
        <!-- Service interface Concurrent Control -->
        <dubbo:service interface="per.lx.service.UserService"ref="daoService" executes="10" />
        <!-- designate implementation -->
      <bean id="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主函数:

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的引用:

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

这样是完成了对Service的引用

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

<?xml version="1.0" encoding="UTF-8"?>
<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 ">
    <!-- consumer application name -->
    <dubbo:application name="Client-Test" />
    <!-- 这是注册中心地址 -->
    <dubbo:registry address="zookeeper://192.168.2.168:2181"/>
        <dubbo:consumer timeout="5000" />
        <!-- which service to consume? -->
        <dubbo:reference id="daoService" interface="per.lx.service.UserService"/>
    </beans>


客户端完成服务端方法:

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入门基本就完了。




向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI