转载

Spring Boot集成Dubbo 并且使用使用zookeeper作为注册中心

Spring Boot集成Dubbo 并且使用使用zookeeper作为注册中心

前言

本文全程在Windos下操作,大致操作与Linux相差无异,具体细节请百度,有条件的可以谷歌!

需要了解的知识点

  • Dubbo 和 zookeeper 环境以及了解这两个是个什么东东
  • Spring Boot 知识点,仅限于包结构 pom依赖 继承等
  • Maven 聚合工程
  • JDK 和 tomcat 环境配置等

首先环境配置

JDK 8

Maven 3.x

Spring Boot 1.5.22

tomcat 8.x

Mysql 5.5

dubbo 2.5.4

zookeeper 3.5.8

关于Dubbo 、zookeeper 以及微服务是什么东西,请百度,如不知道就不要往下看了。

首选需要了解和安装Dubbo 和 zookeeper 环境 可以查看 Windows下搭建dubbo和zookeeper环境

搭建Maven聚合项目

先新建父级工程

Spring Boot集成Dubbo 并且使用使用zookeeper作为注册中心

输入项目名和groupid 和 ArtifactId

Spring Boot集成Dubbo 并且使用使用zookeeper作为注册中心

然后在父级包上右键新建Module

Spring Boot集成Dubbo 并且使用使用zookeeper作为注册中心

如下图,点击完成即可。

Spring Boot集成Dubbo 并且使用使用zookeeper作为注册中心

等等新建结束后点击父级pom

可以看到子maven模块

Spring Boot集成Dubbo 并且使用使用zookeeper作为注册中心

项目结构

Spring Boot集成Dubbo 并且使用使用zookeeper作为注册中心

新建后的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>com.gamll</groupId>
    <artifactId>gamll-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <description>父级maven</description>

    <modules>
        <module>gmall-api</module>
<!--        <module>gmall_user</module>-->
        <module>gmall-common-util</module>
        <module>service-util</module>

        <module>gmall-user-service-dir/gmall-user-service</module>
        <module>gmall-user-service-dir/gmall-user-web</module>

    </modules>


</project>

引入Spring Boot 依赖后 和 部分依赖后,依赖维护

<?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.gamll</groupId>
    <artifactId>gamll-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <description>父级maven</description>


    <modules>
        <module>gmall-api</module>
<!--        <module>gmall_user</module>-->
        <module>gmall-common-util</module>
        <module>service-util</module>

        <module>gmall-user-service-dir/gmall-user-service</module>
        <module>gmall-user-service-dir/gmall-user-web</module>

    </modules>

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

    <packaging>pom</packaging>


    <!-- 定义依赖版本 -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>

        <fastjson.version>1.2.46</fastjson.version>
        <dubbo-starter.version>1.0.10</dubbo-starter.version>
        <dubbo.version>2.6.0</dubbo.version>
        <zkclient.version>0.10</zkclient.version>
        <mybatis.version>1.3.1</mybatis.version>
        <nekohtml.version>1.9.20</nekohtml.version>
        <xml-apis.version>1.4.01</xml-apis.version>
        <batik-ext.version>1.9.1</batik-ext.version>
        <jsoup.version>1.11.2</jsoup.version>
        <httpclient.version>4.5.5</httpclient.version>
        <commons-lang3.version>3.7</commons-lang3.version>
        <mapper-starter.version>1.2.3</mapper-starter.version>
        <jedis.version>2.9.0</jedis.version>
        <jest.version>5.3.2</jest.version>
        <jna.version>4.5.1</jna.version>
        <beanUtils.version>1.9.3</beanUtils.version>
    </properties>



    <!--依赖维护-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>${fastjson.version}</version>
            </dependency>

            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>${dubbo.version}</version>
            </dependency>

            <dependency>
                <groupId>com.101tec</groupId>
                <artifactId>zkclient</artifactId>
                <version>${zkclient.version}</version>
            </dependency>

            <dependency>
                <groupId>com.gitee.reger</groupId>
                <artifactId>spring-boot-starter-dubbo</artifactId>
                <version>${dubbo-starter.version}</version>
            </dependency>

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


            <dependency>
                <groupId>net.sourceforge.nekohtml</groupId>
                <artifactId>nekohtml</artifactId>
                <version>${nekohtml.version}</version>
            </dependency>

            <dependency>
                <groupId>xml-apis</groupId>
                <artifactId>xml-apis</artifactId>
                <version>${xml-apis.version}</version>
            </dependency>

            <dependency>
                <groupId>org.apache.xmlgraphics</groupId>
                <artifactId>batik-ext</artifactId>
                <version>${batik-ext.version}</version>
            </dependency>

            <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
            <dependency>
                <groupId>org.jsoup</groupId>
                <artifactId>jsoup</artifactId>
                <version>${jsoup.version}</version>
            </dependency>

            <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>${httpclient.version}</version>
            </dependency>

            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>${commons-lang3.version}</version>
            </dependency>


            <dependency>
                <groupId>tk.mybatis</groupId>
                <artifactId>mapper-spring-boot-starter</artifactId>
                <version>${mapper-starter.version}</version>
            </dependency>

            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>${jedis.version}</version>
            </dependency>

            <!-- https://mvnrepository.com/artifact/io.searchbox/jest -->
            <dependency>
                <groupId>io.searchbox</groupId>
                <artifactId>jest</artifactId>
                <version>${jest.version}</version>
            </dependency>

            <!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna -->
            <dependency>
                <groupId>net.java.dev.jna</groupId>
                <artifactId>jna</artifactId>
                <version>${jna.version}</version>
            </dependency>

            <dependency>
                <groupId>commons-beanutils</groupId>
                <artifactId>commons-beanutils</artifactId>
                <version>${beanUtils.version}</version>
            </dependency>

        </dependencies>


    </dependencyManagement>


</project>

gmall-api

存放实体类,和定义service接口

Spring Boot集成Dubbo 并且使用使用zookeeper作为注册中心

gmall-common-util

存放一些通用的工具方法以及service和controller都通用的依赖

Spring Boot集成Dubbo 并且使用使用zookeeper作为注册中心

service-util

存放service需要的一些依赖 比如数据库驱动 redis等。

Spring Boot集成Dubbo 并且使用使用zookeeper作为注册中心

然后gmall-user-service-dir 文件夹存放 dubbo 服务提供者和消费者

新建如上所示

gmall-user-service 服务提供者

gmall-user-web 消费者

Spring Boot集成Dubbo 并且使用使用zookeeper作为注册中心

以上每个子模块 中继承父级的pom依赖

<parent>
        <artifactId>gamll-parent</artifactId>
        <groupId>com.gamll</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

这里只贴gmall-common-util的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   
   <!-- 继承父级的maven依赖-- >
    <parent>
        <artifactId>gamll-parent</artifactId>
        <groupId>com.gamll</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>gmall-common-util</artifactId>

 
</project>

各模块引入自己的依赖jar

gmall-common-util

<?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>gamll-parent</artifactId>
        <groupId>com.gamll</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>gmall-common-util</artifactId>

    <dependencies>

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

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

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>

        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
        </dependency>

        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
        </dependency>

        <!--dubbo框架依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>

        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <!--排除log4g12日志-->
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--与sb整合 dubbo-->
        <dependency>
            <groupId>com.gitee.reger</groupId>
            <artifactId>spring-boot-starter-dubbo</artifactId>
        </dependency>


    </dependencies>

</project>

gmall-api

<?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>gamll-parent</artifactId>
        <groupId>com.gamll</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>gmall-api</artifactId>
    <description>用户实体类服务</description>


    <dependencies>

        <!--通用mapper -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>1.2.3</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-jdbc</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>
</project>

gmall-user-service

<?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>
        <artifactId>gamll-parent</artifactId>
        <groupId>com.gamll</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>com.gmall</groupId>
    <artifactId>gmall-user-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>gmall-user-service</name>
    <description>用户服务生产者 , 也就是dubbo 提供端</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>com.gamll</groupId>
            <artifactId>gmall-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.gamll</groupId>
            <artifactId>service-util</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>


        <dependency>
            <groupId>com.gmalluser</groupId>
            <artifactId>gmall_user</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>

</project>

gmall-user-web

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

    <parent>
        <artifactId>gamll-parent</artifactId>
        <groupId>com.gamll</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.gmall</groupId>
    <artifactId>gmall-user-web</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>gmall-user-web</name>
    <description>用户服务消费者  也就是dubbo 消费端 对外的接口</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>com.gamll</groupId>
            <artifactId>gmall-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.gamll</groupId>
            <artifactId>service-util</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
     <!--   Description:

        Cannot determine embedded database driver class for database type NONE-->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

    </dependencies>

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

</project>

service-util

<?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>gamll-parent</artifactId>
        <groupId>com.gamll</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>service-util</artifactId>

    <dependencies>

        <dependency>
            <groupId>com.gamll</groupId>
            <artifactId>gmall-common-util</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <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>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>


        <!--通用mapper -->
        <!-- <dependency>
             <groupId>tk.mybatis</groupId>
             <artifactId>mapper-spring-boot-starter</artifactId>
             <version>1.2.3</version>
             <exclusions>
                 <exclusion>
                     <groupId>org.springframework.boot</groupId>
                     <artifactId>spring-boot-starter-jdbc</artifactId>
                 </exclusion>
             </exclusions>
         </dependency>
 -->

    </dependencies>


</project>

搭建dobbo提供者和消费者

maven子模块服务提供者 gmall-user-service 和消费者 gmall-user-web

服务提供者

这里面新建 数据库访问 和 和 service实现类 同时在yml里配置将服务注册到 中

Spring Boot集成Dubbo 并且使用使用zookeeper作为注册中心

UmsMemberMapper

这里面提供dao查询接口 继承通用mapper接口

实体类依赖在 gmall-api 模块中

Mapper依赖在 service-util 模块中

@Mapper
public interface UmsMemberMapper extends tk.mybatis.mapper.common.Mapper<UmsMember> {

    /**
     * 查询所有
     * @return
     */
    List<UmsMember> selectAllUmsMebers();
}

UmsMemberMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.gmall.gmall.user.service.dao.UmsMemberMapper">
    <resultMap id="BaseResultMap" type="com.gamll.api.bean.UmsMember">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="member_level_id" property="memberLevelId" jdbcType="BIGINT"/>
        <result column="username" property="username" jdbcType="VARCHAR"/>
        <result column="password" property="password" jdbcType="VARCHAR"/>
        <result column="nickname" property="nickname" jdbcType="VARCHAR"/>
        <result column="phone" property="phone" jdbcType="VARCHAR"/>
        <result column="status" property="status" jdbcType="INTEGER"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="icon" property="icon" jdbcType="VARCHAR"/>
        <result column="gender" property="gender" jdbcType="INTEGER"/>
        <result column="birthday" property="birthday" jdbcType="DATE"/>
        <result column="city" property="city" jdbcType="VARCHAR"/>
        <result column="job" property="job" jdbcType="VARCHAR"/>
        <result column="personalized_signature" property="personalizedSignature" jdbcType="VARCHAR"/>
        <result column="source_type" property="sourceType" jdbcType="INTEGER"/>
        <result column="integration" property="integration" jdbcType="INTEGER"/>
        <result column="growth" property="growth" jdbcType="INTEGER"/>
        <result column="luckey_count" property="luckeyCount" jdbcType="INTEGER"/>
        <result column="history_integration" property="historyIntegration" jdbcType="INTEGER"/>
    </resultMap>
    <sql id="Base_Column_List">
    id, member_level_id, username, password, nickname, phone, status, create_time, icon, 
    gender, birthday, city, job, personalized_signature, source_type, integration, growth, 
    luckey_count, history_integration
  </sql>

    <!--  查询所有-->
    <select id="selectAllUmsMebers" resultType="com.gamll.api.bean.UmsMember">
        select
        <include refid="Base_Column_List"/>
        from ums_member
    </select>

</mapper>

ums_member sql语句

CREATE TABLE `ums_member` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `member_level_id` bigint(20) DEFAULT NULL,
  `username` varchar(64) DEFAULT NULL COMMENT '用户名',
  `password` varchar(64) DEFAULT NULL COMMENT '密码',
  `nickname` varchar(64) DEFAULT NULL COMMENT '昵称',
  `phone` varchar(64) DEFAULT NULL COMMENT '手机号码',
  `status` int(1) DEFAULT NULL COMMENT '帐号启用状态:0->禁用;1->启用',
  `create_time` datetime DEFAULT NULL COMMENT '注册时间',
  `icon` varchar(500) DEFAULT NULL COMMENT '头像',
  `gender` int(1) DEFAULT NULL COMMENT '性别:0->未知;1->男;2->女',
  `birthday` date DEFAULT NULL COMMENT '生日',
  `city` varchar(64) DEFAULT NULL COMMENT '所做城市',
  `job` varchar(100) DEFAULT NULL COMMENT '职业',
  `personalized_signature` varchar(200) DEFAULT NULL COMMENT '个性签名',
  `source_type` int(1) DEFAULT NULL COMMENT '用户来源',
  `integration` int(11) DEFAULT NULL COMMENT '积分',
  `growth` int(11) DEFAULT NULL COMMENT '成长值',
  `luckey_count` int(11) DEFAULT NULL COMMENT '剩余抽奖次数',
  `history_integration` int(11) DEFAULT NULL COMMENT '历史积分数量',
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_username` (`username`),
  UNIQUE KEY `idx_phone` (`phone`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8 COMMENT='会员表';

服务提供者类

UmsMemberServiceImpl

需要注意的是以前我们在单体服务中 service实现类采用Spring 提供的 @service 注解将bean注入到IOC容器中

但现在我们是用dubbo实现微服务,需要使用dubbo的 @service 注解将该服务注册到zookeeper中,让消费者从容器中获取进行消费。

`顾名思义就是提供服务供别人调用的,相当于spring中的Service的实现类。

使用也很简单,就是一个注解加一份配置

提供端在实现类上增加注解 @Service,和spring的是一样的但是引的包是不一样的。`

package com.gmall.gmall.user.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.gamll.api.bean.UmsMember;
import com.gamll.api.service.UmsMemberService;
import com.gmall.gmall.user.service.dao.UmsMemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service //这里@Service 注解需要使用dubbo 的注解
public class UmsMemberServiceImpl implements UmsMemberService {


    @Autowired
    private UmsMemberMapper umsMemberMapper;

    @Override
    public List<UmsMember> getUmsMemberList() {

        return umsMemberMapper.selectAllUmsMebers();
    }

    @Override
    public UmsMember findUmsMemberById(long id) {

        return umsMemberMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class,RuntimeException.class})
    public int addUmsMember(UmsMember umb) {
        return umsMemberMapper.insert(umb);
    }


    @Override
    @Transactional(rollbackFor = {Exception.class,RuntimeException.class})
    public int updateUmsMember(UmsMember umb) {

        return umsMemberMapper.updateByPrimaryKey(umb);
    }


}

main方法

需要开启Mapper扫描 这里使用了通用Mapper 必须用到通用mapper的 @MapperScan 注解扫描mapper

以及开启事务

@SpringBootApplication
@tk.mybatis.spring.annotation.MapperScan("com.gmall.gmall.user.service.dao")
@EnableTransactionManagement
public class GmallUserServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(GmallUserServiceApplication.class, args);
    }

}

最后yml中配置注册中心以及dubbo中的服务信息以及mybatis的配置

server:
  port: 10002

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/gmall0105?useunicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
    username: root
    password: root
    driver-class-name:  com.mysql.jdbc.Driver

  dubbo:
    # dubbo中的服务名称
    application:
      name: user-service-server
    protocol:
      # dubbo中的通信协议名称
      name:  dubbo
    registry:
      # zookeeper注册中心的地址加端口号
      address: 127.0.0.1:2181
      # zookeeper注册中心协议
      protocol: zookeeper
    # dubbo的服务扫描路径
    base-package: com.gmall



#mybatis 映射 xml和pojo映射
mybatis:
  type-aliases-package: com.gmalluser.pojo
  mapper-locations: classpath:mapper/*.xml


logging:
  level: debug

最后启动一下Spring Boot

别忘了启动dubbo和zookeeper

Spring Boot集成Dubbo 并且使用使用zookeeper作为注册中心

可以看到已经将服务注册到zookeeper中了

打开dubbo服务监控页面

Spring Boot集成Dubbo 并且使用使用zookeeper作为注册中心

可以看到服务提供者

服务消费者

上面已经将服务注册到zookeeper中了,这里我们将服务拿出来进行消费

Spring Boot集成Dubbo 并且使用使用zookeeper作为注册中心

首先配置Yml

配置注册地址端口等协议

server:
  port: 10003



spring:
  dubbo:
    # dubbo中的服务名称
    application:
      name: user-web
    protocol:
      # dubbo中的通信协议名称
      name:  dubbo
    registry:
      # zookeeper注册中心的地址加端口号
      address: 127.0.0.1:2181
      # zookeeper注册中心协议
      protocol: zookeeper
    # dubbo的服务扫描路径
    base-package: com.gmall
    consumer:
      # 访问提供端服务的超时时间,默认是1000毫秒
      timeout: 30000
      # 是启动消费端时,是否检查服务端能否正常访问。如果选择true,那启动消费端时,必须保证提供端服务正常,否则接口无法注入
      check: false

UmsMemberController

新建controller 消费服务查询数据

以前在单体中我们是从IOC容器中将bean拿出来然后调用方法进行查询,但在dubbo分布式项目中是用dubbo 的service 也就是用 @Reference 将服务从zookeeper拿出来,注入到controller中 然后调用指定方法进行查询

@RequestMapping("/api/umb")
@RestController
public class UmsMemberController {

    //dubbo 的service
    @Reference
    private UmsMemberService umsMemberService;

    @RequestMapping("/findAll")
    public List<UmsMember> all() {
        return umsMemberService.getUmsMemberList();
    }


    @RequestMapping("/findById")
    public UmsMember all(long id) {
        return umsMemberService.findUmsMemberById(id);
    }


}

然后启动消费者

如果启动报错

java.lang.reflect.InvocationTargetException: null
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
    at com.alibaba.dubbo.config.AbstractConfig.toString(AbstractConfig.java:474) ~[dubbo-2.6.2.jar:2.6.2]
    at java.lang.String.valueOf(String.java:2994) [na:1.8.0_181]
    at java.lang.StringBuilder.append(StringBuilder.java:131) [na:1.8.0_181]
    at com.alibaba.dubbo.config.spring.beans.factory.annotation.AbstractAnnotationConfigBeanBuilder.build(AbstractAnnotationConfigBeanBuilder.java:79) [dubbo-2.6.2.jar:2.6.2]
    at com.alibaba.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.buildReferenceBean(ReferenceAnnotationBeanPostProcessor.java:385) [dubbo-2.6.2.jar:2.6.2]
    at com.alibaba.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.access$100(ReferenceAnnotationBeanPostProcessor.java:65) [dubbo-2.6.2.jar:2.6.2]
    at com.alibaba.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor$ReferenceFieldElement.inject(ReferenceAnnotationBeanPostProcessor.java:363) [dubbo-2.6.2.jar:2.6.2]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) [spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at com.alibaba.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.postProcessPropertyValues(ReferenceAnnotationBeanPostProcessor.java:92) [dubbo-2.6.2.jar:2.6.2]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1400) [spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) [spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) [spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) [spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) [spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) [spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1247) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1167) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) [spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1395) [spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) [spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) [spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) [spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) [spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) [spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:849) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at com.springboot.dubbo.DubboApplication.main(DubboApplication.java:12) ~[classes/:na]
Caused by: java.lang.IllegalStateException: Failed to check the status of the service com.spring.boot.service.UserService. No provider available for the service com.spring.boot.service.UserService from the url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=spring-boot-dubbo-consumer&dubbo=2.6.2&interface=com.spring.boot.service.UserService&methods=getUser&pid=40168&register.ip=192.168.1.5&side=consumer&timestamp=1553586960033 to the consumer 192.168.1.5 use dubbo version 2.6.2
    at com.alibaba.dubbo.config.ReferenceConfig.createProxy(ReferenceConfig.java:422) ~[dubbo-2.6.2.jar:2.6.2]
    at com.alibaba.dubbo.config.ReferenceConfig.init(ReferenceConfig.java:333) ~[dubbo-2.6.2.jar:2.6.2]
    at com.alibaba.dubbo.config.ReferenceConfig.get(ReferenceConfig.java:163) ~[dubbo-2.6.2.jar:2.6.2]
    at com.alibaba.dubbo.config.spring.ReferenceBean.getObject(ReferenceBean.java:66) ~[dubbo-2.6.2.jar:2.6.2]
    ... 43 common frames omitted

是因为 main方法上服务提供者的主类上没有配置注解

@SpringBootApplication
@EnableDubbo
public class GmallUserWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(GmallUserWebApplication.class, args);
    }

}

然后启动即可

可以发现成功注册

Spring Boot集成Dubbo 并且使用使用zookeeper作为注册中心

调用查询接口测试

发现成功查询出数据

Spring Boot集成Dubbo 并且使用使用zookeeper作为注册中心

分布式就可以基于这种方式实现不同模块间的调用。每一个实现服务的消费端和提供端分离。

dubbo和zookeeper分布式架构

Spring Boot集成Dubbo 并且使用使用zookeeper作为注册中心

执行流程:

  • Provider:服务的提供者,负责对外提供服务,提供者在启动的时候,需要向Registry注册自己能够提供

的服务

  • Consumer:服务的消费者,消费者在启动的时候,需要向Registry订阅自己需要的服务
  • Registry:注册中心,授受注册和订阅,会异步的通知订阅者,向消费者提供服务列表

当消费者需要执行远程过程调用时,会从Registry获取到服务地址列表(基于负载均衡算法)进行调用,

如果调用失败会重新选择新的提供者再次调用

  • Monitor:监控中心,统计服务的调用次数和调用时间,服务消费者和提供者会在内存中累计调用次数和

调用时间,定时每分钟向监控中心发送一次统计数据

代码地址 分支 gmall0105

原文  https://segmentfault.com/a/1190000023028762
正文到此结束
Loading...