SpringBoot+zk+dubbo架构实践(四):sb+zk+dubbo框架搭建(内附源码GitHub地址)

释放双眼,带上耳机,听听看~!

往期回顾

SpringBoot+zk+dubbo架构实践(一):本地部署zookeeper
SpringBoot+zk+dubbo架构实践(二):SpringBoot 集成 zookeeper
SpringBoot+zk+dubbo架构实践(三):部署Dubbo-admin管理平台

sb+zk+dubbo实现效果

模拟了一个provider服务提供方和PC、Web两个服务消费方.gif

前言


1
2
3
4
5
1先看一下上面的图有个简单的概念,然后开始编码。只需完成2件事情。
21、Spring boot + zk + dubbo 框架搭建(1个主项目4个子模块)
32、编写测试类,实现暴露服务的服务提供方、调用远程服务的服务消费方和服务注册与发现的注册中心 功能。
4
5

dubbo.jpeg

项目目录和结构图

项目目录.png

项目说明


1
2
3
4
5
6
7
1weixin-shop 主项目
2shop-api 公共接口
3shop-ds 服务提供方(provider)
4shop-pc 服务消费方1(consumer)-模拟PC端请求入口
5shop-web 服务消费方2(consumer)-模拟移动端请求入口
6
7

项目结构

项目结构.png

备注:目录结构仅供参考,但是配置文件是必不可少的。

weixin-shop 主项目

pom.xml


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
1<?xml version="1.0" encoding="UTF-8"?>
2<project xmlns="http://maven.apache.org/POM/4.0.0"
3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5    <modelVersion>4.0.0</modelVersion>
6
7    <parent>
8        <groupId>org.springframework.boot</groupId>
9        <artifactId>spring-boot-starter-parent</artifactId>
10        <version>1.5.10.RELEASE</version>
11    </parent>
12
13    <groupId>com.itunion</groupId>
14    <artifactId>weixin-shop</artifactId>
15    <version>1.0-SNAPSHOT</version>
16    <packaging>pom</packaging>
17    <!--<name>${project.artifactId}</name>-->
18
19    <properties>
20        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21        <java.version>1.8</java.version>
22        <skip_maven_deploy>true</skip_maven_deploy>
23    </properties>
24
25    <modules>
26        <module>shop-api</module>
27        <module>shop-ds</module>
28        <module>shop-web</module>
29        <module>shop-pc</module>
30    </modules>
31</project>
32
33

shop-api 公共接口-子项目

pom.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1<?xml version="1.0" encoding="UTF-8"?>
2<project xmlns="http://maven.apache.org/POM/4.0.0"
3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5    <parent>
6        <artifactId>weixin-shop</artifactId>
7        <groupId>com.itunion</groupId>
8        <version>1.0-SNAPSHOT</version>
9    </parent>
10    <modelVersion>4.0.0</modelVersion>
11    <artifactId>shop-api</artifactId>
12    <packaging>jar</packaging>
13    <name>${project.artifactId}</name>
14    <properties>
15        <skip_maven_deploy>true</skip_maven_deploy>
16    </properties>
17</project>
18
19

DemoService 接口


1
2
3
4
5
6
7
8
9
10
11
12
1package com.itunion.shop.service;
2
3/**
4 * 测试demo
5 * Created by lin on 2018年04月16日21:38:07
6 */
7public interface DemoService {
8    String sayHello(String name);
9}
10
11
12

shop-ds 服务提供方-子项目

pom.xml


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
1<?xml version="1.0" encoding="UTF-8"?>
2<project xmlns="http://maven.apache.org/POM/4.0.0"
3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5    <parent>
6        <artifactId>weixin-shop</artifactId>
7        <groupId>com.itunion</groupId>
8        <version>1.0-SNAPSHOT</version>
9    </parent>
10    <modelVersion>4.0.0</modelVersion>
11
12    <artifactId>shop-ds</artifactId>
13    <packaging>jar</packaging>
14    <name>${project.artifactId}</name>
15    <properties>
16        <skip_maven_deploy>false</skip_maven_deploy>
17    </properties>
18    <dependencies>
19        <dependency>
20            <groupId>com.itunion</groupId>
21            <artifactId>shop-api</artifactId>
22            <version>${project.parent.version}</version>
23        </dependency>
24        <dependency>
25            <groupId>org.springframework.boot</groupId>
26            <artifactId>spring-boot-starter-web</artifactId>
27        </dependency>
28                <!--dubbo-->
29        <dependency>
30            <groupId>com.alibaba</groupId>
31            <artifactId>dubbo</artifactId>
32            <version>2.6.0</version>
33        </dependency>
34
35        <dependency>
36            <groupId>com.101tec</groupId>
37            <artifactId>zkclient</artifactId>
38            <version>0.10</version>
39        </dependency>
40
41        <dependency>
42            <groupId>org.apache.curator</groupId>
43            <artifactId>curator-framework</artifactId>
44            <version>4.0.0</version>
45        </dependency>
46    </dependencies>
47</project>
48
49

DemoServiceImpl 测试接口实现


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1package com.itunion.shop.service.impl;
2import com.alibaba.dubbo.common.logger.Logger;
3import com.alibaba.dubbo.common.logger.LoggerFactory;
4import com.alibaba.dubbo.rpc.RpcContext;
5import com.itunion.shop.service.DemoService;
6import java.text.SimpleDateFormat;
7import java.util.Date;
8
9/**
10 * 测试demo-服务提供方
11 * Created by lin on 2018年04月16日21:38:07
12 */
13public class DemoServiceImpl implements DemoService {
14    private final static Logger LOGGER = LoggerFactory.getLogger(DemoServiceImpl.class);
15
16    @Override
17    public String sayHello(String name) {
18        System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
19        return "Hello " + name + ", response form provider: " + RpcContext.getContext().getLocalAddress();
20    }
21}
22
23

Provider 测试执行main


1
2
3
4
5
6
7
8
9
10
11
12
13
14
1package com.itunion.shop.service.impl;
2import org.springframework.context.support.ClassPathXmlApplicationContext;
3
4public class Provider {
5    public static void main(String[] args) throws Exception {
6        System.setProperty("java.net.preferIPv4Stack", "true");
7        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"shop-ds-rovider.xml"});
8        context.start();
9        System.out.println("服务提供方已经启动...");
10        System.in.read(); // press any key to exit
11    }
12}
13
14

dubbo.properties


1
2
3
1dubbo.qos.port=33333
2
3

log4j.properties(后面子项目一样用这个)


1
2
3
4
5
6
7
8
9
10
1###set log levels###
2log4j.rootLogger=info, stdout
3###output to the console###
4log4j.appender.stdout=org.apache.log4j.ConsoleAppender
5log4j.appender.stdout.Target=System.out
6log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
7log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n
8
9
10

shop-ds-rovider.xml


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:xsi="http://www.w3.org/2001/XMLSchema-instance"
3       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
4       xmlns="http://www.springframework.org/schema/beans"
5       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
6       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
7
8    <!--定义了提供方应用信息,用于计算依赖关系;在 dubbo-admin 或 dubbo-monitor 会显示这个名字,方便辨识-->
9    <!--<dubbo:application name="shop-web-provider" owner="programmer" organization="dubbox"/>-->
10    <dubbo:application name="dubbo-provider" owner="dubbo-provider" />
11    <!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper-->
12    <!--<dubbo:registry address="multicast://224.5.6.7:1234"/>-->
13    <dubbo:registry address="zookeeper://localhost:2181"/>
14
15    <!-- 用dubbo协议在20880端口暴露服务 -->
16    <dubbo:protocol name="dubbo" port="20880"/>
17
18    <!--具体实现该接口的 bean-->
19    <bean id="demoService" class="com.itunion.shop.service.impl.DemoServiceImpl"/>
20
21    <!--使用 dubbo 协议实现定义好的 DemoService 接口-->
22    <dubbo:service interface="com.itunion.shop.service.DemoService" ref="demoService"/>
23
24</beans>
25
26

shop-web 服务消费方-子项目

pom.xml


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
1<?xml version="1.0" encoding="UTF-8"?>
2<project xmlns="http://maven.apache.org/POM/4.0.0"
3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5    <parent>
6        <artifactId>weixin-shop</artifactId>
7        <groupId>com.itunion</groupId>
8        <version>1.0-SNAPSHOT</version>
9    </parent>
10    <modelVersion>4.0.0</modelVersion>
11    <artifactId>shop-web</artifactId>
12    <packaging>jar</packaging>
13    <name>${project.artifactId}</name>
14    <properties>
15        <skip_maven_deploy>false</skip_maven_deploy>
16    </properties>
17    <dependencies>
18        <dependency>
19            <groupId>com.itunion</groupId>
20            <artifactId>shop-api</artifactId>
21            <version>${project.parent.version}</version>
22        </dependency>
23        <dependency>
24            <groupId>org.springframework.boot</groupId>
25            <artifactId>spring-boot-starter-web</artifactId>
26        </dependency>
27        <!-- Test -->
28        <dependency>
29            <groupId>org.springframework.boot</groupId>
30            <artifactId>spring-boot-starter-test</artifactId>
31            <scope>test</scope>
32        </dependency>
33
34        <!--dubbo-->
35        <dependency>
36            <groupId>com.alibaba</groupId>
37            <artifactId>dubbo</artifactId>
38            <version>2.6.0</version>
39        </dependency>
40
41        <dependency>
42            <groupId>com.101tec</groupId>
43            <artifactId>zkclient</artifactId>
44            <version>0.10</version>
45        </dependency>
46
47        <dependency>
48            <groupId>org.apache.curator</groupId>
49            <artifactId>curator-framework</artifactId>
50            <version>4.0.0</version>
51        </dependency>
52    </dependencies>
53</project>
54
55

ConsumerWeb 移动消费者入口 测试执行main


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
1package com.itunion.shop.web.controller;
2
3import com.itunion.shop.service.DemoService;
4import org.springframework.beans.factory.annotation.Autowired;
5import org.springframework.context.support.ClassPathXmlApplicationContext;
6
7public class ConsumerWeb {
8    @Autowired
9    DemoService demoService;
10    public static void main(String[] args) {
11        System.setProperty("java.net.preferIPv4Stack", "true");
12        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"shop-web-consumer.xml"});
13        context.start();
14        System.out.println("微商城移动端 消费方(Consumer)启动......");
15        DemoService demoService = (DemoService) context.getBean("demoService"); // get remote service proxy
16        System.out.println("消费方(Consumer)");
17        while (true) {
18            try {
19                Thread.sleep(1000);
20                String hello = demoService.sayHello("第2个:我是移动端"); // call remote method
21                System.out.println(hello); // get result
22
23            } catch (Throwable throwable) {
24                throwable.printStackTrace();
25            }
26        }
27    }
28}
29
30
31
32

dubbo.properties


1
2
3
1dubbo.qos.port=11111
2
3

log4j.properties(用上面shop-ds那个)


1
2
3
1省略....
2
3

shop-web-consumer.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
4       xmlns="http://www.springframework.org/schema/beans"
5       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
6       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
7
8    <!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),
9    don't set it same as provider -->
10    <dubbo:application name="shop-web-consumer" owner="programmer" organization="dubbed"/>
11
12    <!-- use multicast registry center to discover service -->
13    <!--<dubbo:registry address="multicast://224.5.6.7:1234"/>-->
14    <dubbo:registry address="zookeeper://localhost:2181"/>
15
16    <!-- generate proxy for the remote service, then demoService can be used in the same way as the
17    local regular interface -->
18    <dubbo:reference id="demoService" check="false" interface="com.itunion.shop.service.DemoService"/>
19</beans>
20
21

shop-pc 服务消费方-子项目

pom.xml(可以复制shop-web pom文件修改artifactId 即可)


1
2
3
4
1    <artifactId>shop-pc</artifactId>
2    省略......
3
4

ConsumerPC PC消费之业务入口 测试执行main


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
1package com.itunion.shop.web.controller;
2
3import com.itunion.shop.service.DemoService;
4import org.springframework.beans.factory.annotation.Autowired;
5import org.springframework.context.support.ClassPathXmlApplicationContext;
6
7public class ConsumerPC {
8    @Autowired
9    DemoService demoService;
10    public static void main(String[] args) {
11        System.setProperty("java.net.preferIPv4Stack", "true");
12        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"shop-pc-consumer.xml"});
13        context.start();
14        System.out.println("微商城PC端-消费方(Consumer)启动......");
15        DemoService demoService = (DemoService) context.getBean("demoService"); // get remote service proxy
16        System.out.println("消费方(Consumer)");
17        while (true) {
18            try {
19                Thread.sleep(1000);
20                String hello = demoService.sayHello("第1个:我是PC端消费方"); // call remote method
21                System.out.println(hello); // get result
22
23            } catch (Throwable throwable) {
24                throwable.printStackTrace();
25            }
26        }
27    }
28}
29
30
31

dubbo.properties


1
2
3
1dubbo.qos.port=22222
2
3

log4j.properties(用上面那个)

省略….

shop-pc-consumer.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
3       xmlns="http://www.springframework.org/schema/beans"
4       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
5       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
6
7    <!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),
8    don't set it same as provider -->
9    <dubbo:application name="shop-pc-consumer" owner="programmer" organization="dubbed"/>
10
11    <!-- use multicast registry center to discover service -->
12    <dubbo:registry address="zookeeper://localhost:2181"/>
13
14    <!-- generate proxy for the remote service, then demoService can be used in the same way as the
15    local regular interface -->
16    <dubbo:reference id="demoService" check="false" interface="com.itunion.shop.service.DemoService"/>
17</beans>
18
19

好了我们的sb+zk+dubbo 框架已经搭建好了,接下来我们执行一下看看结果!(zookeeper 服务记得启动哈)

启动shop-ds 暴露服务的服务提供方

服务提供方启动.jpg

启动shop-pc 服务消费方 -PC端启动

shop-pc消费方调用.jpg

启动shop-web 服务消费方 -移动端启动

shop-web消费方调用.jpg

最后预告

如上面几张控制台截图我们模拟的 shop-ds(服务提供方)、shop-web(移动)和shop-pc(PC)消费方 都已经跑起来了,效果也达到我们预期的目的,那么还剩下最后一部分内容 我们会在 spring boot + zookeeper + dubbo 框架基础上 集成 mybatis + swagger 来实现增、删、改、查业务。

给TA打赏
共{{data.count}}人
人已打赏
安全网络

CDN安全市场到2022年价值76.3亿美元

2018-2-1 18:02:50

安全运维

zabbix监控nginx

2021-8-18 16:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索