zookeeper官方下载地址:https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.13/ ,下载好后,先将conf文件夹下的 zoo-sample.cfg重命名为zoo.cfg,同时系统环境中要配置JAVA_HOME
-
dubbo-server项目
新建一个maven项目“dubbo-server”,建好相应的包,最终目录结构如下:
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.eduask</groupId>
<artifactId>dubbo-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<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>
在source资源文件夹下的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注册中心 -->
<!-- 通过zookeeper配置发布地址和zookeeper调度端口,其他的项目来访问本项目的接口时,都统一走这个调度端口 -->
<!-- 如果有多个项目互相调用,那么被调用的项目都要配置一个zookeeper -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 通过dubbo,将接口暴露在20880端口 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="com.eduask.service.imp.DemoServiceImp" />
<!-- 向注册中心注册暴漏服务地址,注册服务,3000代表超时时间 -->
<dubbo:service interface="com.eduask.service.DemoService" ref="demoService" executes="3000" />
</beans>
在service包下写接口DemoService.java
:
package com.eduask.service;
public interface DemoService {
public String getHello(String name);
}
和实现类DemoServiceImpl.java
:
package com.eduask.service.imp;
import com.eduask.service.DemoService;
public class DemoServiceImp implements DemoService {
@Override
public String getHello(String name) {
return "hello"+name;
}
}
test包下新建Test.java
package com.eduask.test;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationProvider.xml"});
context.start();
System.out.println("输入任意按键退出 ~ ");
System.in.read();
context.close();
}
}
此时可以先试试服务能不能跑了,先到zookeeper的bin目录下,运行 zkServer.cmd ,将zookeeper的服务跑起来,再运行这个项目中的Test.java
,控制台打印:
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
输入任意按键退出 ~
-
dubbo-client项目
新建一个maven项目“dubbo-client”,建好相应的包,最终目录结构如下:
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.eduask</groupId>
<artifactId>dubbo-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<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.eduask</groupId>
<artifactId>dubbo-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- other end -->
</dependencies>
</project>
在source资源文件夹下的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">
<!-- 写这样一个服务的名字,可以在zookeeper的管理平台看见 -->
<dubbo:application name="consumer-of-dubbo-demo" />
<!-- 向注册中心注册使用,访问的是目的项目的zookeeper调度端口 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 向注册中心订阅服务 -->
<!-- 客户端拿到的是服务端提供的接口,zookeeper通过接口名来调度接口实现类 -->
<dubbo:reference id="demoService" interface="com.eduask.service.DemoService" />
</beans>
最后在test包下建Test.java
:
package com.eduask.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.eduask.service.DemoService;
public class Test {
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.getHello(" 老王"));
context.close();
}
}
运行(前提是zookeeper和dubbo-server项目没停),控制台打印:
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
hello 老王
远程调用成功。