首先你需要下载安装一个zookeeper,下载地址http://zookeeper.apache.org/releases.html。选择你需要的版本,我这里使用的是
3.4.14。解压文件后,修改配置文件zoo.cfg。主要是dataDir,修改为自己电脑上面的地址。我使用的win7。
tickTime = 2000
dataDir = D:\\zookeeper\\data
clientPort = 2181
initLimit = 5
syncLimit = 2
在bin文件夹下找到zkServer.cmd。双击启动,启动成功会有下面的提示:
2019-08-28 10:27:56,183 [myid:] - INFO [main:QuorumPeerConfig@136] - Reading co
nfiguration from: D:\tools\zookeeper-3.4.14\bin\..\conf\zoo.cfg
2019-08-28 10:27:56,183 [myid:] - INFO [main:DatadirCleanupManager@78] - autopu
rge.snapRetainCount set to 3
2019-08-28 10:27:56,199 [myid:] - INFO [main:DatadirCleanupManager@79] - autopu
rge.purgeInterval set to 0
2019-08-28 10:27:56,199 [myid:] - INFO [main:DatadirCleanupManager@101] - Purge
task is not scheduled.
2019-08-28 10:27:56,199 [myid:] - WARN [main:QuorumPeerMain@116] - Either no co
nfig or no quorum defined in config, running in standalone mode
2019-08-28 10:27:56,341 [myid:] - INFO [main:QuorumPeerConfig@136] - Reading co
nfiguration from: D:\tools\zookeeper-3.4.14\bin\..\conf\zoo.cfg
2019-08-28 10:27:56,341 [myid:] - INFO [main:ZooKeeperServerMain@98] - Starting
server
2019-08-28 10:27:56,373 [myid:] - INFO [main:Environment@100] - Server environm
ent:zookeeper.version=3.4.14-4c25d480e66aadd371de8bd2fd8da255ac140bcf, built on
03/06/2019 16:18 GMT
2019-08-28 10:27:56,373 [myid:] - INFO [main:Environment@100] - Server environm
ent:host.name=SKY-20190319PCC
2019-08-28 10:27:56,373 [myid:] - INFO [main:Environment@100] - Server environm
ent:java.version=1.8.0_221
2019-08-28 10:27:56,373 [myid:] - INFO [main:Environment@100] - Server environm
ent:java.vendor=Oracle Corporation
2019-08-28 10:27:56,373 [myid:] - INFO [main:Environment@100] - Server environm
ent:java.home=C:\Program Files\Java\jdk1.8.0_221\jre
2019-08-28 10:27:56,373 [myid:] - INFO [main:Environment@100] - Server environm
ent:java.class.path=D:\tools\zookeeper-3.4.14\bin\..\build\classes;D:\tools\zook
eeper-3.4.14\bin\..\build\lib\*;D:\tools\zookeeper-3.4.14\bin\..\zookeeper-3.4.1
4.jar;D:\tools\zookeeper-3.4.14\bin\..\lib\audience-annotations-0.5.0.jar;D:\too
ls\zookeeper-3.4.14\bin\..\lib\jline-0.9.94.jar;D:\tools\zookeeper-3.4.14\bin\..
\lib\log4j-1.2.17.jar;D:\tools\zookeeper-3.4.14\bin\..\lib\netty-3.10.6.Final.ja
r;D:\tools\zookeeper-3.4.14\bin\..\lib\slf4j-api-1.7.25.jar;D:\tools\zookeeper-3
.4.14\bin\..\lib\slf4j-log4j12-1.7.25.jar;D:\tools\zookeeper-3.4.14\bin\..\conf
2019-08-28 10:27:56,373 [myid:] - INFO [main:Environment@100] - Server environm
ent:java.library.path=C:\Program Files\Java\jdk1.8.0_221\bin;C:\Windows\Sun\Java
\bin;C:\Windows\system32;C:\Windows;C:\Program Files (x86)\Common Files\Oracle\J
ava\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\
System32\WindowsPowerShell\v1.0\;C:\Program Files\Java\jdk1.8.0_221\bin;C:\Progr
am Files\Java\jdk1.8.0_221\jre\bin;D:\tools\apache-maven-3.6.1\bin\;;.
2019-08-28 10:27:56,388 [myid:] - INFO [main:Environment@100] - Server environm
ent:java.io.tmpdir=C:\Users\ADMINI~1\AppData\Local\Temp\
2019-08-28 10:27:56,388 [myid:] - INFO [main:Environment@100] - Server environm
ent:java.compiler=<NA>
2019-08-28 10:27:56,388 [myid:] - INFO [main:Environment@100] - Server environm
ent:os.name=Windows 7
2019-08-28 10:27:56,388 [myid:] - INFO [main:Environment@100] - Server environm
ent:os.arch=amd64
2019-08-28 10:27:56,404 [myid:] - INFO [main:Environment@100] - Server environm
ent:os.version=6.1
2019-08-28 10:27:56,404 [myid:] - INFO [main:Environment@100] - Server environm
ent:user.name=Administrator
2019-08-28 10:27:56,404 [myid:] - INFO [main:Environment@100] - Server environm
ent:user.home=C:\Users\Administrator
2019-08-28 10:27:56,419 [myid:] - INFO [main:Environment@100] - Server environm
ent:user.dir=D:\tools\zookeeper-3.4.14\bin
2019-08-28 10:27:56,435 [myid:] - INFO [main:ZooKeeperServer@836] - tickTime se
t to 2000
2019-08-28 10:27:56,435 [myid:] - INFO [main:ZooKeeperServer@845] - minSessionT
imeout set to -1
2019-08-28 10:27:56,435 [myid:] - INFO [main:ZooKeeperServer@854] - maxSessionT
imeout set to -1
2019-08-28 10:27:56,708 [myid:] - INFO [main:ServerCnxnFactory@117] - Using org
.apache.zookeeper.server.NIOServerCnxnFactory as server connection factory
2019-08-28 10:27:56,716 [myid:] - INFO [main:NIOServerCnxnFactory@89] - binding
to port 0.0.0.0/0.0.0.0:2181
到这里前期准备工作就做好了。下面是dubbo工程创建。
你需要创建三个maven工程,一个common,一个consumer,一个provider
首先我们创建一个common项目。
主要注意点有两个,1接口定义
package com.common.service;
public interface ComputeService {
Integer add(int a, int b);
}
2pom文件内容,这里我将devtools注释了,不然会导致报错。具体原因有待研究
<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</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>common</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<!-- <dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency> -->
</dependencies>
</project>
下面是consumer项目。项目结构如下
Application类是启动类
package com.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource({"classpath:dubbo.xml"})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
ComputeController是对外调用接口,可以使用测试类或者定时任务代替。
package com.consumer.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.consumer.service.DComputeService;
@RestController
public class ComputeController {
@Autowired
private DComputeService dcService;
@RequestMapping("getSome")
public Integer getSome(int a, int b) {
return dcService.getSome(a, b);
}
}
DComputeService是关键类,调用provider暴露的接口
package com.consumer.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.common.service.ComputeService;
@Component
public class DComputeService{
@Autowired
private ComputeService computeService;
public Integer getSome(int a, int b) {
System.out.println("=========consumer=========");
return computeService.add(a, b);
}
}
这里的application.properties只是简单的配置,可以根据自己的需要补充
dubbo.registry.adress=localhost:2181
server.port=8081
最后一个是dubbo的配置文件。
<?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 id="zookeeper" protocol="zookeeper" address="${dubbo.registry.adress}"/>
<!-- 指定服务提供者和服务消费者的通信协议 -->
<dubbo:protocol name="dubbo" port="40001"/>
<dubbo:reference id="computeService" interface="com.common.service.ComputeService" check="false"
version="1.0" registry="zookeeper" protocol="dubbo" timeout="1500"/>
</beans>
pom文件要注意添加common项目的依赖
<dependency>
<groupId>com</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
到这里consumer的配置就完成了。
最后是provider项目。
项目结构和consumer类似。这里就不展示了。
启动类SpringBootApplication
package com.wc.www.ScBus;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ImportResource;
@org.springframework.boot.autoconfigure.SpringBootApplication
@ImportResource({"classpath:dubbo.xml"})
public class SpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootApplication.class, args);
}
}
对外暴露的接口DComputeService
package com.wc.www.ScBus.service;
import org.springframework.stereotype.Component;
import com.common.service.ComputeService;
@Component
public class DComputeService implements ComputeService{
public Integer add(int a, int b) {
System.out.println("=========server===========");
System.out.println("a: " + a + ",b: "+ b);
return a + b;
}
}
dubbo配置
<?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="ScBus"/>
<!-- 指定注册中心 -->
<dubbo:registry id="zookeeper" protocol="zookeeper" address="zookeeper://127.0.0.1:2181"/>
<!-- 指定服务提供者和服务消费者的通信协议 -->
<dubbo:protocol name="dubbo" port="20881" dispather="all" threadpool="cached" threads="5000"/>
<!-- 暴露服务接口及接口关联的实现 -->
<dubbo:service interface="com.common.service.ComputeService" ref="computeService"
timeout="1000" retries="3" registry="zookeeper" version="1.0"/>
<bean id="computeService" class="com.wc.www.ScBus.service.DComputeService"/>
</beans>
pom文件记得加入common项目的依赖
<dependency>
<groupId>com</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
到这里provider也完成了。
启动provider项目,然后启动consumer,在浏览器的地址栏输入
http://localhost:8081/getSome?a=4&b=2
如果正确的话,会返回一个“6”;
下面简单总结一些搭建过程中的问题。
1consumer找不到注册的服务。
在consumer的dubbo配置文件的dubbo:reference标签里面,我加了一个version="1.0"。但是在provider的dubbo:service标签中没有添加,导致找不到对于的服务。添加后问题解决了
网上有一个解释是说,provider的实现类没有使用dubbo的@Service 注解。这个经过测试不是必须的。
如果搭建中遇到文件,建议首先检查自己的配置文件是否正确。一开始的时候可以把配置文件写简单一点。非必须的属性可以先不写。避免不必要的错误。
先写到这里。后面有机会再做深入一些的探索
知识使我快乐