由于比赛,需要用到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中心手册,修改后,就好了。具体原因尚不清楚。