I. Main points of this paper

We’ve already integrated SpringBoot with Mybatis +Hikari+es+ Redis + Kafka. In this article, we’ll explain how SpringBoot can integrate dubbo. A complete catalog of articles in the series

  • Dubbo annotated edition for use

  • Dubbo Provider and Dubbo Consumer

  • Maven multi-module configuration, Maven batch change project version number

  • Springboot integrates the configuration of the Dubbo, ZooKeeper, and ZooKeeper clusters

  • springboot + mybatis + Hikari + elasticsearch + redis + dubbo

Second, development environment

  • JDK 1.8
  • Maven 3.6.2
  • Springboot 2.4.3
  • Dubbo – 2.0 the starter
  • Dubbo server
  • Zookeeper 3.4.13
  • idea 2020

3. Modify the project structure

1. Since we need to make the membership service into dubbo service and provide API to consumers, we need to extract API for consumers to use.

2, create member-service directory, create parent pom.xml.


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

    <! -- Unified multi-module dependency version -->
    <dependencyManagement>

        <dependencies>

        </dependencies>

    </dependencyManagement>

    <! -- Unified version number -->
    <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>
Copy the code

Create API module in member-service directory, modify API project pom.xml.


      
<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 - the 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>
Copy the code

3, In the API module, define a member interface memberapi. Java and the necessary entity class, memberInfo. Java is consistent with db.

public interface MemberApi {

    /** ** Get membership. */
    Result get(MemberInfo member);

    /** * save membership. */
    Result save(MemberInfo member);
}
Copy the code
@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);
        returnr; }}Copy the code

4, Move the original member project to the member-service directory and modify the original member module pom.xml. Note: The dependencies for the configuration of the series of articles are omitted. See the source code. Major changes: Modified the parent tag, added dubbo and ZooKeeper dependencies.


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

        <! -- Here omitted many series of article dependence -->
        
        <! -- 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>

Copy the code

5, modify MemberService. Java, make its implementation MemberApi. Java interfaces, and increase the com. Alibaba. Dubbo. Config. The annotation. The Service annotations, exposed dubbo Service. Changes: MemberApi interface, where converXXX method is generated by idea plug-in, students can be replaced with BeanUtil.

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

    // Omitted several series of articles of code

    @Override
    public Result get(MemberInfo member) {

        TblMemberInfo info = convertMember(member);
        TblMemberInfo data = get(info);
        if (null == data) {
            return Result.fail("Membership does not exist.");
        }
        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));
    }

    // Omitted several series of articles of code
}
Copy the code

Modify the configuration file

Modify the application-dev.properties file, and also modify the corresponding configuration file for later release to test or formal environments. Add the configuration of Dubbo here.

#################### DUBBO ####################
spring.application.name=member-service
spring.dubbo.server=true
spring.dubbo.registry.address=Zookeeper: / / 9.134.77.133:2181
The cluster edition configuration is as follows
# spring. Dubbo. Registry. Address = zookeeper: / / 9.134.77.133:2181? Backup = 9.134.77.133:2182,9.134. 77.133:2183

Copy the code

Modify the startup class

1. Modify MemberApplication @enabledubboconfiguration to EnableDubboConfiguration.

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

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

Six, run it

1. Write unit tests.

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

    @Reference
    private MemberApi memberApi;

    @Test
    void testGet(a) {

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

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

You can see that the dubbo method has been called normally.

[2021-03-26 17:40:08405.] [main] [DEBUG] [org.mybatis.spring.SqlSessionUtils:? ]  - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@71df5f30]
[2021-03-26 17:40:08406.] [main] [INFO] [c.mmc.lesson.member.service.DubboMemberServiceTest:? ] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -2021-03-26 17:40:08410.] [main] [INFO] [c.mmc.lesson.member.service.DubboMemberServiceTest:? ]  - ret: {"code": -1."message":"Membership does not exist."}
[2021-03-26 17:40:08412.] [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
Copy the code

3, after the MemberApi interface changes, add fields or add methods, you can run the following command to upgrade the interface version.

 #Execute in the parent pom.xml directoryMVN versions: the set - DnewVersion = 1.0.1 - DgenerateBackupPoms = falseCopy the code

Seven, summary

This is just a brief introduction to how to integrate Dubbo. Please check out the following article for more details on how to use dubbo. Docker builds a development environment and installs Apollo

Add me to exchange learning!