springboot integrates apache dubbo

As dubbo became an apache Foundation incubator on February 15, 2018, it is no longer a project maintained by Ali. Then there seems to be a fault. Most of the articles on the Internet are about dubbo development tutorials in Ali stage, which leads to a long search for apache dubbo development tutorials. So I want to sort out the simple integration of the latest dubbo and spring boot here.

First we need to download and run zookeeper:
http://mirror.bit.edu.cn/apac...
To download the latest version, what you need to know is that the zookeeper version number in the project is best consistent with the downloaded version.
We just need to download and decompress the zoo_sample.cfg copy in conf, rename it zoo.cfg, and run zkServer.cmd under bin.

The new version of dubbo admin has not been built yet, so the old version is used, and the research understanding will be updated.
There are a lot of old dubbo admin built on the Internet, so I won't dwell on it any more.

Here I use IDEA for development and construction projects.

  • First we need to build an empty project:

    Fill in the project name and click Finish.

  • Since it is a newly built empty project, we need to build new modules:

    We use the spring boot module here. First we build a new producer module.


    Create a new consumer module in the above order.

  • Because dubbo recommend using the interface layer as a separate module. Then both producers and consumers rely on this module for operation.
    So we need to build a new maven module.


    After the completion of the three modules:

  • We first develop the common module, that is, the common module.
    Create a new entity class and a service interface:

    public class User implements Serializable {
    
        private String name;
    
        private Integer age;
    
        public User() {
        }
    
        public User(String name, Integer age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    }
    public interface GetUserService {
    
        List<User> getUserList(String name);
    
    }

    Because you want to use the common module as a template, both the producer and consumer modules will inherit it, so you can integrate all the packages you need in its pom file.

    <dependencies>
            <!-- apache Of springboot dubbo rely on -->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>2.7.3</version>
            </dependency>
    
            <!-- zookeeper -->
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>3.4.14</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-log4j12</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>log4j</groupId>
                        <artifactId>log4j</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>io.netty</groupId>
                        <artifactId>netty</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    
            <!-- Zookeeper Client -->
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-recipes</artifactId>
                <version>4.2.0</version>
            </dependency>
        </dependencies>

    common module development is over.

  • Now to develop the producer module, we only need to inherit the common module and then implement the service interface.


    The Service here is apache.dubbo's package, which needs attention.

    @Service
    public class GetUserServiceImpl implements GetUserService {
    
        @Override
        public List<User> getUserList(String name) {
            //There is no connection to the database, so you can return several pieces of data.
            System.out.println(name);
            List<User> list = new ArrayList<>();
            list.add(new User("Xiaoming",20));
            list.add(new User("Cockroach",21));
            list.add(new User("Xiaohong",22));
            return list;
        }
    }
  • Rename application.properties to application.yml and configure it

    server:
      port: 8082 #Port number
    
    dubbo:
      application:
        name: provider #Name of current service/application
    #  scan:
    #    base-packages: com.zhouxiaoxi.provider.service.impl #Open package scan instead of @EnableDubbo annotation
    
      monitor:
        protocol: registry #Connect to the monitoring center
    
      protocol:
        name: dubbo #communication protocol
        port: 20880 #Interface
    
      registry:
        address: 127.0.0.1:2181 #Address of the Registry
        protocol: zookeeper #Registry Agreement

    Here we also add the @EnableDubbo annotation on the startup class, because it's relatively simple:

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

    Then we need to inherit the common module in the pom file:

    <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>
        </dependency>
    
        <!-- common Modular -->
        <dependency>
            <groupId>com.zhouxiaoxi</groupId>
            <artifactId>common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

    Now you can start the producer, and then look at the dubbo admin interface and you will find a provider:

  • Next, we continue to develop consumers:
    Here we just need to write a controller to call the interface.

    @RestController
    public class GetUserController {
    
        @Reference
        GetUserService getUserService;
    
        @RequestMapping("getUserList")
        public List<User> getUserList(@RequestParam("name") String name){
            return getUserService.getUserList(name);
        }
    
    }
  • Rename application.properties to application.yml and configure it

    server:
      port: 8081 #Port number
    
    dubbo:
      application:
        name: consumer #Name of current service/application
      #  scan:
      #    base-packages: com.zhouxiaoxi.provider.service.impl #Open package scan instead of @EnableDubbo annotation
    
      monitor:
        protocol: registry #Connect to the monitoring center
    
      protocol:
        name: dubbo #communication protocol
        port: 20880 #Interface
    
      registry:
        address: 127.0.0.1:2181 #Address of the Registry
        protocol: zookeeper #Registry Agreement

    Here we also add the @EnableDubbo annotation on the startup class, because it's relatively simple:

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

    Then we need to inherit the common module in the pom file and add web dependencies:

    <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>
        </dependency>
    
        <!-- web rely on -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.9.RELEASE</version>
        </dependency>
    
        <!-- common Modular -->
        <dependency>
            <groupId>com.zhouxiaoxi</groupId>
            <artifactId>common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

    Now you can start the consumer, then look at the dubbo admin interface and you will find a consumer:

    Visit http://localhost 8081/getUserList?name=zhouxiaoxi can see the corresponding return value.

    So far our integration of spring boot and apache dubbo is over.

    The project has been uploaded to github, you can download and run it:
    https://github.com/zhouzhaodo...

Keywords: Java Dubbo Spring Apache Zookeeper

Added by stephenlk on Thu, 10 Oct 2019 04:06:34 +0300