前面的章节中我们已经安装好了zookeeper,tomcat了。今天我们来实现一个完整的从dubbo消息产生到消费的完整流程。
1.下载 dubbo-admin-2.4.1.war
到我的百度云盘下载 http://pan.baidu.com/s/1nvO5CCT
2.解压到tomcat中
unzip dubbo-admin-2.4.1.war -d /apps/tomcat/tomcat3/apache-tomcat-7.0.69/webapps/admin
3.修改dubbo配置文件,指定注册中心地址
cd /apps/tomcat/tomcat3/apache-tomcat-7.0.69/webapps/admin/WEB-INF
vi dubbo.properties
4.启动zookeeper
/apps/zookeeper/zookeeper-3.4.6/bin/zkServer.sh start
5.启动tomcat
sh /apps/tomcat/tomcat3/apache-tomcat-7.0.69/bin/startup.sh
6.访问注册中心控制台
http://192.168.158.129:8080/admin
输入账号密码 root/root
登录成功
7.dubbo api
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.yum</groupId> <artifactId>dubbo-api</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>dubbo-api</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- dubbo start --> <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.4.6</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <!-- dubbo end --> </dependencies> </project>
接口 DubboServer.java
package com.yum.service; /** *Administrator *2016年8月9日 *TODO */ public interface DubboServer { public String hello(String name); }
接口 DubboProvider.java
package com.yum.service; import com.yum.entity.Person; /** *Administrator *2016年8月10日 *TODO */ public interface DubboProvider { public String PersonInfo(Person person); }
Person.java
package com.yum.entity; import java.io.Serializable; import java.util.ArrayList; import java.util.List; /** *Administrator *2016年8月10日 *TODO */ public class Person implements Serializable{ private static final long serialVersionUID = -3829509742163991304L; private String name; private int age; private List<Object> list = new ArrayList<Object>(); public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public List<Object> getList() { return list; } public void setList(List<Object> list) { this.list = list; } }
8.dubbo consumer 消费者 依赖dubbo api 模块
pom.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 "> <!-- consumer application name --> <dubbo:application name="dubbo_client" /> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://192.168.158.129:2181" check="false" subscribe="true" register="true" ></dubbo:registry> <!-- <dubbo:protocol name="dubbo" port="20880" /> --> <dubbo:consumer timeout="5000" /> <!-- which service to consume? --> <dubbo:reference id="dubboService" interface="com.yum.service.DubboServer" version="1.0" /> <dubbo:reference id="dubboProvider" interface="com.yum.service.DubboProvider" version="1.0" /> </beans>
DubboClientTest.java 来一个测试类
package com.dubbo.test; import java.util.ArrayList; import java.util.List; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.yum.entity.Person; import com.yum.service.DubboProvider; import com.yum.service.DubboServer; /** *Administrator *2016年8月10日 *TODO */ public class DubboClientTest { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "dubbo_client.xml" }); context.start(); DubboServer dubboServer = (DubboServer) context.getBean("dubboService"); DubboProvider dubboProvider = (DubboProvider) context.getBean("dubboProvider"); Person person = new Person(); person.setAge(22); person.setName("qihh"); List list = new ArrayList(); list.add("abc"); list.add("123"); list.add("zzzz"); person.setList(list); System.out.println(dubboServer.hello("hello world")); System.out.println(dubboProvider.PersonInfo(person)); } }
9.dubbo provider 生产者 依赖dubbo api 模块
pom.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管理页面比较清晰是哪个应用暴露出来的 --> <dubbo:application name="dubbo_provider" /> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://192.168.158.129:2181" check="false" subscribe="false" register=""></dubbo:registry> <!-- 要暴露的服务接口 --> <dubbo:service interface="com.yum.service.DubboServer" ref="dubboService" version="1.0"/> <dubbo:service interface="com.yum.service.DubboProvider" ref="dubboProvider" version="1.0"/> <!-- expose this service through dubbo protocol, through port 20880 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- which service interface do we expose? --> <!-- bean配置 --> <bean id="dubboService" class="com.qihh.dubbo.service.impl.DubboServerImpl" /> <bean id="dubboProvider" class="com.qihh.dubbo.service.impl.DubboProviderImpl" /> </beans>
接口实现
DubboProviderImpl.java
package com.qihh.dubbo.service.impl; import java.util.Iterator; import java.util.List; import org.apache.log4j.Logger; import com.yum.entity.Person; import com.yum.service.DubboProvider; /** *Administrator *2016年8月10日 *TODO */ public class DubboProviderImpl implements DubboProvider { Logger logger = Logger.getLogger(getClass()); public String PersonInfo(Person person) { logger.info("name:"+person.getName()+" age:"+person.getAge()); List list = person.getList(); Iterator<String> ite = list.iterator(); while(ite.hasNext()){ String obj = ite.next(); logger.info(obj); } return "success"; } }
DubboServerImpl.java
package com.qihh.dubbo.service.impl; import com.yum.service.DubboServer; /** *Administrator *2016年8月10日 *TODO */ public class DubboServerImpl implements DubboServer{ public String hello(String name) { return "hello "+name; } }
10.部署调用
部署 dubbo-server
调用 DubboClientTest.java
11.查看zookeeper注册中心
完整的安装包已经源码可以去我的百度云找。
百度云: http://pan.baidu.com/s/1nvO5CCT