首先下载zookeeper并解压得到如下
修改conf文件夹下面的zoo.cfg文件配置如下
# The number of milliseconds of each tick
#这个时间是作为zookeeper服务器之间或客户端与服务器支架维持心跳的时间间隔,也就是每个tickTime时间就会发送一个心跳。
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 an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
#zookeeper保存数据的目录,默认情况下,zookeeper将写数据的日志文件也保存在这个目录里。
dataDir=F:\TOOLS\zookeeper\zookeeper-3.4.13\database\data
#zookeeper保存日志文件的目录
dataDirLog=F:\TOOLS\zookeeper\zookeeper-3.4.13\database\log
# the port at which the clients will connect
#这个端口是客户端连接zookeeper服务器的端口,zookeeper会监听这个端口,接受客户端的访问请求。
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
然后进入bin目录下windows系统选择zkServer.cmd启动zookeeper服务。
然后下载dubbo
修改dubbo-admin的配置信息编辑dubbo.properties文件
修改zookeeper地址
dubbo.registry.address=zookeeper://127.0.0.1:2181 //zookeeper地址
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest
然后将dubbo-admin打成war包放到tomcat里面
修改tomcat端口号为8001并启动,输入如下地址http://localhost:8001/dubbo-admin-2.8.4/
然后创建我们的工程目录如下
dubbo-consumer为消费者
dubbo-provider为生产者
dubbo-api为基础模块
api项目结构如下
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>dubbox</artifactId>
<groupId>com.**</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-api</artifactId>
<dependencies>
</dependencies>
</project>
dobbo-provider项目结构为
这里需要注意的是引入的Service为dubbo包里面的Service而不是spring的
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>dubbox</artifactId>
<groupId>com.**</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-provider</artifactId>
<dependencies>
<dependency>
<groupId>com.**</groupId>
<artifactId>dubbo-api</artifactId>
<version>${dubbo-api.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--dubbo2.8.4必须引入spring-boot-starter-web依赖,这也是我在做项目时遇到的坑,旧版本2.5.5不需要引入-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- DubboX相关 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!-- zookeeper 第三方操作工具类 -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>${zkclient.version}</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
dubbox.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="provider"/>
<!-- 注册中心的ip地址 -->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!-- 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类 -->
<dubbo:annotation package="com.**.service.impl"/>
<!-- <!– 用dubbo协议在20880端口暴露服务,默认:20880 –>
<dubbo:protocol name="dubbo" port="20881" />-->
<!-- 要暴露的服务接口-->
<dubbo:service interface="com.**.service.TestService" ref="testService" version="1.0.0"/>
<bean id="testService" class="com.**.service.impl.TestServiceImpl"></bean>
</beans>
接下来是dubbo-consumer项目
调用生产者提供的接口服务
@RestController
public class TestController {
@Reference(version = "1.0.0")
private TestService testService;
@GetMapping("hello")
public String hello() {
return testService.sayHello("hello springboot and dubbo");
}
}
pom.xml引入的依赖为
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>dubbox</artifactId>
<groupId>com.**</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-consumer</artifactId>
<dependencies>
<dependency>
<groupId>com.**</groupId>
<artifactId>dubbo-api</artifactId>
<version>${dubbo-api.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--dubbo2.8.4必须引入spring-boot-starter-web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- DubboX相关 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!-- zookeeper 第三方操作工具类 -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>${zkclient.version}</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
dubbox.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"/>
<dubbo:registry address="zookeeper://localhost:2181"/>
<!-- <!– 用dubbo协议在20880端口暴露服务,默认:20880 –>
<dubbo:protocol name="dubbo" port="20881" />-->
<dubbo:annotation package="com.**.controller"/>
<dubbo:reference id="testService" interface="com.**.service.TestService" check="false" version="1.0.0"/>
</beans>
最后启动生产者和消费者服务
请求controller里面的接口
请求成功!
然后在回头看dubbo-admin界面显示如下