springboot+dubbo+zookeeper基础学习

小菜鸟记录第一次写博客

起始阶段:
zookeeper官网下载地址:https://www.apache.org/dyn/closer.cgi/zookeeper/
这里写图片描述

下载成功后进入zookeeper/bin目录下点击zkServer.cmd,查看2181端口是否开启,开启则成功

dubbo下载地址:https://github.com/apache/incubator-dubbo

下载完成后把dubbo-admin打成war包部署在tomcat的webapps下面,启动tomcat后访问dubbo项目名即可

注意:先启动zookeeper,再启动tomcat

项目搭建:

provider:
目录结构
这里写图片描述
pom文件

<!-- springboot项目必须需要一个parent -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.2.RELEASE</version>
</parent>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <springboot.version>1.3.2.RELEASE</springboot.version>
    <!--<swagger2.version>2.2.2</swagger2.version> -->
    <mybatis.version>1.1.1</mybatis.version>
    <!-- <mongodb.version>3.2.2</mongodb.version> -->
</properties>

<dependencies>
    <!-- springboot 基础包 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <!-- 引入log4j日志时需去掉默认的logback -->
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- 日志管理 log4j -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j</artifactId>
    </dependency>
    <!-- springboot 测试包 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- springboot web包 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- 添加dubbo依赖 -->  
    <dependency> 
      <groupId>com.alibaba</groupId>  
      <artifactId>dubbo</artifactId>  
      <version>2.5.10</version>  
      <exclusions> 
        <exclusion> 
          <groupId>org.springframework</groupId>  
          <artifactId>spring</artifactId> 
        </exclusion> 
      </exclusions> 
    </dependency> 
    <!-- 添加zk客户端依赖 -->   
    <dependency> 
      <groupId>com.github.sgroschupf</groupId>  
      <artifactId>zkclient</artifactId>  
      <version>0.1</version> 
    </dependency> 
    <!-- zookeeperjar包 -->
    <dependency>  
        <groupId>org.apache.zookeeper</groupId>  
        <artifactId>zookeeper</artifactId>  
        <version>3.4.9</version>  
    </dependency>
     <!-- thymeleaf -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <!--数据库相关-->
    <!-- springboot jdbc -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <!-- mysql支持 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <!-- Spring-data-jpa -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <!-- 整合mybatis -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>${mybatis.version}</version>
    </dependency>

    <!-- aop -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
   <!-- alibaba的druid数据库连接池 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.0</version>
    </dependency> 


    <!-- springboot工具 修改代码后不需重启即生效 -->
    <!-- spring boot devtools 依赖包. -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
        <scope>true</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>springloaded</artifactId>
    </dependency>
    <dependency>
        <groupId>com.csf.springbootdzDemo</groupId>
        <artifactId>springboot-dubbo-interface</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>

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

springboot-provider配置文件
这里写图片描述

provider启动类:
配置@ImportResource让启动类启动的时候扫描provider配置文件
这里写图片描述

consumer
目录结构
这里写图片描述
pom文件

<!-- springboot项目必须需要一个parent -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.2.RELEASE</version>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <springboot.version>1.3.2.RELEASE</springboot.version>
    <!--<swagger2.version>2.2.2</swagger2.version> -->
    <mybatis.version>1.1.1</mybatis.version>
    <!-- <mongodb.version>3.2.2</mongodb.version> -->
</properties>

<dependencies>
    <!-- springboot 基础包 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <!-- 引入log4j日志时需去掉默认的logback -->
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- 日志管理 log4j -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j</artifactId>
    </dependency>
    <!-- springboot 测试包 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- springboot web包 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- 添加dubbo依赖 -->  
    <dependency> 
      <groupId>com.alibaba</groupId>  
      <artifactId>dubbo</artifactId>  
      <version>2.5.10</version>  
      <exclusions> 
        <exclusion> 
          <groupId>org.springframework</groupId>  
          <artifactId>spring</artifactId> 
        </exclusion> 
      </exclusions> 
    </dependency> 
    <!-- 添加zk客户端依赖 -->   
    <dependency> 
      <groupId>com.github.sgroschupf</groupId>  
      <artifactId>zkclient</artifactId>  
      <version>0.1</version> 
    </dependency> 
    <!-- zookeeperjar包 -->
    <dependency>  
        <groupId>org.apache.zookeeper</groupId>  
        <artifactId>zookeeper</artifactId>  
        <version>3.4.9</version>  
    </dependency>
    <!-- thymeleaf -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <!--数据库相关-->
    <!-- springboot jdbc -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <!-- mysql支持 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <!-- Spring-data-jpa -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <!-- 整合mybatis -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>${mybatis.version}</version>
    </dependency>

    <!-- aop -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    <!-- alibaba的druid数据库连接池 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.0</version>
    </dependency> 

    <!-- springboot工具 修改代码后不需重启即生效 -->
    <!-- spring boot devtools 依赖包. -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
        <scope>true</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>springloaded</artifactId>
    </dependency>
    <dependency>
        <groupId>com.csf.springbootdzDemo</groupId>
        <artifactId>springboot-dubbo-interface</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>

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

springboot-consumer配置文件
这里写图片描述

consumer启动类:
配置@ImportResource让启动类启动的时候扫描consumer配置文件
这里写图片描述

interface(此处service用于暴露接口供消费者调用)
目录结构
这里写图片描述

代码实现流程

暴露服务的接口
这里写图片描述

位于provider的具体实现类
这里写图片描述

controller
这里写图片描述

总结:
这里写图片描述
dubbo作为一个分布式服务框架,需要服务提供者,服务消费者以及用于暴露服务的接口,如上图所示,服务提供者在zookeeper注册中心注册服务,由服务消费者进行调用,如果所注册的服务崩了,基于zookeeper自身负载均衡算法进行计算再选择下一个,而dubbo则作为监控中心记录请求数和请求时间

这次的项目搭建实为第二次,第一次搭建的时候由于初学,不太弄得清楚他的实现原理以及流程,所以在dubbo主界面始终看不到消费者,现在知道是服务提供者提供服务,把服务暴露在接口,由消费方去调用暴露于接口的服务就好了~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值