Dubbo入门案例

由于比赛,需要用到dubbo,因此花了点时间熟悉一下。写一个简单的案例,捋一捋流程。

主要分为服务端、客户端。

一、服务定义

也即是接口的定义,参照官网给出的用例。定义如下接口:

package com.alibaba.dubbo.demo;

public interface DemoService {
    String sayHello(String name);
}
其中,客户端只需要有这个接口的签名即可,服务端需要有接口签名+接口实现

二、服务端接口实现

package com.alibaba.dubbo.demo.provider;
import com.alibaba.dubbo.demo.DemoService;

public class DemoServiceImpl implements DemoService {
    public String sayHello(String name) {
        return "Hello " + name;
    }
} 

三、服务端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="demo-provider"/>
    <dubbo:registry protocol="zookeeper" address="xxx.xxx.xxx.xxx:2181" client="zkclient" />
    <dubbo:protocol name="dubbo" port="20880"/>
    <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/>
    <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>
</beans>
配置xml文件,具体标签都比较容易理解。需要注意的是,直接使用官网的例子(multicast)

可能会出问题。这里使用了推荐的zookeeper注册中心。

四、服务端启动代码

package com.alibaba.dubbo.demo.provider;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    public static void main(String[] args) throws Exception {
    	
    	
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] {"classpath:dubbo-demo-provider.xml"});
        System.out.println("Start");
        context.start();
        // press any key to exit                 
        System.in.read();  
        System.out.println("Stop");
    }
}  

以上。服务端配置成功。

客户端配置就比较简单了。

五、客户端接口定义

和服务端一样就行

六、客户端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="demo-consumer"/>
     <dubbo:registry protocol="zookeeper" address="xxx.xxx.xxx.xxx:2181" client="zkclient" />
    <dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService" check="false"/>
</beans>

七、客户端启动代码

package com.alibaba.dubbo.demo.consumer;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.dubbo.demo.DemoService;

public class App2 {
	 public static void main(String[] args) throws Exception {
	        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
	                new String[]{"dubbo-demo-consumer.xml"});
	        System.out.println("Consumer!");
	        context.start();
	        // obtain proxy object for remote invocation
	        DemoService demoService = (DemoService) context.getBean("demoService");
	        // execute remote invocation
	        String hello = demoService.sayHello("world");
	        // show the result
	        System.out.println("From Server:"+hello);
	    }
}

至此,全部配置完毕。

整个过程中,弄得最久的就是注册中心那块,开始使用官网的multicast,一直找不到provider。

后来重新参照官网给的zookeeper中心手册,修改后,就好了。具体原因尚不清楚。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值