(1)准备工作
1.zookeeper
因为要使用zookeeper的注册管理,所以需要安装zookeeper
2)windows下安装
<1>把下载的zookeeper的文件解压到指定目录,如D:\machine\zookeeper-3.3.6>
<2>修改conf下增加一个zoo.cfg
内容如下:
# The number of milliseconds of each tick 心跳间隔 毫秒每次
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting anacknowledgement
syncLimit=5
# the directory where the snapshot isstored. //镜像数据位置
dataDir=D:\\data\\zookeeper
#日志位置
dataLogDir=D:\\logs\\zookeeper
# the port at which the clients willconnect 客户端连接的端口
clientPort=2181
注:如果启动有报错提示cfg文件有错误,可以用zoo_sample.cfg内内容替代也是可以的
<3>进入到bin目录,并且启动zkServer.cmd,这个脚本中会启动一个java进程
D:\machine\zookeeper-3.3.6>cd bin
D:\machine\zookeeper-3.3.6\bin>
D:\machine\zookeeper-3.3.6\bin >zkServer.cmd
<4>启动后jps可以看到QuorumPeerMain的进程
D:\machine\zookeeper-3.3.6\bin >jps
启动客户端运行查看一下
D:\machine\zookeeper-3.3.6\bin>zkCli.cmd -server 127.0.0.1:2181
这个时候zookeeper已经安装成功了
2.添加依赖
1)服务端
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.15.0-GA</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>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6.SEC03</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.6</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.4</version>
</dependency>
</dependencies>
2)客户端
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.15.0-GA</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>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6.SEC03</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>com.github.adyliu</groupId>
<artifactId>zkclient</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.6</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.huangjie</groupId>
<artifactId>dubbo-Service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
3)注意
<1>zookeeper的version必须和下载的版本一致,此处为3.3.6
<2>服务端需要将自身install到本地仓库,然后客户端添加服务端的依赖
(2)服务器端
1.服务接口定义
public interface IProcessData {
public String deal(String data);
}
2.服务实现
public class ProcessDataImpl implements IProcessData {
public String deal(String data) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Finished:" + data;
}
}
3.spring配置文件
<?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
">
<!-- Application name -->
<dubbo:application name="hello-world-app" />
<!-- registry address, used for service to register itself -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- expose this service through dubbo protocol, through port 20880 -->
<!--
<dubbo:protocol name="dubbo" port="20880" />
<dubbo:protocol name="dubbo" port="9090" server="netty"
client="netty" codec="dubbo" serialization="hessian2" charset="UTF-8"
threadpool="fixed" threads="100" queues="0" iothreads="9" buffer="8192"
accepts="1000" payload="8388608" />
-->
<!-- Service interface Concurrent Control -->
<dubbo:service interface="com.huangjie.dubbo_Service.service.IProcessData"
ref="demoService" executes="10" />
<!-- Default Protocol -->
<!--
<dubbo:protocol server="netty" />
-->
<!-- designate implementation -->
<bean id="demoService" class="com.huangjie.dubbo_Service.service.impl.ProcessDataImpl" />
</beans>
4.测试
public class DubboProviderMain {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"applicationProvider.xml"});
context.start();
System.out.println("Press any key to exit.");
System.in.read();
}
}
(3)客户端
1.spring配置文件
<?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="consumer-of-helloworld-app" />
<!-- registry address, used for consumer to discover services -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:consumer timeout="5000"/>
<!-- which service to consume? -->
<dubbo:reference id="demoService" interface="com.huangjie.dubbo_Service.service.IProcessData" />
</beans>
4.测试
public class ConsumerThd implements Runnable {
@Override
public void run() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"applicationConsumer.xml"});
context.start();
IProcessData demoService = (IProcessData) context.getBean("demoService"); // get
// service
// invocation
// proxy
String hello = demoService.deal("nihao"); // do invoke!
System.out.println(Thread.currentThread().getName() + " "+hello);
}
public static void main(String[] args) {
new Thread(new ConsumerThd()).start();
}
}