了解
认识dubbo和dubbox可以参考:
Dubbo架构设计详解
Dubbo详细介绍与安装使用过程
Dubbo入门
dubbo服务管理中心
用来管理,查询发布的服务,和dubbo本身的业务逻辑没有关系,是用来管理消费者和服务方的
dubbo
下载安装dubbox
到githug下载dubbox的源码,
在checkout出来的dubbox目录执行mvn install
-Dmaven.test.skip=true来尝试编译一下dubbo(并将dubbo的jar安装到本地maven库)
在checkout出来的dubbox根目录执行mvn idea:idea或者mvn eclipse:eclipse,来创建IDE工程文件
具体可以参考:demo
上面引用至dubbox的github,除此之外可以将项目同步到本地,用ide工具的maven项目执行Install根项目(parent项目)来实现第二步,idea和eclipse都有这个功能
将jar包安装到本地之后就可以在项目中引用了
使用dubbo搭建RPC项目
RPC的目的就是为了远程调用服务,所以dubbo在使用的时候有两个角色,一个是服务的提供方,一个是服务的消费方,提供方提供接口和接口的实现,消费方通过接口消费服务。
准备
一个web server作为服务的提供者(也可以不是web项目,通过main方法也可以),
一个项目作为服务的消费者
一个定义接口的项目打成jar包供消费者和提供者调用
下载zookper作为服务的注册中心
启动zookeeper
window
下载下来之后可以在/conf
下将zoo_sample.cfg
复制一份,然后更改名字为zoo.cfg
(也可以根据需要去配置zookeeper,配置参照百度)然后在/bin
目录下点击zkServer.cmd
启动zookeeper。
注:conf
目录下没有zoo.cfg
启动的时候会出现闪退。
Linux
在项目中使用
下载完install后导入
1 2 3 4 5 6 7 8 9 10 11 12 13
| <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.8.4</version> </dependency>
<dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.4</version> </dependency>
|
如果需要RESTFul风格的可以参照githug查看需要导入的包
除此之外还要导入spring的包,这里不赘述
公共接口类(这里取名remote-api)中的接口
1 2 3 4
| public interface RemoteService {
String sayHello(String word); }
|
提供方的实现类
1 2 3 4 5 6 7
| public class RemoteServiceImpl implements RemoteService{
@Override public String sayHello(String word){ return "Helllo"+word; } }
|
消费方只需要引入接口就行了
spring配置文件
配置文件可以参考:Dubbo配置方式详解
Dubboo配置方式详解
提供方
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <?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="provider" owner="programmer" organization="dubbox"/> <dubbo:registry address="zookeeper://localhost:2181"/> <dubbo:protocol name="dubbo" port="20880" /> <dubbo:service interface="cn.cases.remote.RemoteService" ref="remoteService" protocol="dubbo" /> <bean id="remoteService" class="RemoteServiceImpl"/> </beans>
|
消费方
1 2 3 4 5 6 7 8 9 10 11 12 13
| <?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" owner="programmer" organization="dubbox"/> <dubbo:registry address="zookeeper://localhost:2181"/> <dubbo:reference id="remoteService" interface="cn.cases.remote.RemoteService"/> </beans>
|
调用
消费方和服务方我都是起的web项目,在web.xml引入spring的Listener之后,分别在不同的端口启动,在消费方执行下面的测试用例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:spring/spring-dubbo.xml") public class RemoteTest { @Autowired RemoteService remoteService;
@Test public void test1 (){ String word = remoteService.sayHello("张三"); System.out.println(word); } } --------------------------------------------------------------------
public class RemoteTest {
@Test public void test1 (){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-dubbo.xml"); RemoteService remoteService = (RemoteService)context.getBean("remoteService"); String word = remoteService.sayHello("张三"); System.out.println(word); } }
|
调用成功
Helllo张三
用main方法调用,不用servlet容器,启动之后就可以在客户端调用
1 2 3 4 5
| public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-dubbo.xml"); context.start(); System.in.read(); }
|
注:如果使用外网连接zookeeper
出现一直超时的问题,可以更改<dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" timeout="10000"/>
将超时时间设置长点就可以了,如果依旧超时,参考内网ip问题
至此基本的dubbo使用就完了。
Dubbo client
Dubbo client是用来管理和查看服务的消费和提供情况的,将下载下来的dubbo项目install后,在dubbo-admin下将war拷贝到tomcat的webapps下,启动tomcat,访问localhost+端口号+项目名就可以登录了(默认密码用户名都是root)。