2018-08-01 Spring-boot, dubbo, multiple-modules, zoo-keeper(3)

Since the service-interfaces are registered onto zoo-keeper, we can now try to consume them.

Step 6 Prepare the consumer-project

  1. Create the folder-structure for the consumer-project.
e:\Downloads\documents\STS_projects\justProject>cd consumer

e:\Downloads\documents\STS_projects\justProject\consumer>mkdir src\main\java\mconsumer

e:\Downloads\documents\STS_projects\justProject\consumer>tree . /f
卷 Tools 的文件夹 PATH 列表
卷序列号为 E885-5830
E:\DOWNLOADS\DOCUMENTS\STS_PROJECTS\JUSTPROJECT\CONSUMER
└─src
    └─main
        └─java
            └─mconsumer
  1. Create pom.xml for consumer-project, it also depends on the library-project.

Content of consumer\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.justproject.example</groupId>
    <artifactId>consumer</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>com.justproject.example</groupId>
        <artifactId>parentproject</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>com.justproject.example</groupId>
            <artifactId>common_api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  1. Add the module definition in parent-project.

pom.xml of parent-project

<?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.justproject.example</groupId>
    <artifactId>parentproject</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.7.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

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

    <modules>
        <module>common_api</module>
        <module>consumer</module>
        <module>provider</module>
    </modules>
</project>
  1. Add an entry-point for the consumer-project.

Content of consumer\src\main\java\mconsumer\Consumer.java

package mconsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Consumer {
    public static void main(String[] args) {
        SpringApplication.run(Consumer.class, args);
    }
}
  1. Try to package this empty project.

e:\Downloads\documents\STS_projects\justProject\consumer>tree . /f
卷 Tools 的文件夹 PATH 列表
卷序列号为 E885-5830
E:\DOWNLOADS\DOCUMENTS\STS_PROJECTS\JUSTPROJECT\CONSUMER
│  .classpath
│  .project
│  pom.xml
│
├─.settings
│      org.eclipse.core.resources.prefs
│      org.eclipse.jdt.core.prefs
│      org.eclipse.wst.common.project.facet.core.xml
│
└─src
    └─main
        └─java
            └─mconsumer
                    Consumer.java


e:\Downloads\documents\STS_projects\justProject\consumer>mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< com.justproject.example:consumer >------------------
[INFO] Building consumer 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ consumer ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ consumer ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory e:\Downloads\documents\STS_projects\justProject\consumer\src\main\resources
[INFO] skip non existing resourceDirectory e:\Downloads\documents\STS_projects\justProject\consumer\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ consumer ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to e:\Downloads\documents\STS_projects\justProject\consumer\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ consumer ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory e:\Downloads\documents\STS_projects\justProject\consumer\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ consumer ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ consumer ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ consumer ---
[INFO] Building jar: e:\Downloads\documents\STS_projects\justProject\consumer\target\consumer-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:1.5.7.RELEASE:repackage (default) @ consumer ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.170 s
[INFO] Finished at: 2018-08-01T14:07:58+08:00
[INFO] ------------------------------------------------------------------------

e:\Downloads\documents\STS_projects\justProject\consumer>

Step 7 Add a controller for the consumer-project

  1. To use annotation @RestController, add dependency in pom.xml of the consumer-project.

Content of consumer\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.justproject.example</groupId>
    <artifactId>consumer</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>com.justproject.example</groupId>
        <artifactId>parentproject</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>com.justproject.example</groupId>
            <artifactId>common_api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  1. Write a controller to handle the request.

Content of consumer\src\main\java\mconsumer\SampleController.java

package mconsumer;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SampleController {
    @RequestMapping("/test")
    public String test() {
        return "hello from test.";
    }
}
  1. Create the resources folder for the consumer-project.
e:\Downloads\documents\STS_projects\justProject\consumer>mkdir src\main\resources

e:\Downloads\documents\STS_projects\justProject\consumer>mvn clean
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< com.justproject.example:consumer >------------------
[INFO] Building consumer 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ consumer ---
[INFO] Deleting e:\Downloads\documents\STS_projects\justProject\consumer\target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.098 s
[INFO] Finished at: 2018-08-01T14:18:29+08:00
[INFO] ------------------------------------------------------------------------

e:\Downloads\documents\STS_projects\justProject\consumer>tree .
卷 Tools 的文件夹 PATH 列表
卷序列号为 E885-5830
E:\DOWNLOADS\DOCUMENTS\STS_PROJECTS\JUSTPROJECT\CONSUMER
├─.settings
└─src
    └─main
        ├─java
        │  └─mconsumer
        └─resources
  1. Create the application.properties file to save the configuration for consumer-project

Content of consumer\src\main\resources\application.properties

## Use a different port
server.port=6666
  1. Package and run the consumer-project to see.
e:\Downloads\documents\STS_projects\justProject\consumer>mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< com.justproject.example:consumer >------------------
[INFO] Building consumer 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ consumer ---
[INFO] Deleting e:\Downloads\documents\STS_projects\justProject\consumer\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ consumer ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ consumer ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to e:\Downloads\documents\STS_projects\justProject\consumer\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ consumer ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory e:\Downloads\documents\STS_projects\justProject\consumer\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ consumer ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ consumer ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ consumer ---
[INFO] Building jar: e:\Downloads\documents\STS_projects\justProject\consumer\target\consumer-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:1.5.7.RELEASE:repackage (default) @ consumer ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.545 s
[INFO] Finished at: 2018-08-01T14:23:42+08:00
[INFO] ------------------------------------------------------------------------

e:\Downloads\documents\STS_projects\justProject\consumer>java -jar target\consumer-1.0-SNAPSHOT.jar
  1. Access http://localhost:666/test or use curl to see that the consumer is working.
C:\Users\home>curl http://localhost:6666/test
hello from test.

Step 8 Change consumer-project to consume from zoo-keeper

  1. Add dependency to dubbo & zoo-keeper

Content of consumer\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.justproject.example</groupId>
    <artifactId>consumer</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>com.justproject.example</groupId>
        <artifactId>parentproject</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>com.justproject.example</groupId>
            <artifactId>common_api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- dubbo & zoo-keeper -->
        <dependency>
            <groupId>io.dubbo.springboot</groupId>
            <artifactId>spring-boot-starter-dubbo</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  1. Add a service-class, which refers to the service-interface.

Content of consumer\src\main\java\mconsumer\AnotherServiceConsumer.java

package mconsumer;

import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Reference;
import mapi.IAnotherService;

@Component
public class AnotherServiceConsumer {
    @Reference
    IAnotherService anotherService;
    
    public String printSomething() {
        return anotherService.sayHello();
    }
}
  1. Update the controller to use the service-class.

Content of consumer\src\main\java\mconsumer\SampleController.java

package mconsumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SampleController {
    
    @Autowired
    AnotherServiceConsumer consumer;
    
    @RequestMapping("/test")
    public String test() {
        return consumer.printSomething();
    }
}
  1. Edit application.properties, to use zoo-keeper.
    Here, we should define to scan the package of the service-implementation, not the package where we define the service-interface!!!

Content of consumer\src\main\resources\application.properties

## Use a different port
server.port=6666
## Configuration as a dubbo-consumer
spring.dubbo.application.name=provider
spring.dubbo.registry.address=zookeeper://xxx.xxx.xxx.xxx:2181
## package to scan for the service
spring.dubbo.scan=mconsumer
  1. Package the consumer-project & run it.
e:\Downloads\documents\STS_projects\justProject\consumer>mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< com.justproject.example:consumer >------------------
[INFO] Building consumer 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ consumer ---
[INFO] Deleting e:\Downloads\documents\STS_projects\justProject\consumer\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ consumer ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ consumer ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to e:\Downloads\documents\STS_projects\justProject\consumer\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ consumer ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory e:\Downloads\documents\STS_projects\justProject\consumer\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ consumer ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ consumer ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ consumer ---
[INFO] Building jar: e:\Downloads\documents\STS_projects\justProject\consumer\target\consumer-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:1.5.7.RELEASE:repackage (default) @ consumer ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.022 s
[INFO] Finished at: 2018-08-01T16:31:42+08:00
[INFO] ------------------------------------------------------------------------

e:\Downloads\documents\STS_projects\justProject\consumer>java -jar target\consumer-1.0-SNAPSHOT.jar
  1. Access http://localhost:666/test or use curl to see that the consumer is working.
C:\Users\home>curl http://localhost:6666/test
Hello from AnotherServiceImp

More

We can configure to ask Spring to scan multiple-packages, in case we defined different services in the practice. Example of application.properties as below.

## Configuration as a dubbo-provider
spring.dubbo.application.name=provider
spring.dubbo.registry.address=zookeeper://aaa.bbb.ccc.ddd:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
## package to scan for the service
spring.dubbo.scan=mservice,moreservice

References

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,734评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,931评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,133评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,532评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,585评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,462评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,262评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,153评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,587评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,792评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,919评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,635评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,237评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,855评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,983评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,048评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,864评论 2 354