dubbo+zookeeper实战01

近期受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.xsd
http://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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值