RPC Dubbo入门

相关文摘

dubbo documentation:http://dubbo.io/
dubbo 源码:https://github.com/alibaba/dubbo

创建服务端

  1. 将所有模块导入IDEA,打开dubbo-demo-provide模块,在 com.alibaba.dubbo.demo.provider目录下创建服务端
  • Provider.java
1
2
3
4
5
6
7
8
9
10
11
12
package com.alibaba.dubbo.demo.provider;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"META-INF/spring/dubbo-demo-provider.xml"});
context.start();
System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
}
}
  • 创建dubbo-demo-provider.xml并配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?xml version="1.0" encoding="UTF-8"?>
<!--
- Copyright 1999-2011 Alibaba Group.
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-->
<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-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="hello-world-app" />
<!-- 本机 伪集群 测试 -->
<!--<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />-->
<dubbo:registry protocol="multicast" address="224.5.6.7:1234" />
<dubbo:protocol name="dubbo" port="20880" />
<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" timeout="1200000" /> <!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" />
</beans>
  1. 运行Provider服务端

创建客户端

  1. 打开dubbo-demo-consumer模块,在com.alibaba.dubbo.demo.consumer目录下创建客户端测试
  • Consumer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.alibaba.dubbo.demo.consumer;
import com.alibaba.dubbo.demo.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by Administrator on 2016/6/1.
*/
public class Consumer {
public static void main(String[]args) throws InterruptedException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "spring/dubbo-demo-consumer.xml");
Thread.sleep(6000);
DemoService demoService = (DemoService) context.getBean("demoService"); //
String hello = demoService.sayHello("tom"); // ִ
System.out.println(hello); //
}
}
  • 创建dubbo-demo-consumer.xml并配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.alibaba.dubbo.demo.consumer;
import com.alibaba.dubbo.demo.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Consumer {
public static void main(String[]args) throws InterruptedException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "spring/dubbo-demo-consumer.xml");
Thread.sleep(6000);
DemoService demoService = (DemoService) context.getBean("demoService");
String hello = demoService.sayHello("tom");
System.out.println(hello);
}
}
  1. 运行Consumer main方法测试,调用成功就OK了,按着dubbo开发指南断点,查看源码
文章目录
  1. 1. 相关文摘
  2. 2. 创建服务端
  3. 3. 创建客户端
,