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

往期回顾

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

sb+zk+dubbo实现效果

SpringBoot+zk+dubbo架构实践(四):sb+zk+dubbo框架搭建(内附源码GitHub地址)_第1张图片
模拟了一个provider服务提供方和PC、Web两个服务消费方.gif

前言

先看一下上面的图有个简单的概念,然后开始编码。只需完成2件事情。
1、Spring boot + zk + dubbo 框架搭建(1个主项目4个子模块)
2、编写测试类,实现暴露服务的服务提供方、调用远程服务的服务消费方和服务注册与发现的注册中心 功能。 
SpringBoot+zk+dubbo架构实践(四):sb+zk+dubbo框架搭建(内附源码GitHub地址)_第2张图片
dubbo.jpeg

项目目录和结构图

SpringBoot+zk+dubbo架构实践(四):sb+zk+dubbo框架搭建(内附源码GitHub地址)_第3张图片
项目目录.png

项目说明

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

项目结构


SpringBoot+zk+dubbo架构实践(四):sb+zk+dubbo框架搭建(内附源码GitHub地址)_第4张图片
项目结构.png

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

weixin-shop 主项目

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">
    <modelVersion>4.0.0modelVersion>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.10.RELEASEversion>
    parent>

    <groupId>com.ituniongroupId>
    <artifactId>weixin-shopartifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>pompackaging>
    

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <java.version>1.8java.version>
        <skip_maven_deploy>trueskip_maven_deploy>
    properties>

    <modules>
        <module>shop-apimodule>
        <module>shop-dsmodule>
        <module>shop-webmodule>
        <module>shop-pcmodule>
    modules>
project>

shop-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>weixin-shopartifactId>
        <groupId>com.ituniongroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>
    <artifactId>shop-apiartifactId>
    <packaging>jarpackaging>
    <name>${project.artifactId}name>
    <properties>
        <skip_maven_deploy>trueskip_maven_deploy>
    properties>
project>
DemoService 接口
package com.itunion.shop.service;

/**
 * 测试demo
 * Created by lin on 2018年04月16日21:38:07
 */
public interface DemoService {
    String sayHello(String name);
}

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

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>weixin-shopartifactId>
        <groupId>com.ituniongroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>shop-dsartifactId>
    <packaging>jarpackaging>
    <name>${project.artifactId}name>
    <properties>
        <skip_maven_deploy>falseskip_maven_deploy>
    properties>
    <dependencies>
        <dependency>
            <groupId>com.ituniongroupId>
            <artifactId>shop-apiartifactId>
            <version>${project.parent.version}version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
                
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>dubboartifactId>
            <version>2.6.0version>
        dependency>

        <dependency>
            <groupId>com.101tecgroupId>
            <artifactId>zkclientartifactId>
            <version>0.10version>
        dependency>

        <dependency>
            <groupId>org.apache.curatorgroupId>
            <artifactId>curator-frameworkartifactId>
            <version>4.0.0version>
        dependency>
    dependencies>
project>
DemoServiceImpl 测试接口实现
package com.itunion.shop.service.impl;
import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.alibaba.dubbo.rpc.RpcContext;
import com.itunion.shop.service.DemoService;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 测试demo-服务提供方
 * Created by lin on 2018年04月16日21:38:07
 */
public class DemoServiceImpl implements DemoService {
    private final static Logger LOGGER = LoggerFactory.getLogger(DemoServiceImpl.class);

    @Override
    public String sayHello(String name) {
        System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
        return "Hello " + name + ", response form provider: " + RpcContext.getContext().getLocalAddress();
    }
}
Provider 测试执行main
package com.itunion.shop.service.impl;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {
    public static void main(String[] args) throws Exception {
        System.setProperty("java.net.preferIPv4Stack", "true");
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"shop-ds-rovider.xml"});
        context.start();
        System.out.println("服务提供方已经启动...");
        System.in.read(); // press any key to exit
    }
}
dubbo.properties
dubbo.qos.port=33333
log4j.properties(后面子项目一样用这个)
###set log levels###
log4j.rootLogger=info, stdout
###output to the console###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n

shop-ds-rovider.xml
xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    
    
    <dubbo:application name="dubbo-provider" owner="dubbo-provider" />
    
    
    <dubbo:registry address="zookeeper://localhost:2181"/>

    
    <dubbo:protocol name="dubbo" port="20880"/>

    
    <bean id="demoService" class="com.itunion.shop.service.impl.DemoServiceImpl"/>

    
    <dubbo:service interface="com.itunion.shop.service.DemoService" ref="demoService"/>

beans>

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

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>weixin-shopartifactId>
        <groupId>com.ituniongroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>
    <artifactId>shop-webartifactId>
    <packaging>jarpackaging>
    <name>${project.artifactId}name>
    <properties>
        <skip_maven_deploy>falseskip_maven_deploy>
    properties>
    <dependencies>
        <dependency>
            <groupId>com.ituniongroupId>
            <artifactId>shop-apiartifactId>
            <version>${project.parent.version}version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>dubboartifactId>
            <version>2.6.0version>
        dependency>

        <dependency>
            <groupId>com.101tecgroupId>
            <artifactId>zkclientartifactId>
            <version>0.10version>
        dependency>

        <dependency>
            <groupId>org.apache.curatorgroupId>
            <artifactId>curator-frameworkartifactId>
            <version>4.0.0version>
        dependency>
    dependencies>
project>
ConsumerWeb 移动消费者入口 测试执行main
package com.itunion.shop.web.controller;

import com.itunion.shop.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ConsumerWeb {
    @Autowired
    DemoService demoService;
    public static void main(String[] args) {
        System.setProperty("java.net.preferIPv4Stack", "true");
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"shop-web-consumer.xml"});
        context.start();
        System.out.println("微商城移动端 消费方(Consumer)启动......");
        DemoService demoService = (DemoService) context.getBean("demoService"); // get remote service proxy
        System.out.println("消费方(Consumer)");
        while (true) {
            try {
                Thread.sleep(1000);
                String hello = demoService.sayHello("第2个:我是移动端"); // call remote method
                System.out.println(hello); // get result

            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
    }
}


dubbo.properties
dubbo.qos.port=11111
log4j.properties(用上面shop-ds那个)
省略....
shop-web-consumer.xml
xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    
    <dubbo:application name="shop-web-consumer" owner="programmer" organization="dubbed"/>

    
    
    <dubbo:registry address="zookeeper://localhost:2181"/>

    
    <dubbo:reference id="demoService" check="false" interface="com.itunion.shop.service.DemoService"/>
beans>

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

pom.xml(可以复制shop-web pom文件修改artifactId 即可)
    <artifactId>shop-pcartifactId>
    省略......
ConsumerPC PC消费之业务入口 测试执行main
package com.itunion.shop.web.controller;

import com.itunion.shop.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ConsumerPC {
    @Autowired
    DemoService demoService;
    public static void main(String[] args) {
        System.setProperty("java.net.preferIPv4Stack", "true");
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"shop-pc-consumer.xml"});
        context.start();
        System.out.println("微商城PC端-消费方(Consumer)启动......");
        DemoService demoService = (DemoService) context.getBean("demoService"); // get remote service proxy
        System.out.println("消费方(Consumer)");
        while (true) {
            try {
                Thread.sleep(1000);
                String hello = demoService.sayHello("第1个:我是PC端消费方"); // call remote method
                System.out.println(hello); // get result

            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
    }
}

dubbo.properties
dubbo.qos.port=22222
log4j.properties(用上面那个)

省略....

shop-pc-consumer.xml
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    
    <dubbo:application name="shop-pc-consumer" owner="programmer" organization="dubbed"/>

    
    <dubbo:registry address="zookeeper://localhost:2181"/>

    
    <dubbo:reference id="demoService" check="false" interface="com.itunion.shop.service.DemoService"/>
beans>

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

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

SpringBoot+zk+dubbo架构实践(四):sb+zk+dubbo框架搭建(内附源码GitHub地址)_第5张图片
服务提供方启动.jpg

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

SpringBoot+zk+dubbo架构实践(四):sb+zk+dubbo框架搭建(内附源码GitHub地址)_第6张图片
shop-pc消费方调用.jpg

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

SpringBoot+zk+dubbo架构实践(四):sb+zk+dubbo框架搭建(内附源码GitHub地址)_第7张图片
shop-web消费方调用.jpg

最后预告

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

关注我们

更多精彩内容请关注“IT实战联盟”公*众*号,如果需要源码的话可以关注*公*众*号并留言(sb+zk+boot源码)会自动回复 源码GitHub地址,也可以加入交流群和作者互撩哦~~~

你可能感兴趣的:(互联网技术,微服务架构,架构实践)