Simple use of dubbo (3)

Here, we will optimize the previous two examples (production and consumption), create a new parent maven module, and make production and consumption depend on the parent module. The first purpose is to remove duplicate code, such as the DemoProviderService interface; the second purpose is to let the pom of the parent module manage the version uniformly .

If you are using eclipse, as long as you can rely on projects in the same workspace, the author is using idea, and you need to create a new module. The steps are as follows:

1. Create a new maven project as the parent module

2, as shown in the figure, according to the prompt, add a new module


Note: Baidu has directly added other projects as modules, but the author's experiments have two errors, one: the pom file of the submodule cannot be scanned; two: the entire submodule in the maven project interface is gray and unavailable. If you geek have a solution, please let me know.

3. Create a new java and resource, and copy the previous files in, as shown in the figure:


4. Add project dependencies to the pom files of the production module and the consumption module, and then delete the interfaces of the respective modules. The author's parent dependencies are as follows:


5. Add the interface that needs to be consumed to the parent module, so that the purpose is completed.


6. Extract the pom content of the submodule, define and version management in the parent module pom, 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>gccDubbo</groupId>
  <artifactId>gccDubbo</artifactId>
  <version>1.0-SNAPSHOT</version>
  <modules>
    <module>gccDubboProvider</module>
    <module>gccDubboConsumer</module>
  </modules>
  <packaging>pom</packaging>
  <name>gccDubbo Maven Webapp</name>

  <!-- Dependency version management-->
  <properties>
    <dubbo-demo-api.version>0.0.1-SNAPSHOT</dubbo-demo-api.version>
    <dubbo.version>2.6.0</dubbo.version>
    <zkclient.version>0.10</zkclient.version>
    <curator-framework.version>4.0.1</curator-framework.version>
    <fastjson.version>1.2.46</fastjson.version>
    <log4j.version>1.2.17</log4j.version>
    <slf4j-api.version>1.7.25</slf4j-api.version>
    <commons-lang3.version>3.4</commons-lang3.version>
    <netty-all.version>4.0.35.Final</netty-all.version>
  </properties>

  <!-- Dependency management only defines -->
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>${dubbo.version}</version>
      </dependency>
      <dependency>
        <groupId>com.101tec</groupId>
        <artifactId>zkclient</artifactId>
        <version>${zkclient.version}</version>
      </dependency>
      <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-framework</artifactId>
        <version>${curator-framework.version}</version>
      </dependency>
      <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>${fastjson.version}</version>
      </dependency>
      <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>${log4j.version}</version>
      </dependency>
      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j-api.version}</version>
      </dependency>
      <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>${commons-lang3.version}</version>
      </dependency>
      <dependency>
        <groupId>io.netty</groupId>
        <artifactId>netty-all</artifactId>
        <version>${netty-all.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

</project>

7. Remove the versions of the two submodules and define parent dependencies, for example:

<?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>gccDubbo</groupId>
        <artifactId>gccDubbo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>gccDubboProvider</artifactId>
    <name>gccDubboProvider Maven Webapp</name>

    <dependencies>
        <dependency>
            <groupId>gccDubbo</groupId>
            <artifactId>gccDubbo</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
        </dependency>
    </dependencies>
</project>

In this way, the second purpose is achieved, the version is managed in the same pom of the parent module.

After this management, if a new service is added, it is only necessary to add an interface in the parent module, complete the implementation in the production module, and call it in the consumption module, and there will be no code redundancy.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324649778&siteId=291194637