dubbo入门例子
2016-05-01 21:39 tony4geek 阅读(706) 评论(0) 编辑 收藏 举报SOA 服务框架dubbo
节点角色说明:
Provider: 暴露服务的服务提供方。
Consumer: 调用远程服务的服务消费方。
Registry: 服务注册与发现的注册中心。
Monitor: 统计服务的调用次调和调用时间的监控中心。
Container: 服务运行容器
远程生产者
至于dubbo的详细介绍网上很多,这次主要是简单测试。
远程接口
package com.tony.dubbo.service;
public interface CustomerService {
public String getName();
}
远程接口实现方法
package com.tony.dubbo.service;
public class CustomerServiceImpl implements CustomerService {
public String getName() {
System.out.println("remote method~~~~");
return "CustomerServiceImpl=====>getName";
}
}
**远程dobbo的接口dubbo-provide.xmll文件
因为要用到zookeeper,至于zookeeper的安装可以参考上篇博文
<?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管理页面比较清晰是哪个应用暴露出来的 -->
<dubbo:application name="dubbo_provider"></dubbo:application>
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://192.168.163.33:2181"></dubbo:registry>
<dubbo:protocol name="dubbo" port="20880" />
<!-- 具体的实现bean -->
<bean id="demoService" class="com.tony.dubbo.service.CustomerServiceImpl" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.tony.dubbo.service.CustomerService" ref="demoService" />
</beans>
DemoProvider.java
package com.tony.dubbo.service;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DemoProvider {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*:META-INF/spring/*.xml");
context.start();
System.out.println("run DemoProvider...");
System.in.read();
}
}
Maven依赖配置
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.5.0-alpha</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
简单工程
以上是远程的的方法,我们要把远程的接口方法打成jar。提供给消费者使用。
消费者方法
package com.tony.dubbo.serviceImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tony.dubbo.service.CustomerService;
public class DemoConsumer {
public static void main(String[] args) {
// Consumer
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*:META-INF/spring/*.xml");
context.start();
CustomerService userService = (CustomerService) context.getBean("demoService");
System.out.println(userService.getName());
}
}
dubbo-consumer.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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
default-lazy-init="false" >
<!-- <dubbo:application name="dubbo_consumer"></dubbo:application> -->
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://192.168.163.33:2181" check="false"></dubbo:registry>
<!-- 要引用的服务 -->
<dubbo:reference interface="com.tony.dubbo.service.CustomerService" id="demoService"></dubbo:reference>
</beans>
Maven依赖 pom.xml配置
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.5.0-alpha</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>com.tony</groupId>
<artifactId>dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
消费者工程
运行结果
- 首先运行远程生产方法
- 运行消费服务方法
- 可以进入linux中查看zookeeper的连接状态,发现 dubbo
以上是基于dubbo 2.5.3 版本,zookeeper的3.5.0-alpha版本测试,开始测试过程用的zookeeper其他版本测试发现有些依赖jar不全。
进过测试发现基于SOA,可以调用到远程的方法。当然可以debug远程的方法就和本地调试一样。
当然我们dobbo也有monitor监控中心,可以详细看到现在启动的服务,有哪些机器正在调用等等。