一、本文要点

我们已经把SpringBoot整合mybatis+Hikari+es+redis+kafka了,本文将介绍SpringBoot如何整合dubbo。

  • dubbo注解版使用
  • dubbo provider、dubbo consumer
  • maven多模块配置、maven批量修改项目版本号
  • springboot整合dubbo、zookeeper、zookeeper集群配置
  • springboot + mybatis + Hikari + elasticsearch + redis + dubbo

二、开发环境

  • jdk 1.8
  • maven 3.6.2
  • springboot 2.4.3
  • dubbo-starter 2.0
  • dubbo 2.6.0
  • zookeeper 3.4.13
  • idea 2020

三、修改项目结构

1、由于我们需要把会员服务做成dubbo服务,需要提供API给消费者,所以需要抽取出API供消费者使用。

2、创建member-service目录,创建parent的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.mmc.lesson</groupId>
    <artifactId>member-service</artifactId>
    <packaging>pom</packaging>
    <version>1.0.0-SNAPSHOT</version>

    <modules>
        <module>api</module>
        <module>member</module>
    </modules>

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

    <!-- 统一多模块的依赖版本 -->
    <dependencyManagement>

        <dependencies>

        </dependencies>

    </dependencyManagement>

    <!-- 统一版本号 -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>

    </properties>

    <dependencies>

    </dependencies>

</project>

2、在member-service目录下创建api模块,修改api项目的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">
    <parent>
        <groupId>com.mmc.lesson</groupId>
        <artifactId>member-service</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>member-api</artifactId>

    <dependencies>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

3、在api模块中,定义一个会员接口MemberApi.java和必须的实体类,MemberInfo.java跟db一致。

public interface MemberApi {

    /**
     * 获取会员.
     */
    Result get(MemberInfo member);

    /**
     * 保存会员.
     */
    Result save(MemberInfo member);
}
@Data
public class Result {

    private int code;

    private String message;

    private Object data;

    public static Result ok(Object data) {

        Result r = new Result();
        r.setCode(0);
        r.setMessage("SUCCESS");
        r.setData(data);
        return r;
    }

    public static Result fail(String message) {
        Result r = new Result();
        r.setCode(-1);
        r.setMessage(message);
        r.setData(null);
        return r;
    }
}

4、将原来member项目,移动到member-service目录,并修改原member模块的pom.xml。注意:省略了系列文章配置的依赖,请查看源码。主要改动点,修改了parent标签,增加了dubbo、zookeeper依赖。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.mmc.lesson</groupId>
        <artifactId>member-service</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <artifactId>member</artifactId>
    <name>member</name>
    <description>Member Service</description>
    <properties>
        <java.version>1.8</java.version>
        <version.mybatis>1.3.0</version.mybatis>
        <version.mysql>5.1.48</version.mysql>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- 此处省略了好多系列文章的依赖 -->
        
        <!-- dubbo -->
        <dependency>
            <groupId>com.mmc.lesson</groupId>
            <artifactId>member-api</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba.spring.boot/dubbo-spring-boot-starter -->
        <dependency>
            <groupId>com.alibaba.spring.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.9</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </dependency>

    </dependencies>

</project>

5、修改MemberService.java,使其实现MemberApi.java接口,并增加com.alibaba.dubbo.config.annotation.Service注解,暴露dubbo服务。改动点:实现MemberApi接口,其中的converXXX方法是idea插件生成的,同学们可以换成BeanUtil。

@Slf4j
@Service
@com.alibaba.dubbo.config.annotation.Service(interfaceClass = MemberApi.class)
public class MemberService implements MemberApi {

    // 省略了好多系列文章的代码

    @Override
    public Result get(MemberInfo member) {

        TblMemberInfo info = convertMember(member);
        TblMemberInfo data = get(info);
        if (null == data) {
            return Result.fail("会员不存在");
        }
        return Result.ok(convertMember(data));
    }

    @Override
    public Result save(MemberInfo member) {
        TblMemberInfo info = convertMember(member);
        TblMemberInfo data;
        try {
            data = save(info);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.ok(convertMember(data));
    }

    // 省略了好多系列文章的代码
}

四、修改配置文件

修改application-dev.properties文件,同理,后面发布到测试、正式环境的话,修改对应的配置文件。这里增加dubbo的配置。

#################### DUBBO ####################
spring.application.name=member-service
spring.dubbo.server=true
spring.dubbo.registry.address=zookeeper://9.134.77.133:2181
# 集群版配置如下
#spring.dubbo.registry.address=zookeeper://9.134.77.133:2181?backup=9.134.77.133:2182,9.134.77.133:2183

五、修改启动类

1、修改MemberApplication,@EnableDubboConfiguration启用dubbo配置。

@EnableDubboConfiguration
@MapperScan(basePackages = "com.mmc.lesson.member.mapper")
@SpringBootApplication
public class MemberApplication {

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

六、运行一下

1、编写单元测试。

@Slf4j
@ActiveProfiles("dev")
@ExtendWith(SpringExtension.class)
@SpringBootTest
@Transactional
public class DubboMemberServiceTest {

    @Reference
    private MemberApi memberApi;

    @Test
    void testGet() {

        MemberInfo member = new MemberInfo();
        member.setUid(888L);

        Result ret = memberApi.get(member);
        log.info("--------------------------------------------------------");
        log.info("ret: {}", GsonUtil.toJson(ret));
    }

}

2、效果,可以看到dubbo方法已经被正常调用了。

[2021-03-26 17:40:08.405] [main] [DEBUG] [org.mybatis.spring.SqlSessionUtils:?] - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@71df5f30]
[2021-03-26 17:40:08.406] [main] [INFO] [c.mmc.lesson.member.service.DubboMemberServiceTest:?] - --------------------------------------------------------
[2021-03-26 17:40:08.410] [main] [INFO] [c.mmc.lesson.member.service.DubboMemberServiceTest:?] - ret: {"code":-1,"message":"会员不存在"}
[2021-03-26 17:40:08.412] [main] [DEBUG] [o.s.t.c.c.DefaultCacheAwareContextLoaderDelegate:?] - Retrieved ApplicationContext [1390301622] from cache with key [[WebMergedContextConfiguration@5167f57d testClass = DubboMemberServiceTest, locations = '{}', classes = '{class com.mmc.lesson.member

3、后面MemberApi接口有变动,增加字段或者增加方法啥的,可以执行以下命令,提升接口版本。

# 在父pom.xml目录下执行
 mvn versions:set -DnewVersion=1.0.1 -DgenerateBackupPoms=false