转载

搭建基于springboot的dubbo项目踩坑记

dubbo作为最常用的分布式框架之一,本人觉得很有必要自己搭建一个简单的框架。

项目最终结构

搭建基于springboot的dubbo项目踩坑记

最外层为名为dubbo_demo的maven模块,内有基于springboot的生产者与消费者模块。

创建maven项目

使用idea新建maven项目,填入基本信息。

搭建基于springboot的dubbo项目踩坑记

只保留pom.xml文件,内容如下:

<?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.lamarsan</groupId>
    <artifactId>dubbo_demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--子模块声明-->
    <modules>
        <module>
            provider
        </module>
        <module>
            consumer
        </module>
    </modules>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <dubbo.version>2.8.5-SNAPSHOT</dubbo.version>
        <curator-framework.version>1.3.3</curator-framework.version>
        <zookeeper.version>3.4.8</zookeeper.version>
        <zkclient.version>0.1</zkclient.version>
        <mybatis-starter.version>1.3.2</mybatis-starter.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.boot</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>0.2.0</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>2.0.1.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis-starter.version}</version>
            </dependency>
            <!-- zookeeper start -->
            <dependency>
                <groupId>com.netflix.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>${curator-framework.version}</version>
            </dependency>

            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>${zookeeper.version}</version>
                <exclusions>
                    <exclusion>
                        <groupId>commons-logging</groupId>
                        <artifactId>commons-logging</artifactId>
                    </exclusion>
                    <exclusion>
                        <artifactId>log4j-over-slf4j</artifactId>
                        <groupId>org.slf4j</groupId>
                    </exclusion>
                    <exclusion>
                        <artifactId>slf4j-log4j12</artifactId>
                        <groupId>org.slf4j</groupId>
                    </exclusion>
                    <exclusion>
                        <artifactId>log4j</artifactId>
                        <groupId>log4j</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>com.github.sgroschupf</groupId>
                <artifactId>zkclient</artifactId>
                <version>${zkclient.version}</version>
            </dependency>
            <!-- zookeeper end -->

            <dependency>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.5</version>
            </dependency>
            <dependency>
                <groupId>commons-lang</groupId>
                <artifactId>commons-lang</artifactId>
                <version>2.6</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <resources>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/application*.yml</include>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <includes>
                    <include>**/application*.yml</include>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>
复制代码

下面进行子模块的创建 :

搭建基于springboot的dubbo项目踩坑记

右击项目名,新建Module,创建springboot项目。

provider

项目结构如下,包括与数据库连接的dao,实体类model,以及服务service:

搭建基于springboot的dubbo项目踩坑记

pom.xml

<?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>
    <!--父模块声明-->
    <parent>
        <groupId>com.lamarsan</groupId>
        <artifactId>dubbo_demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>com.lamarsan</groupId>
    <artifactId>provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>provider</name>
    <description>Demo project for Spring Boot</description>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.0.1.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.16</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>2.0.1.RELEASE</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
    </dependencies>

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

</project>
复制代码

service

dubbo提供了服务方的声明方式@Service,虽然和springboot的service声明方式一样,但是两者引用的是不同的包。dubbo的包名为com.alibaba.dubbo.config.annotation.Service,不要导错。

搭建基于springboot的dubbo项目踩坑记

属性配置

#当前服务/应用的名字
dubbo.application.id=provider
dubbo.application.name=provider
dubbo.application.qos-enable=false
dubbo.scan.basePackages=com.lamarsan.provider.service.impl
#通信规则(通信协议和接口)
dubbo.protocol.id=dubbo
dubbo.protocol.name=dubbo
dubbo.protocol.port=12346
#注册中心的协议和地址
dubbo.registry.protocol=zookeeper
dubbo.registry.address=xxxxx:2181
user.service.version=1.0.0
spring.datasource.url=jdbc:mysql://localhost:3306/debate?serverTimezone=GMT%2B8&&useCursorFetch=true&&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

mybatis.mapper-locations = classpath*:mapper/*.xml
复制代码

启动项

package com.lamarsan.provider;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@EnableDubbo
@ComponentScan(basePackages = {"com.lamarsan.provider"})   //dubbo扫描的包
@MapperScan("com.lamarsan.provider.dao")
public class ProviderApplication {

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

}

复制代码

consumer

项目结构如下,包括一个controller;

搭建基于springboot的dubbo项目踩坑记

pom.xml

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

    <groupId>com.lamarsan</groupId>
    <artifactId>consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>consumer</name>
    <description>Demo project for Spring Boot</description>

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

    <dependencies>
        <dependency>
            <groupId>com.lamarsan</groupId>
            <artifactId>provider</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.0.1.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <!--引入web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>2.0.1.RELEASE</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

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

</project>

复制代码

controller

dubbo提供了@Reference注解,该注解可以替换@Autowired注解,用于引入远程微服务,包名为 com.alibaba.dubbo.config.annotation.Reference

搭建基于springboot的dubbo项目踩坑记

属性配置

# ApplicationConfig Bean
dubbo.application.id = consumer
dubbo.application.name = consumer
dubbo.application.qos-enable = false
dubbo.consumer.timeout=60000
dubbo.consumer.retries=0

server.port = 8098
# zookeeper
## ProtocolConfig Bean
dubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.registry.protocol = zookeeper
dubbo.registry.address = xxxxx:2181
复制代码

启动项

package com.lamarsan.consumer;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@EnableDubbo
@ComponentScan(basePackages = {"com.lamarsan.consumer"})
public class ConsumerApplication {

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

复制代码

调用结果

先启动provider模块,再启动consumer模块,实现从数据库读取一个用户信息:

搭建基于springboot的dubbo项目踩坑记

以上就是全部的搭建流程。接下来是搭建过程中碰到的一些问题的解答。

踩坑环节

1)数据源没有自动配置

Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and no embedded datasource could be auto-configured.
提示数据源没有得到正确的配置。

解决方案1

在消费者启动向中加入 exclude = {DataSourceAutoConfiguration.class} ,如图所示:

搭建基于springboot的dubbo项目踩坑记

解决方案2

将消费者模块的pom.xml中的有关mybatis与数据库的依赖删去即可。由于一开始并没有注意这一细节,所以直接是从生产者的pom.xml中复制黏贴的,故报了这个错。

2)服务方正确启动,但是无法调用服务中的方法

No provider available from registry 127.0.0.1:2181 for service com.focussend.email.service.EdmTaskS
提示没有可用的服务。 由于之前在@service(version="")中加入了版本信息,但是消费者端并没有定义,故出现了版本不一致的问题,所以无法正确启动。

解决方案1

去掉@service括号里的内容。

解决方案2

在消费者端定义dubbo.xml中的版本信息。

3)数据库绑定异常

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
出现的错误网上是千篇一律的提示mapper.xml与dao层接口类的名字不一致以及namespace问题,也有提示没有生成.xml文件的问题,但都不适用于自己的情况。到最后才发现是配置项的声明问题。

解决方案

在.properties文件中加入: mybatis.mapper-locations = classpath*:mapper/*.xml 一行,实现对map的扫描。

总结

之前一直使用别人已经搭建好的dubbo框架,自己搭建一套才发现坑真的不是一般的多,经常会踩一些别人认为理所当然会配置正确的坑,别看只有这么三个异常,从开始搭建到搭建结束花了将近四个小时,疯狂的查找问题。但是这些坑都是有意义的,确实是让我这个记忆力不好的人深深地记住了。

最后给出此项目的的gihub地址: github.com/lamarsan/du…

搭建基于springboot的dubbo项目踩坑记
原文  https://juejin.im/post/5d29dbdd6fb9a07ef90cc7d0
正文到此结束
Loading...