首先创建maven父子模块java项目,给出命令,如下
mvn archetype:generate -DgroupId=com.github.ll -DartifactId=dubbo-parent3 -DarchetypeArtifactId=maven-archetype-quickstart
在创建完父模块之后,需要手动更改父pom文件,修改打包方式为pom
mvn archetype:generate -DgroupId=com.github.ll -DartifactId=provider3 -DarchetypeArtifactId=maven-archetype-quickstartmvn archetype:generate -DgroupId=com.github.ll -DartifactId=consumer3 -DarchetypeArtifactId=maven-archetype-quickstart
修改相关依赖项,消费者依赖提供者。再创建子模块之后需要打包,mvn clean package,安装到本地仓库,打包之后可以执行测试 mvn test, 如果出现问题可以 -X 也就是debug形式 跟你程序debug一样,会输出相关信息。最后将整个父工程 打包 安装到本地仓库
然后将整个项目导入eclipse,如果是idea的话应该会有相关的mvn命令。之后将项目编辑环境改成本地jdk,创建资源文件夹。
之后给出相关pom文件以及相关代码,zk去官网下载。
消费者
public class ServerMain1 {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationConsumer.xml" });
context.start();
DubboService service = (DubboService) context.getBean("demoService");
System.out.println(service.sayHello("world"));
context.close();
}
}
消费者的application.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.github.ll.DubboService" />
</beans>
父模块的pom依赖项
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!-- dubbo begin -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<!-- dubbo end -->
<!-- 注册中心zookeeper begin -->
<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.5.3-beta</version>
<type>pom</type>
</dependency>
<!--A zookeeper client, that makes life a little easier.-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.8</version>
</dependency>
</dependencies>
<modules>
<module>provider3</module>
<module>consumer3</module>
</modules>
provider代码接口以及实现类暴漏服务
public interface DubboService {
String sayHello(String name);
}
public class DubboServiceImpl implements DubboService {
public String sayHello(String name) {
System.out.println("init : " + name);
return "hello " + name;
}
}
public class ServerMain {
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();
}
}
provider提供者application.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.github.ll.DubboServiceImpl" />
<!-- 向注册中心注册暴漏服务地址,注册服务 -->
<dubbo:service interface="com.github.ll.DubboService"
ref="demoService" executes="10" />
</beans>
最后修改zk配置文件,将下载项的例子直接改成zoo.cfg