Build distributed project practice based on SpringBoot+Dubbo+Zookeeper+Maven+IDEA

 

1, Explanation of terms (from Baidu Encyclopedia):

Spring Boot is a new framework provided by the Pivotal team, which is designed to simplify the initial construction and development process of new spring applications. The framework uses a specific way to configure, so that developers no longer need to define a templated configuration.

Dubbo (pronunciation [ˈ d ʌ b ə]) is a high-performance excellent service framework open-source by Alibaba company, which enables applications to realize the output and input functions of services through high-performance RPC, and can integrate seamlessly with Spring framework.

ZooKeeper is a distributed, open-source distributed application coordination service. It is an open-source implementation of Chubby of Google and an important component of Hadoop and Hbase. It is a software that provides consistency services for distributed applications. Its functions include configuration maintenance, domain name service, distributed synchronization, group service, etc.

Maven project object model (POM), a project management tool software that can manage project construction, reporting and documentation through a short description of information.

IDEA, the full name of IntelliJ IDEA, is an integrated environment for Java programming language development. IntelliJ is recognized as the best java development tool in the industry, especially in intelligent code assistant, automatic code prompt, refactoring, Java EE support, various versions of tools (git, svn, etc.), JUnit, CVS integration, code analysis, innovative GUI design and other functions can be said to be extraordinary. IDEA is the product of JetBrains, a company headquartered in Prague, the capital of the Czech Republic, whose developers are mainly strict Eastern European programmers. Its flagship version also supports HTML, CSS, PHP, MySQL, Python, etc. The free version only supports a few languages, such as Java.

2, Project construction steps:

1. Install the local Zookeeper Registration Center at: https://www.apache.org/dyn/closer.cgi/zookeeper/ The image download speed is fast, as shown in the following figure:

After downloading, unzip it, then open the configuration and copy a copy of zoo_sample.cfg , modified to zoo.cfg , as shown below:

Open zoo_.cfg, modify the data saving path, as shown in the figure below, change it to a local available path, and create a corresponding folder directory without it:

Enter the bin command under the terminal, and then execute the command:/ zkServer.sh Start, as shown below:

At this point, Zookeeper starts successfully.

2. Open the IDEA development tool, create a parent project of spring boot based on maven, and then create three sub module s based on the parent project. The final project structure is as follows:

1) Open parent project pom.xml File, add the three sub modules just created, as shown in the following figure:

<?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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.qunhongtech</groupId>
    <artifactId>springboot-dubbo-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-dubbo-demo</name>
    <description>Demo project for Spring Boot</description>
    <packaging>pom</packaging>


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

    <modules>
        <module>dubbo-api</module>
        <module>dubbo-customer</module>
        <module>dubbo-provider</module>
    </modules>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>

Note: < packaging > POM < / packaging > needs to be added manually.

2) A new service interface class HelloService is added in the Dubbo API module. The code is as follows:

package com.qunhongtech.api.service;

public interface HelloService {

    String sayHello(String name);
}

Dubbo API pom.xml The code is as follows:

<?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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.qunhongtech</groupId>
    <artifactId>dubbo-api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo-api</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>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>

The structure catalog is as follows:

3) The implementation interface class HelloServiceImpl is added in Dubbo provider module. The code is as follows:

package com.qunhongtech.provider.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.qunhongtech.api.service.HelloService;


@Service //Publishing services
public class HelloServiceImpl implements HelloService {

    @Override
    public String sayHello(String name) {
        return "Hello, " + name;
    }
}

dubbo annotation is added to the DubboProviderApplication startup class in dubbo provider. The code is as follows:

package com.qunhongtech.provider;

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


@EnableDubbo
@SpringBootApplication
public class DubboProviderApplication {

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

}

Dubbo provider pom.xml The code is as follows:

<?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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

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

    <properties>
        <java.version>1.8</java.version>
        <zookeeper.version>3.4.13</zookeeper.version>
        <dubbo.version>0.2.0</dubbo.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>com.qunhongtech</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>


        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>${dubbo.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>${zookeeper.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>

Dubbo provider application.yml The configuration contents are as follows:

server:
  port: 8091

dubbo:
  application:
    name: dubbo-provider
  protocol:
    name: dubbo
    port: 20880
  registry:
    address: zookeeper://127.0.0.1:2181

The structure catalog is as follows:

be careful:

@Service is the annotation of dubbo publishing service;

Log4j and slf4j-log4j12 need to be excluded, otherwise they will conflict with zookeeper;

 

4) A new control class HelloController is added in the Dubbo customer module, which is easy to test. The code is as follows:

package com.qunhongtech.customer;


import com.alibaba.dubbo.config.annotation.Reference;
import com.qunhongtech.api.service.HelloService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Reference //Reference service
    private HelloService helloService;

    @GetMapping("/sayHello")
    private String sayHello(@RequestParam String name){

        System.out.println("call sayHello succeed..." + " name:" + name);

        return helloService.sayHello(name);
    }

}
 

Dubbo customer pom.xml The code is as follows:

<?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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.qunhongtech</groupId>
    <artifactId>dubbo-customer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo-customer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <zookeeper.version>3.4.13</zookeeper.version>
        <dubbo.version>0.2.0</dubbo.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>com.qunhongtech</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>${dubbo.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>${zookeeper.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>
 

Dubbo customer application.yml The configuration contents are as follows:

server:
  port: 8090

dubbo:
  application:
    name: dubbo-customer
  registry:
    address: zookeeper://127.0.0.1:2181

The structure catalog is as follows:

be careful:

@Reference is the comment of dubbo reference service;

So far, the framework of distributed project based on dubbo has been built.

3, Project consumption service function test:

1) First, run the startup service provider Dubbo provider sub module, as shown in the following figure:

2) Then start the service consumer Dubbo customer sub module, as shown below:

3) Test URL path: http://localhost:8090/sayHello?name = Qunhong Technology , the test results are as follows:

 

 

Conclusion: through step-by-step practice, we have successfully built a dubbo based distributed project framework. As for the principles involved, please refer to relevant materials or videos for further understanding.

 

Tags: Dubbo Spring Maven Zookeeper

Posted on Thu, 11 Jun 2020 00:02:57 -0400 by cpjok