Spring Boot integrates Dubbo and uses zookeeper as the registry

preface

The whole process of this article is under the operation of windows. The general operation is the same as that of Linux. Please Baidu for details and Google if you have conditions!

Knowledge points to understand

  • Dubbo and zookeeper environments and what they are
  • Spring Boot knowledge points, limited to package structure pom dependency inheritance, etc
  • Maven polymerization project
  • JDK and tomcat environment configuration, etc

Environment configuration first

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 micro service, please Baidu, if you don't know, don't look down.

Preferred need to understand and install Dubbo and zookeeper environments can be viewed Building dubbo and zookeeper environment under Windows

Build Maven aggregation project

Create a new parent project first


Enter project name and groupid and ArtifactId


Then right-click the parent package to create a new Module

As shown in the figure below, click finish.

Wait, click the parent pom after the creation
You can see the sub maven module

Project structure

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 versions -->
    <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 classes and define service interfaces

gmall-common-util

Store some common tools and methods as well as common dependencies of service and controller

service-util

Some dependencies needed to store service s, such as database driven redis, etc.

Then the GMALL user service dir folder holds dubbo service providers and consumers

New as shown above

GMALL user service service service provider
GMALL user web consumer

pom dependency inherited from the parent in each of the above submodules

 <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">
   
   <!-- Inherited from the parent maven rely on-- >
    <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 dependent 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 journal-->
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--And sb integration 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>User entity services</description>


    <dependencies>

        <!--currency 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</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>User service consumers dubbo External interface of consumer end</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>


        <!--currency 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 sub module service provider GMALL user service and consumer GMALL user web

Service provider

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

UmsMemberMapper

The dao query interface is provided to inherit the general mapper interface

Entity class depends on the GMALL API module
Mapper depends on the service util 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,
  `username` varchar(64) DEFAULT NULL COMMENT 'user name',
  `password` varchar(64) DEFAULT NULL COMMENT 'password',
  `nickname` varchar(64) DEFAULT NULL COMMENT 'Nickname?',
  `phone` varchar(64) DEFAULT NULL COMMENT 'phone number',
  `status` int(1) DEFAULT NULL COMMENT 'Account enabling status:0->Disabled; 1->Enable',
  `create_time` datetime DEFAULT NULL COMMENT 'Registration time',
  `icon` varchar(500) DEFAULT NULL COMMENT 'head portrait',
  `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 'Cities made',
  `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 remaining',
  `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='Membership form';

Service provider class

UmsMemberServiceImpl

It should be noted that in the past, we used the @ service annotation provided by Spring to inject bean s into the IOC container in the service implementation class of the singleton service
But now we use dubbo to implement microservices. We need to use dubbo's @ service annotation to register the service in zookeeper so that consumers can get it from the container for consumption.

`As the name implies, it provides services for others to call, which is equivalent to the implementation class of services in spring.
It is also very simple to use, that is, an annotation and a configuration
The provider adds the annotation @ Service to the implementation class, which is the same as spring's but not the same package. `

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 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
You need to enable Mapper scanning. If you use general Mapper, you must use @ MapperScan annotation of general Mapper to scan Mapper
And open transactions

@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:
      # Communication protocol name in dubbo
      name:  dubbo
    registry:
      # Address and end slogan of zookeeper Registration Center
      address: 127.0.0.1:2181
      # zookeeper registry protocol
      protocol: zookeeper
    # Service scan 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


You can see that the service has been registered in zookeeper

Open dubbo service monitoring page


You can see the service provider

Service consumers

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

Configure Yml first

Configure protocols such as registered address port

server:
  port: 10003



spring:
  dubbo:
    # Service name in dubbo
    application:
      name: user-web
    protocol:
      # Communication protocol name in dubbo
      name:  dubbo
    registry:
      # Address and end slogan of zookeeper Registration Center
      address: 127.0.0.1:2181
      # zookeeper registry protocol
      protocol: zookeeper
    # Service scan path of dubbo
    base-package: com.gmall
    consumer:
      # Timeout for accessing the provider service, 1000 milliseconds by default
      timeout: 30000
      # Is to check whether the server can access normally when the consumer is started. If true is selected, the service of the provider must be normal when starting the consumer. Otherwise, the interface cannot be injected
      check: false

UmsMemberController

New controller consumer service query data

In the past, in the monomer, we took the bean from the IOC container and then called the method to query, but in the dubbo distributed project, we used dubbo's service, that is, we took the service out of zookeeper with @Reference, injected it into controller, and then called 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 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

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

Successful registration can be found

Call query interface test

Data found successfully

Distributed can be based on this way to achieve calls between different modules. Each implementation service's consumer side and provider side are separated.

dubbo and zookeeper distributed architecture

Execution process:

  • Provider: the service provider, responsible for providing external services. When the provider starts, it needs to register with the Registry to provide its own services

Services for

  • Consumer: the consumer of the service. When the consumer starts, he / she needs to subscribe to the service he / she needs from the Registry
  • Registry: the registration center, which receives registration and subscription, will notify subscribers asynchronously and provide service list to consumers

When the consumer needs to perform a remote procedure call, it will get the service address list (based on the load balancing algorithm) from the Registry for calling,
If the call fails, the new provider will be re 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 calls in memory

Call time, send statistics to the monitoring center every minute

Code address branch gmall0105

Tags: Java Spring Dubbo Maven

Posted on Sat, 27 Jun 2020 02:39:33 -0400 by dhillarun