dubbo+zookeeper实战01

来源:互联网 发布:minix源码 编辑:程序博客网 时间:2024/05/09 22:35

近期受SOA影响(ps:各大软件公司招聘官网上陆续出现服务治理、rpc框架、SOA等字眼),于是乎研究起来了dubbo(阿里巴巴产品,最早提出的SOA理念)。

废话少说,不如正题。分别创建两个web工程 dubbo-client、dubbo-server

dubbo-client工程所需jar,即为dubbo-admin lib下的所有jar

ps:之前用的另外一套jar导致消费者调用报错(rpc异常)后来才发现是jar包问题。


dubbo-server工程所需jar


先注册【提供者】

applicationProvider.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="dubbo-demo" />    <!-- zookeeper注册中心 -->    <dubbo:registry address="zookeeper://10.143.184.56:2181" group="dubbo" id="myjhd_id"/>    <dubbo:protocol name="dubbo" port="20880" /><!-- 20880 -->       <!-- 和本地bean一样实现服务 -->    <bean id="demoService" class="com.ardo.dubbo.service.impl.DemoServiceImpl" />     <!-- 向注册中心注册暴漏服务地址,注册服务 -->    <dubbo:service interface="com.ardo.dubbo.service.DemoService"       ref="demoService" executes="10" /></beans>


 

提供者测试类:Test.java

public class Test {    public static void main(String[] args) throws IOException {       System.out.println("注册提供者");        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationProvider.xml");        System.out.println(context.getDisplayName() + ": here");        context.start();        System.out.println("服务已经启动...");        System.in.read();    }}


右键run as服务后即可调用提供者服务

结构图:


【消费者】

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.xsdhttp://code.alibabatech.com/schema/dubbo    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">    <dubbo:application name="consumer-of-dubbo-demo" />     <dubbo:registry address="zookeeper://10.143.184.56:2181" group="dubbo" id="myjhd_id" timeout="30000"/>     <!-- 向注册中心订阅服务 -->    <dubbo:reference id="demoService" interface="com.ardo.dubbo.service.DemoService" check="false" timeout="12000" /></beans>



 

消费者测试类(调用方法)

public class ClientMain {    public static void main(String[] args) {       try {           ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(                  new String[] { "applicationConsumer.xml" });           context.start();           DemoService service = (DemoService) context.getBean("demoService");           //DemoService service = (DemoService) context.getBean(DemoService.class);           System.out.println(service.sayHello(" ardo world"));           context.close();           //while (true) {} //由于消费者用完后就关闭连接,admin平台看不到消费者信息;放开后就能看到了。           System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟       } catch (Exception e) {           e.printStackTrace();       }    }}


结构图:


其中服务API

DemoService.java是两个工程都要有的,所以最好打成jar包供双方使用。

Dubbo平台显示提供者和消费者:


Web.xml方式(tomcat容器)启动注册服务

<context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:applicationProvider.xml</param-value>    </context-param>    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>


阅读全文
0 0
原创粉丝点击