Spring boot integrates Dubbo and uses zookeeper as the registry

Time:2021-6-12

Spring boot integrates Dubbo and uses zookeeper as the registry

preface

In this paper, the whole operation in windows, roughly the same operation with Linux, specific details please Baidu, conditional can Google!

Knowledge points to understand

  • Dubbo and zookeeper environments and understanding what they are
  • Spring boot knowledge is limited to package structure, POM, dependency inheritance, etc
  • Maven polymerization Engineering
  • JDK and Tomcat environment configuration

First, configure the environment

JDK 8
Maven 3.x
Spring Boot 1.5.22
tomcat 8.x
Mysql 5.5
dubbo 2.5.4
zookeeper 3.5.8

About Dubbo, zookeeper and what is micro service, please Baidu, if you don’t know, don’t look down.

First, you need to understand and install Dubbo and zookeeper environment. You can view how to build Dubbo and zookeeper environment under windows

Build Maven aggregation project

Create a new parent project first

Spring boot integrates Dubbo and uses zookeeper as the registry
Enter the project name and groupid and artifactid

Spring boot integrates Dubbo and uses zookeeper as the registry
Then right click on the parent package to create a new module
Spring boot integrates Dubbo and uses zookeeper as the registry
As shown in the figure below, click finish.

Spring boot integrates Dubbo and uses zookeeper as the registry

And so on. After the creation, click the parent POM
You can see the sub Maven module
Spring boot integrates Dubbo and uses zookeeper as the registry

Project structure

Spring boot integrates Dubbo and uses zookeeper as the registry

New POM file

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

After introducing spring boot dependency and partial dependency, dependency maintenance

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


    <!--  Define dependent version -- >
    <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>



    <!-- Dependency maintenance -- >
    <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

Store entity class and define service interface

Spring boot integrates Dubbo and uses zookeeper as the registry

gmall-common-util

Store some common tools, methods, and dependencies common to both service and controller
Spring boot integrates Dubbo and uses zookeeper as the registry

service-util

Store some dependencies required by the service, such as database driven redis.
Spring boot integrates Dubbo and uses zookeeper as the registry

Then, the GMALL user service dir folder stores the Dubbo service providers and consumers

New as shown above

GMALL user service provider
GMALL user web consumer

Spring boot integrates Dubbo and uses zookeeper as the registry

Each of the above submodules inherits the POM dependency of the parent

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

Only the POM file of GMALL common util is pasted here. The structure of other sub modules is the same

<?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">
   
   <!--  Inherits the parent's Maven dependency -- >
    <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>

Each module introduces its own dependency 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 framework dependency -- >
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>

        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <!-- Exclude log4g12 log -- >
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- Integrating with sb
        <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 > user entity class service


    <dependencies>

        <!-- General 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 > user service producer, that is, Dubbo provider

    <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 > user service consumer is the external interface of Dubbo consumer end < / description > user service consumer

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


        <!-- General 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>

Building dobbo providers and consumers

Maven submodule service providergmall-user-serviceAnd consumersgmall-user-web

Service provider

The new database access and service implementation classes are configured in YML to register the service in

Spring boot integrates Dubbo and uses zookeeper as the registry

UmsMemberMapper

It provides Dao query interface and inherits general mapper interface

Entity classes depend ongmall-apiIn the module
Mapper relies onservice-utilIn the module

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

    /**
     *Query all
     * @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>

    <!--   Query all -- >
    <select id="selectAllUmsMebers" resultType="com.gamll.api.bean.UmsMember">
        select
        <include refid="Base_Column_List"/>
        from ums_member
    </select>

</mapper>

ums_ Member SQL statement

CREATE TABLE `ums_member` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `member_level_id` bigint(20) DEFAULT NULL,
  `User name ` varchar (64) default null comment 'user name',
  `Password ` varchar (64) default null comment 'password',
  `Nickname ` varchar (64) default null comment ',
  `Phone ` varchar (64) default null comment 'mobile number',
  `Status ` int (1) default null comment 'account enabled status: 0 - > disabled; 1 - > enable ',
  `create_ Time ` datetime default null comment 'registration time',
  `Icon ` varchar (500) default null comment 'Avatar',
  `Gender ` int (1) default null comment 'gender: 0 - > unknown; 1 - > male; 2 - > female ',
  `Birthday ` date default null comment 'birthday',
  `City ` varchar (64) default null comment 'city',
  `Job ` varchar (100) default null comment 'occupation',
  `personalized_ Signature ` varchar (200) default null comment 'personal signature',
  `source_ Type ` int (1) default null comment 'user source',
  `Integration ` int (11) default null comment 'integral',
  `Growth ` int (11) default null comment 'growth value',
  `luckey_ Count ` int (11) default null comment 'number of Raffles left',
  `history_ Integration ` int (11) default null comment 'number of historical points',
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_username` (`username`),
  UNIQUE KEY `idx_phone` (`phone`)
) ENGINE=InnoDB AUTO_ Increment = 11 default charset = utf8 comment ='member table ';

Service provider class

UmsMemberServiceImpl

It should be noted that in the past, we used the service implementation class provided by spring in the single service@serviceAnnotations inject beans into the IOC container
But now we are using Dubbo to implement micro services. We need to use Dubbo@serviceAnnotation will register the service in zookeeper, so that consumers can get it from the container for consumption.

`As the name suggests, it provides services for others to call, which is equivalent to the implementation class of service in spring.
It’s also very simple to use, that is, a comment and a configuration
The provider adds annotation @ service to the implementation class, which is the same as spring’s, but the referenced package is different`

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 // here @ service annotation needs to use Dubbo annotation
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 method
Mapper scanning needs to be started. The general mapper is used here, and the general mapper must be used@MapperScanAnnotation scanning mapper
And open the transaction

@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);
    }

}

Finally, configure the registry in YML, the service information in Dubbo and the configuration of 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:
    #Service name in Dubbo
    application:
      name: user-service-server
    protocol:
      #The name of communication protocol in Dubbo
      name:  dubbo
    registry:
      #Address of zookeeper registry plus slogan
      address: 127.0.0.1:2181
      #Zookeeper registry agreement
      protocol: zookeeper
    #Service scanning path of Dubbo
    base-package: com.gmall



#Mybatis mapping, XML and POJO mapping
mybatis:
  type-aliases-package: com.gmalluser.pojo
  mapper-locations: classpath:mapper/*.xml


logging:
  level: debug

Finally, start spring boot
Don’t forget to start Dubbo and zookeeper

Spring boot integrates Dubbo and uses zookeeper as the registry
You can see that the service has been registered in zookeeper

Open the Dubbo service monitoring page

Spring boot integrates Dubbo and uses zookeeper as the registry
You can see the service provider

Service consumers

The above service has been registered in zookeeper. Here we take out the service for consumption

Spring boot integrates Dubbo and uses zookeeper as the registry

First configure YML

Configure the registration address, port and other protocols

server:
  port: 10003



spring:
  dubbo:
    #Service name in Dubbo
    application:
      name: user-web
    protocol:
      #The name of communication protocol in Dubbo
      name:  dubbo
    registry:
      #Address of zookeeper registry plus slogan
      address: 127.0.0.1:2181
      #Zookeeper registry agreement
      protocol: zookeeper
    #Service scanning path of Dubbo
    base-package: com.gmall
    consumer:
      #The timeout for accessing the provider service, which is 1000 ms by default
      timeout: 30000
      #Is whether to check whether the server can access normally when starting the consumer. If you select true, you must ensure that the service of the provider is normal when starting the consumer, otherwise the interface cannot be injected
      check: false

UmsMemberController

New controller consumer service query data

In the past, we took the bean out of the IOC container and then called the method to query in the monomer, but in the Dubbo distributed project, we used Dubbo service. @ReferenceTake the service out of zookeeper, inject it into controller, and then call the specified method to query.

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

    //Dubbo's 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);
    }


}

Then start the consumer

If start, report an error

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

This is because there is no configuration annotation on the main class of the service provider on the main method

@SpringBootApplication
@EnableDubbo
public class GmallUserWebApplication {

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

}

Then start it

Successful registration can be found
Spring boot integrates Dubbo and uses zookeeper as the registry

Call query interface test

The data is found successfully
Spring boot integrates Dubbo and uses zookeeper as the registry

Distributed can be based on this way to achieve the call between different modules. The consumer side and provider side of each implementation service are separated.

Dubbo and zookeeper distributed architecture
Spring boot integrates Dubbo and uses zookeeper as the registry

Implementation process:

  • Provider: the service provider, responsible for providing services to the outside world. When the provider starts, it needs to register with the registry to provide services

Our services

  • Consumer: the consumer of the service. When starting, the consumer needs to subscribe to the service he needs from registry
  • Registry: Registration Center, giving and receiving registration and subscription, will inform subscribers asynchronously and provide service list to consumers

When consumers need to execute remote procedure calls, they will get the service address list (based on the load balancing algorithm) from registry for calling,
If the call fails, a new provider is selected and called again

  • Monitor: monitoring center, which counts the number and time of service calls. Service consumers and providers will accumulate the number and time of service calls in memory

Call time, send statistics to the monitoring center every minute

Code address branch gmall0105