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