Dubbo简单使用
欢迎关注微信公众号: 程序员小圈圈
转载请标明出处^_^
原文首发于: www.zhangruibin.com
本文出自于: RebornChang的博客
首先说下环境:
win7,maven,jdk1.8.2
基础环境
因为Dubbo的注册中心使用的是zookeeper,所以,应该在本地启动zookeeeper。
1.本文所使用的的zookeeper版本为3.4.2.关注公众号,回复:zk,即可获取资源。
2.本文所使用的demo,回复:dubbodemo,即可获取下载。
zookeeper 资源下载之后是zip压缩包,直接解压,进入到bin目录下双击运行zkServer bat文件即可。
Dubbo demo
我们将demo分为两部分,一部分为服务端,用于向zk注册发布服务。
另一部分是Dubbo客户端,用于从zk中心订阅服务。
我们整体的工程大致是这个做:
服务端有一个接口,及接口实现类,实现类里有一个方法,返回 “hello world”
客户端订阅服务,调用服务端的接口,并打印。
简单吧,那就来看看demo吧,主要是让大家看看整个流程是怎样的,不要再听到Dubbo+zookeeper就内心慌得一批,本文适合那种没用过d+z的小白,大神请略过。
DubboServer
因为是maven的,所以首先是pom文件。引入依赖:
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.zrb</groupId>
<artifactId>dubbo-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>server</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- spring begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!-- spring end --> <!-- dubbo begin --> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<!-- dubbo end --> <!-- 注册中心zookeeper begin --> <dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.6</version>
</dependency>
<!-- 注册中心zookeeper end --> <!-- log begin --> <dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
<exclusion>
<artifactId>jms</artifactId>
<groupId>javax.jms</groupId>
</exclusion>
<exclusion>
<artifactId>mail</artifactId>
<groupId>javax.mail</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- log end -->
<!-- other begin -->
<dependency>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
<version>3.2.0.Final</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.8</version>
</dependency>
<!-- other end -->
</dependencies>
</project>
然后是配置文件:
applicationProvider.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="dubbo-demo" />
<!-- zookeeper注册中心 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:protocol name="dubbo" port="20880" />
<!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="com.zrb.service.impl.DemoServiceImpl" />
<!-- 向注册中心注册暴漏服务地址,注册服务 -->
<dubbo:service interface="com.zrb.service.DemoService"
ref="demoService" executes="10" />
</beans>
接下来就是简单的代码层了,加载配置文件,打印输出:
ServerMain.java
package main;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ServerMain {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationProvider.xml" });
context.start();
System.out.println("~~~ESC break out~~~~");
System.in.read();
context.close();
}
}
DemoService.java
package com.zrb.service;
public interface DemoService {
String sayHello(String name);
}
DemoServiceImpl.java
package com.zrb.service.impl;
import com.zrb.service.DemoService;
public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
System.out.println("init : " + name);
return "hello " + name;
}
}
至此服务端的代码就结束了,是不是很简单?不要慌,因为客户端的更简单!
DubboClient
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.zrb</groupId>
<artifactId>dubbo-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>client</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- spring begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!-- spring end --> <!-- dubbo begin --> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<!-- dubbo end --> <!-- 注册中心zookeeper begin --> <dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.6</version>
</dependency>
<!-- 注册中心zookeeper end --> <!-- log begin --> <dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
<exclusion>
<artifactId>jms</artifactId>
<groupId>javax.jms</groupId>
</exclusion>
<exclusion>
<artifactId>mail</artifactId>
<groupId>javax.mail</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- log end --> <!-- other begin --> <dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.8</version>
</dependency>
<dependency>
<groupId>com.zrb</groupId>
<artifactId>dubbo-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- other end -->
</dependencies>
</project>
配置文件applicationConsumer.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="consumer-of-dubbo-demo" />
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 向注册中心订阅服务 -->
<dubbo:reference id="demoService" interface="com.zrb.service.DemoService" />
</beans>
测试类 ClientMain.java
package main;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zrb.service.DemoService;
public class ClientMain {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationConsumer.xml" });
context.start();
DemoService service = (DemoService) context.getBean("demoService");
System.out.println(service.sayHello("world"));
context.close();
}
}
完事,是不是更简单?
接下来说下怎么使用这个demo。
执行demo
不嫌麻烦的朋友可以复制代码粘贴,嫌麻烦的可以公众号回复:dubbodemo。
即可或许本demo,idea导入即可。
启动顺序
先启动zookeeper,然后启动dubboserver.dubboserver启动起来之后再启动dubboclient.
注意启动的时候观察zookeeper的cmd窗口,可以看到服务的注册以及订阅。
类似下图所示:
至此,最简单的dubbo+zookeeper demo就完事了。
看了这个作为小白的笔者暗自欣喜,这算是迈进门槛了,那接下来有必要深入研究下,有这想法的朋友可以关注下笔者的公众号或者个人记住博客,博主接下来会深入的分享下d+z的使用,配置,源码之类的。
Over!