搭建基于dubbo,zookeeper的spring boot分布式系统示例

  • 内容
  • 评论
  • 相关

目前越来越多的系统采用分布式,微服务的结构,将业务根据模块进行拆分,然后分布式部署,采用RPC的方式调用服务,这样的架构灵活,高效,这里就简单介绍一下如何搭建基于dubbo,zookeeper的spring boot分布式系统,过程很简单,具体的复杂实践还需要在企业级应用中练习。

首先介绍一下用到的各个组件和框架。

spring boot,首先引用官方的介绍,最为准确,Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".spring boot让你快速搭建一个技术spring的应用,大大简化了传统spring项目的配置,使用起步依赖和自动配置简化配置流程,快速搭建一个模块或者应用。

dubbo,引用官方介绍,Apache Dubbo™ (incubating) is a high-performance, java based open source RPC framework高性能的RPC框架,比如多台服务器之间服务互相调用,就可以使用它,性能很高,就好比从本地调用,其实我们在蚂蚁没有用这个框架,我们用的自主开发的分布式开发框架也集成了此功能,当然具体应用看自己需求了。

zkoopeer,同样引用官方介绍,Apache ZooKeeper is an effort to develop and maintain an open-source server which enables highly reliable distributed coordination.主要是一个服务协调的软件。具体功能包括了很多,ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services。在本教程中,我们将其作为注册中心使用。对于生产者,可以在zk上注册服务地址,消费者通过在注册中心获取注册地址来获取服务。

首先预览一下项目的整个目录结构


这是一个多模块的maven项目,主项目是dubbo-zookeeper,下面有三个子模块,分别是interface,provider,consumer,作用分别是定义服务接口,生产者,消费者。


首先,新建一个spring boot的项目,使用idea搭建很快,细节不表,然后分别建立三个子模块,子模块建立好之后,注意设置一下pom文件,讲parent设置为主项目,比如给出一个provider的配置示例

<?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.0</modelVersion>

    <groupId>com.gaoxueping</groupId>
    <artifactId>dubbo-zookeeper-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>dubbo-zookeeper-provider</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.gaoxueping</groupId>
        <artifactId>dubbo-zookeeper</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.gaoxueping</groupId>
            <artifactId>duboo-zookeeper-interface</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

同时在父项目中设置一下pom,加入一下modules标签,讲子模块加入进去

<?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.0</modelVersion>

    <groupId>com.gaoxueping</groupId>
    <artifactId>dubbo-zookeeper</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <modules>
        <module>duboo-zookeeper-interface</module>
        <module>dubbo-zookeeper-provider</module>
        <module>dubbo-zookeeper-consumer</module>
    </modules>

    <name>dubbo-zookeeper</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

三个子模块建立好,分别配置resource文件,配置zk的注册信息,这样,通过zk的注册中心,生产者和消费者可以实现通。这里给出provider的配置信息。

<?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
    ">

    <description>Dubbo Demo Service</description>
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="dubbo-zookeeper-provider" />

    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"
                    client="zkclient" />

    <!-- dubbo缓存 -->
    <dubbo:protocol id="dubbo" name="dubbo" port="20880"
                    threadpool="cached" threads="100" />

    <!-- Greeting服务 -->
    <bean id="userService" class="com.gaoxueping.dubbozookeeperprovider.UserServiceImpl" />

    <dubbo:service protocol="dubbo"
                   interface="com.gaoxueping.dubooookeeperinterface.UserService" ref="userService" />
</beans>

同样的方式配置完consumer,然后分别启动zookeeper,provider,consumer。就可以实现功能了。

项目的github地址为:https://github.com/hellogxp/dubbo-zookeeper,大家可以现在下来参考

评论

0条评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注