Springboot 整合 Dubbo/ZooKeeper 案例

主要演示了使用spring-boot-starter-dubbo以Springboot的方式使用dubbo

一、启动zookeeper
运行bin目录下的zkServer.cmd即可

二、创建server工程,作为提供者
说明:创建两个Springboot工程或者创建两个MavenModule没有区别。
1、创建springboot-dubbo-server工程。pom文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<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>
    <artifactId>springboot-dubbo-server</artifactId>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.7.RELEASE</version>
    </parent>

    <properties>
        <dubbo-spring-boot>1.0.0</dubbo-spring-boot>
    </properties>

    <dependencies>
        <!-- Spring Boot Dubbo 依赖 -->
        <dependency>
            <groupId>io.dubbo.springboot</groupId>
            <artifactId>spring-boot-starter-dubbo</artifactId>
            <version>${dubbo-spring-boot}</version>
        </dependency>

        <!-- Spring Boot Web 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Spring Boot Test 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
    </plugins>
</project>

其中:spring-boot-starter-dubbo是阿里的teaey个人开发的jar包,git地址:https://github.com/teaey/spring-boot-starter-dubbo

2、启动类ServerApplication.java

1
2
3
4
5
6
@SpringBootApplication
public class ServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class,args);
    }
}

3、domain
org.spring.springboot.domain 包下创建City类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
public class City implements Serializable {

    private static final long serialVersionUID = -1L;

    private Long id;

    private Long provinceId;

    private String cityName;

    private String description;

    public City() {
    }

    public City(Long id, Long provinceId, String cityName, String description) {
        this.id = id;
        this.provinceId = provinceId;
        this.cityName = cityName;
        this.description = description;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getProvinceId() {
        return provinceId;
    }

    public void setProvinceId(Long provinceId) {
        this.provinceId = provinceId;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

4、service接口
org.spring.springboot.service包下创建CityDubboService.java

1
2
3
4
5
6
7
public interface CityDubboService {
    /**
     * 根据城市名称,查询城市信息
     * @param cityName
     */

    City findCityByName(String cityName);
}

5、impl实现类
org.spring.springboot.impl包下创建CityDubboImpl.java
用@Service注解表明这是一个Dubbo服务

1
2
3
4
5
6
@Service(version = "1.0.0")
public class CityDubboServiceImpl implements CityDubboService {
    public City findCityByName(String cityName) {
        return new City(1L, 2L, "永州宁远", "是我的故乡");
    }
}

6、application.properties
在src/main/resources下创建application.properties,内容如下:

1
2
3
4
5
6
7
## Dubbo server
server.port=9010
spring.dubbo.application.name=provider
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
spring.dubbo.scan=org.spring.springboot

注意:spring.dubbo.scan配置的包名一定要跟实际创建的包名一致(填写最上层的包名),否则无法扫描到dubbo服务
以上配置指定了Zookeeper注册中心的地址,启动server工程后服务会注册到ZK,如图:

三、创建client工程,作为提供者
1、创建springboot-dubbo-client工程,pom文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<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>com.jianyu</groupId>
        <artifactId>chapter6</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <artifactId>springboot-dubbo-client</artifactId>

    <properties>
        <dubbo-spring-boot>1.0.0</dubbo-spring-boot>
    </properties>

    <dependencies>
        <!-- Spring Boot Dubbo 依赖 -->
        <dependency>
            <groupId>io.dubbo.springboot</groupId>
            <artifactId>spring-boot-starter-dubbo</artifactId>
            <version>${dubbo-spring-boot}</version>
        </dependency>

        <!-- Spring Boot Web 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Spring Boot Test 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
</project>

2、启动类ClientApplication.java

1
2
3
4
5
6
7
8
@SpringBootApplication
public class ClientApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(ClientApplication.class, args);
        CityDubboConsumerService cityService = run.getBean(CityDubboConsumerService.class);
        cityService.printCity();
    }
}

3、domain
org.spring.springboot.domain 包下创建City类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package org.spring.springboot.domain;

import java.io.Serializable;

/**
 * 城市实体类
 *
 */

public class City implements Serializable {

    private static final long serialVersionUID = -1L;

    /**
     * 城市编号
     */

    private Long id;

    /**
     * 省份编号
     */

    private Long provinceId;

    /**
     * 城市名称
     */

    private String cityName;

    /**
     * 描述
     */

    private String description;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getProvinceId() {
        return provinceId;
    }

    public void setProvinceId(Long provinceId) {
        this.provinceId = provinceId;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return "City{" +
                "id=" + id +
                ", provinceId=" + provinceId +
                ", cityName='" + cityName + '\'' +
                ", description='" + description + '\'' +
                '}';
    }
}

4、service接口
org.spring.springboot.service包下创建CityDubboService.java

1
2
3
4
5
6
7
public interface CityDubboService {
    /**
     * 根据城市名称,查询城市信息
     * @param cityName
     */

    City findCityByName(String cityName);
}

5、impl实现类
org.spring.springboot.impl包下创建CityDubboConsumerService.java
@Reference表明其消费的服务以及版本

1
2
3
4
5
6
7
8
9
10
11
12
@Component
public class CityDubboConsumerService {

    @Reference(version = "1.0.0")
    CityDubboService cityDubboService;

    public void printCity() {
        String cityName="永州";
        City city = cityDubboService.findCityByName(cityName);
        System.out.println(city.toString());
    }
}

6、application.properties
在src/main/resources下创建application.properties,内容如下:

1
2
3
4
5
6
7
## dubbo client
server.port=9011

## Dubbo consumer
spring.dubbo.application.name=consumer
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.scan=org.spring.springboot

启动client工程:

四、源码
https://github.com/baijy/springcloudlearn/tree/master/chapter6

参考:
https://www.bysocket.com/?p=1681
http://blog.csdn.net/evankaka/article/details/47858707