1.安装dubbo依赖的常用的zookeeper注册中心
下载地址:http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz
解压 zookeeper-3.4.10.tar.gz,进入解压后conf目录下,复制zoo_sample.cfg文件,重命名为zoo.cfg即可
修改zoo.cfg文件
dataDir=D:\\software\\zookeeper
dataLogDir=D:\\software\\zookeeper
然后进入解压后bin目录,双击zkServer.cmd启动zookeeper
进入zookeeper-3.4.10\bin Shift+右键调出cmd命令窗口:
D:\Program Files\zookeeper-3.3.6\bin>./zkCli.cmd -server 127.0.0.1:2181
如果出现如上图所示内容,表示zookeeper已经安装成功了
2.安装zkui(zookeeper的可视化操作工具)
下载https://github.com/DeemOpen/zkui zkui安装包
下载之后进入zkui-master\ 目录下执行
mvn clean install
打包成功后在zkui-master\target目录下找到zkui-2.0-SNAPSHOT-jar-with-dependencies.jar包复制到zkui-master\ 目录下
然后在zkui-master\执行
java -jar .\zkui-2.0-SNAPSHOT-jar-with-dependencies.jar
执行结果如下:
然后打开网址:http://localhost:9090/
用户名:admin 密码:manager 用户名密码可以在zkui-master\config.cfg文件中配置,也可以在此文件中指定zookeeper的ip和端口
zkui感觉用处没多大,就是下面项目建立好之后启动项目进行消费 点击zkui菜单上Export会查看到zookeeper的活动
3.搭建Dubbo项目
创建maven工程
工程分为4个结构
- dubbo 工程,主工程,主要导入公共jar包等
- dubbo-api 工程,公共接口
- dubbo-provider 工程,服务提供者
- dubbo-consumer 工程,服务消费者
先创建父dubbo工程
Eclipse>file>new>Maven Project 然后建立maven项目
Finish之后我们是无法add Maven Module子工程的
我们需要做一下操作
1.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.feeling.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
</project>
然后我们会发现项目会报错
鼠标放在dubbo工程上右键>maven>Update Project
然后删除上图红色方框圈住的文件,然后变成如下:
然后鼠标放在dubbo工程上右键 >Build Path > Configure Build Path......
将Source和Libraries 下的内容全部Remove
然后Apply and Close,结果如下:
然后我们就可以放心的建依赖于dubbo的子工程了。
dubbo > 右键 > New > Maven Module
然后依次将剩下的两个子工程建好
- dubbo 工程,主工程,主要导入公共jar包等
- dubbo-api 工程,公共接口
- dubbo-provider 工程,服务提供者
- dubbo-consumer 工程,服务消费者
在dubbo-consumer和dubbo-provider的pom中添加依赖,其中dubbo-api是dubbo-api接口子项目引入
<dependencies>
<dependency>
<groupId>com.feeling.api</groupId>
<artifactId>dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.9</version>
</dependency>
</dependencies>
1.dubbo-api接口子项目创建ProviderService.java类
package com.feeling.api.service;
public interface ProviderService {
public void sayHello();
}
2.dubbo-provider子项目(提供方)
ProviderServiceImpl.java
package com.feeling.provider.service.impl;
import com.alibaba.dubbo.rpc.RpcContext;
import com.feeling.api.service.ProviderService;
public class ProviderServiceImpl implements ProviderService{
public void sayHello() {
// TODO Auto-generated method stub
System.out.println("Hello " + ", response form provider: " + RpcContext.getContext().getLocalAddress());
}
}
application.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- provider's application name, used for tracing dependency relationship -->
<dubbo:application name="provider"/>
<!-- use multicast registry center to export service
<dubbo:registry address="multicast://224.5.6.7:1234"/>-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!-- use dubbo protocol to export service on port 20880 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- service implementation, as same as regular local bean -->
<bean id="providerService" class="com.feeling.provider.service.impl.ProviderServiceImpl"/>
<!-- declare the service interface to be exported -->
<dubbo:service interface="com.feeling.api.service.ProviderService" ref="providerService"/>
</beans>
Provider.java启动类
package com.feeling.provider;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"application.xml"});
context.start();
System.out.println("----------------Provider服务已启动,按任意键结束···········--------------------");
System.in.read(); // press any key to exit
}
}
3.dubbo-consumer子项目
application.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),
don't set it same as provider -->
<dubbo:application name="consumer"/>
<dubbo:registry address="zookeeper://localhost:2181"/>
<!-- generate proxy for the remote service, then demoService can be used in the same way as the
local regular interface -->
<dubbo:reference id="providerService" check="false" interface="com.feeling.api.service.ProviderService"/>
</beans>
Consumer.java消费者启动类:
package com.feeling.consumer;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.feeling.api.service.ProviderService;
public class Consumer {
public static void main(String[] args) {
System.setProperty("java.net.preferIPv4Stack", "true");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"application.xml"});
context.start();
ProviderService demoService = (ProviderService) context.getBean("providerService"); // get remote service proxy
while (true) {
try {
Thread.sleep(1000);
demoService.sayHello(); // call remote method
System.out.println("----------------Consumer服务已启动调用接口,1s调用一次");
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
}
启动项目:先运行Provider.java
然后再启动:Consumer.java会发现dubbo-provider中的实现类方法已经被调用;
3.搭建dubbo-admin控制台
1、下载地址:https://github.com/apache/incubator-dubbo/tree/dubbo-2.5.8/
2、解压后,打开cmd进入dubbo-admin目录下,如我解压的目录为:D:\Test\dubbo-master\dubbo-admin,在cmd下输入命令:mvn clean install
3、在target目录下生成dubbo-admin-2.5.8.war,然后将该war包放入tomcat的webapps目录下,启动tomcat, 访问localhost:8080/dubbo-admin-2.5.8,输入默认的用户名和密码:root/root 如图所示:
在当前页输入刚才项目中application.xml配置的<dubbo:application name="provider"/>
搜索provider显示如下