步骤

  1. 安装、启动、zookeeper
  2. 搭建父级项目parent
  3. 创建common模块
  4. 创建provider模块
  5. 创建customer模块
  6. customer访问provider服务

一、下载安装zookeeper(此处使用的为window环境,Linux环境同理)

zookeeper安装启动可参考此博客zookeeper window安装

二:搭建parent项目

idea创建springboot项目命名为parent即可,后续为parent创建子模块,最终项目接口如下图

spring boot zookpeer 服务端 调用 客户端接口_java


修改pom文件

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.demo</groupId>
    <artifactId>parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>parent</name>
    <description>Demo project for Spring Boot</description>
    <packaging>jar</packaging>

    <modules>
        <!-- 服务消费者工程 -->
        <module>customer</module>
        <!--  服务提供者工程-->
        <module>provider</module>
        <!-- 主要放实体、接口、也可以放一些公用的工具类工程-->
        <module>common</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <dubbo.version>2.5.5</dubbo.version>
        <zkclient.version>0.10</zkclient.version>
        <lombok.version>1.16.18</lombok.version>
        <spring-boot.version>1.5.7.RELEASE</spring-boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <!-- 使用lombok实现JavaBean的get、set、toString、hashCode、equals等方法的自动生成  -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <scope>provided</scope>
        </dependency>

        <!-- Dubbo依赖 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>${dubbo.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--zookeeper的客户端依赖-->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>${zkclient.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>UTF-8</encoding>
                </configuration>

            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

parent父工程到这里就可以不动他了,如果你的子模块有变动再更新

三、创建common模块

对parent项目创建cmmon子模块,common模块一般用于存放公共的实体类、工具类等公用信息。

此处用于仅用于测试所以只创建一个user对象以及userservice

最终结构如下图(此处有个不影响使用的问题,应该时domain,结果拼成了domin了,当然也可自行更改为entity之类的 看个人习惯)

spring boot zookpeer 服务端 调用 客户端接口_spring_02


User.java(需要实现Serializable 接口)

package com.demo.common.domin;

import java.io.Serializable;

/**
 * @ClassName: User
 * @Package: com.demo.common.domin
 * @Description:
 * @Datetime: 2020/7/10   17:04
 * @author: kaishun.zhou
 */
public class User implements Serializable {

    private String id;

    private String username;

    private String password;

    private Integer age;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

UserService.java

package com.demo.common.service;

import com.demo.common.domin.User;

import java.util.List;

/**
 * @ClassName: UserService
 * @Package: com.demo.common.service
 * @Description:
 * @Datetime: 2020/7/10   17:05
 * @author: kaishun.zhou
 */
public interface UserService {

    List<User> findAll();

}

common模块到此结束。pom以及application默认即可,此案例中可以不使用。

四、创建provider模块

创建provider模块去具体实现common下的服务。实际开发中会有多个provider模块以及多种服务以及实现。
首先修改pom文件

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.demo</groupId>
    <artifactId>provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>provider</name>
    <packaging>jar</packaging>
    <description>Demo project for Spring Boot</description>


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

    <dependencies>
        <!--引入自己的common模块-->
        <dependency>
            <groupId>com.demo</groupId>
            <artifactId>common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

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

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


        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.5</version>
            <exclusions>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--zookeeper的客户端依赖-->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>${zkclient.version}</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
        </dependency>
        <!-- alibaba的druid数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.11</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

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


</project>

创建service的实现impl、创建mapper、修改application.yml、创建spring-dubbo.xml

结构如图(application.properties为默认带的,可删除,也可直接修改后缀为yml)

spring boot zookpeer 服务端 调用 客户端接口_java_03


UserMapper.java

package com.demo.provider.mapper;

import com.demo.common.domin.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @ClassName: UserMapper
 * @Package: com.demo.provider.mapper
 * @Description:
 * @Datetime: 2020/7/10   17:10
 * @author: kaishun.zhou
 */
@Mapper
public interface UserMapper {

    @Results(id = "userMap", value = {
            @Result(column = "id", property = "id"),
            @Result(column = "username", property = "username"),
            @Result(column = "age", property = "age"),
            @Result(column = "password", property = "password")})
    @Select("SELECT * FROM user")
    List<User> getAll();

}

添加mapper后对应的启动类需要添加扫描后面会贴代码

UserServiceImpl.java

package com.demo.provider.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.demo.common.domin.User;
import com.demo.common.service.UserService;
import com.demo.provider.mapper.UserMapper;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName: UserImpl
 * @Package: com.demo.provider.service.impl
 * @Description:
 * @Datetime: 2020/7/10   17:09
 * @author: kaishun.zhou
 */
@Service(version = "1.0.0")
public class UserImpl implements UserService {


    @Resource
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {
        System.out.println("进入提供者实现体");
        List<User> list = userMapper.getAll();
        System.out.println("用户数量:"+list.size());
        return list;
    }
}

注意:
此处的@Service为com.alibaba.dubbo.config.annotation.Service;
而不是org.springframework.stereotype.Service
此处的demo测试中版本号可加可不加

application.yml

server:
  port: 8082
spring:
  datasource:
    name: dubbo_demo
    url: jdbc:mysql://127.0.0.1:3306/dubbo_demo
    username: root
    password: 123456
    # 使用druid数据源
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver

mybatis:
  type-aliases-package: domain

此处provider模块为8082端口,后面customer模块为8081端口

创建spring-dubbo.xml

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

    <dubbo:application name="provider"/>
    <!-- 注册中心的ip地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <!-- 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类 -->
    <dubbo:annotation package="service.impl"/>

    <!-- use dubbo protocol to export service on port 20880 -->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!-- service implementation, as same as regular local bean -->
    <bean id="userService" class="com.demo.provider.service.impl.UserImpl"/>
    <!-- declare the service interface to be exported -->
    <dubbo:service interface="com.demo.common.service.UserService" ref="userService"/>
</beans>

此文件中配置参数路径根据自己配置路径修改。

修改启动类ProviderApplication.java

package com.demo.provider;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource({"classpath:spring-dubbo.xml"})
@MapperScan("mapper")
public class ProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
        System.out.println("服务端启动成功!!!");

    }

}

provider模块至此结束

五、创建customer模块

创建customer模块去调用provider模块下服务。
修改pom文件

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.demo</groupId>
    <artifactId>customer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>customer</name>
    <packaging>jar</packaging>
    <description>Demo project for Spring Boot</description>

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


    <dependencies>
        <!--引入自己的common模块-->
        <dependency>
            <groupId>com.demo</groupId>
            <artifactId>common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.5</version>
            <exclusions>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--zookeeper的客户端依赖-->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>${zkclient.version}</version>
        </dependency>
    </dependencies>

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

</project>

创建UserController,创建application.yml或者直接修改application.properties文件,此处用于测试选择的直接修改application.properties,至于为何不新建ym原因在下面、创建spring-dubbo.xml

最终结构如下图:

spring boot zookpeer 服务端 调用 客户端接口_spring_04


UserController.java

package com.demo.customer.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.demo.common.domin.User;
import com.demo.common.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName: UserController
 * @Package: com.demo.customer.controller
 * @Description:
 * @Datetime: 2020/7/10   17:15
 * @author: kaishun.zhou
 */
@RestController
public class UserController {

    @Resource
    @Reference(version = "1.0.0")
    private UserService userService;

    @GetMapping("getUser")
    public List<User> user(){
        System.out.println("进入消费者方法");
        return userService.findAll();
    }

}

application.properties(这就是为啥不新建yml的原因)

server.port=8081

spring-dubbo.xml

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

    <dubbo:application name="consumer"/>
    <dubbo:registry address="zookeeper://192.168.5.13:2181"/>
    <dubbo:protocol name="dubbo" port="20880"/>
    <dubbo:reference id="userService" check="false" interface="com.demo.common.service.UserService"/>
    <dubbo:annotation package="controller"/>
</beans>

同理,配置参数根据自己的路径进行修改
修改启动类CustomerApplication.java

package com.demo.customer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource({"classpath:spring-dubbo.xml"})
public class CustomerApplication {

    public static void main(String[] args) {
        SpringApplication.run(CustomerApplication.class, args);
        System.out.println("消费端启动成功!!!");
    }

}

customer模块结束

创建数据库dubbo-demo,并创建user表,字段与上述domain一致即可

CREATE TABLE `user` (
  `id` varchar(32) NOT NULL,
  `username` varchar(100) DEFAULT NULL,
  `password` varchar(100) DEFAULT NULL,
  `age` int(3) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

添加模拟数据

INSERT INTO `dubbo_demo`.`user` (`id`, `username`, `password`, `age`) VALUES ('1', 'tom', '123456', '22');

至此即可启动项目测试。
首先确保zookeeper,根据上述博客安装完毕后进入bin目录下,打开zkServer.cmd和zkCli.cmd

spring boot zookpeer 服务端 调用 客户端接口_spring_05


spring boot zookpeer 服务端 调用 客户端接口_maven_06


启动provider

spring boot zookpeer 服务端 调用 客户端接口_spring_07

启动customer

spring boot zookpeer 服务端 调用 客户端接口_maven_08


访问 http://localhost:8081/getUser

spring boot zookpeer 服务端 调用 客户端接口_Java dubbo_09


customer控制台日志

spring boot zookpeer 服务端 调用 客户端接口_spring_10


provider控制台日志

spring boot zookpeer 服务端 调用 客户端接口_java_11


到这里这个demo就结束了,但是或许有人会有问题,比如我们平时使用mybatis开发时可以通过easycode或者generator插件等自动生成实体类、service、imp、dao、xml等文件,此处却用的mapper接口以及@Select注解实现查询。可能有些人并不习惯甚至繁琐。这里其实也可以改为我们传统的dao、xml开发模式。为了不混淆我将方法写在下一篇博客中

至此结束。下面内容可看可不看

也可以安装dubbo-admin进行查看服务提供者、消费者详情。

进入dubbo-admin官网

spring boot zookpeer 服务端 调用 客户端接口_Java dubbo_12


点击GitHub地址后下拉找到下图部分

spring boot zookpeer 服务端 调用 客户端接口_java_13


点击跳转后切换到master分支下载dubbo-admin

spring boot zookpeer 服务端 调用 客户端接口_spring_14


(不同版本获取会有少许差异,可根据自己版本进行更改)

我把我下载的分享在百度云,有需要的可自行下载。

链接:https://pan.baidu.com/s/1Pdwi6-tOMCkJPTJJOgHsag

提取码:pexh

我下载的版本结构如下图:

spring boot zookpeer 服务端 调用 客户端接口_java_15


不同的版本可能需要对dubbo-admin下的application.properties进行修改,我下周的版本因为地址也都在本地所以没有修改

spring boot zookpeer 服务端 调用 客户端接口_java_16


之后对dubbo-admin进行maven打包

spring boot zookpeer 服务端 调用 客户端接口_Java dubbo_17


打包成功后在项目target目录下会出现 dubbo-admin-0.0.1-SNAPSHOT.jar 包,我这里为了方便重命名为dubbo-admin.jar

spring boot zookpeer 服务端 调用 客户端接口_java_18


在此目录下进入命令行输入:java -jar -文件名 运行dubbo-admin

spring boot zookpeer 服务端 调用 客户端接口_java_19


启动成功后访问:http://localhost:7001/

提示登录账号密码:默认都是root

看到如下页面说明成功了

spring boot zookpeer 服务端 调用 客户端接口_java_20


可在服务治理菜单下查看服务/应用/提供者/消费者等信息

spring boot zookpeer 服务端 调用 客户端接口_Java dubbo_21