Zookeeper环境搭建(Windows)
Demo源码【dubbo-provider/dubbo-consumer】地址:
https://github.com/duanjunhua/spring-boot
Zookeeper插件地址:
http://www.massedynamic.org/eclipse/updates/
搭建zookeeper环境:
-
在Apache(https://zookeeper.apache.org/)网站下载Zookeeper包,此处使用的是zookeeper-3.4.12.tar.gz版本
-
复制一份zoo_sample.cfg并重命名为zoo.cfg,并修改以下内容:
-
配置zookeeper启动环境:
1. 新建环境变量:ZOO_HOME=F:\MichaelDuan\zookeeper\zookeeper //zookeeper解压(安装)的目录 2. 在Path中添加:%ZOO_HOME%\bin;
-
可以启动zookeeper以及查看启动状态:
- zkZerver.cmd启动:
- 进入客户端查看注册中心信息:
- zkZerver.cmd启动:
配置Dubbo服务端
-
项目依赖
<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.dubbo.demo</groupId> <artifactId>dubbo-provider</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- Spring Boot 启动父依赖 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <properties> <dubbo-spring-boot>1.0.0</dubbo-spring-boot> </properties> <dependencies> <!-- Spring Boot Dubbo 依赖 --> <dependency> <groupId>io.dubbo.springboot</groupId> <artifactId>spring-boot-starter-dubbo</artifactId> <version>${dubbo-spring-boot}</version> </dependency> <!-- Spring Boot Web 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Boot Test 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> </dependencies> </project>
-
项目结构:
-
配置文件:
-
服务端的Service实现类必须导入的为Dubbo的Service
package com.dubbo.demo.service.impl; import com.alibaba.dubbo.config.annotation.Service; import com.dubbo.demo.service.HelloWorldService; /** * 注册为Dubbo服务,导入的为Dubbo Service注解 */ @Service(version="1.0") public class HelloWorldServiceImpl implements HelloWorldService { public String hello(String name) { return "Hello World! " + name; } }
Dubbo客户端
-
依赖
<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.dubbo.demo</groupId> <artifactId>dubbo-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- Spring Boot 启动父依赖 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <properties> <dubbo-spring-boot>1.0.0</dubbo-spring-boot> </properties> <dependencies> <!-- Spring Boot Dubbo 依赖 --> <dependency> <groupId>io.dubbo.springboot</groupId> <artifactId>spring-boot-starter-dubbo</artifactId> <version>${dubbo-spring-boot}</version> </dependency> <!-- Spring Boot Web 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Boot Test 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Swagger UI dependency --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency> <!-- Junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> </dependencies> </project>
-
项目结构:
-
Reference服务:
package com.dubbo.demo.service; import org.springframework.stereotype.Component; import com.alibaba.dubbo.config.annotation.Reference; @Component public class HelloConsumerService { @Reference(version="1.0") HelloWorldService helloWorldService; //不能在Controller中直接Reference,需要使用Component优先加载Bean,否则会报空指针问题 public String rest(String name) { return helloWorldService.hello(name); } }
Dubbo控制台
-
下载Dubbo war包 [dubbo-admin-2.5.4.war],并将war包解压到tomcat webapps路径下并修改dubbo-admin/WEB-INF/dubbo.properties如下配置:
dubbo.registry.address=zookeeper://127.0.0.1:2181 //配置Zookeeper中心地址,集群时填写集群地址 dubbo.admin.root.password=root //客户端默认用户名为root dubbo.admin.guest.password=root //客户端密码
-
启动tomcat,并打开控制台,如下:
-
启动Dubbo服务端并查看:
- Zookeeper注册中心服务:
- 控制台显示:
- Zookeeper注册中心服务:
-
启动客户端,并查看结果:
- Swagger 界面:
- Swagger 界面:
-
Dubbo控制台:
-
测试结果: