Dubbo出现的目的是为了应对现在高并发,高数据量请求的问题。目前的垂直应用架构已经无法满足现在大数据的冲击,SOA就应运而生,而Dubbo在国内使用的还是比较多,稳定性也比较不错。

架构

节点角色说明:

Provider: 暴露服务的服务提供方。
Consumer: 调用远程服务的服务消费方。
Registry: 服务注册与发现的注册中心。
Monitor: 统计服务的调用次调和调用时间的监控中心。
Container: 服务运行容器。

调用关系说明:

服务容器负责启动,加载,运行服务提供者。 服务提供者在启动时,向注册中心注册自己提供的服务。 服务消费者在启动时,向注册中心订阅自己所需的服务。 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

(1) 连通性:

注册中心负责服务地址的注册与查找,相当于目录服务,服务提供者和消费者只在启动时与注册中心交互,注册中心不转发请求,压力较小

监控中心负责统计各服务调用次数,调用时间等,统计先在内存汇总后每分钟一次发送到监控中心服务器,并以报表展示

服务提供者向注册中心注册其提供的服务,并汇报调用时间到监控中心,此时间不包含网络开销

服务消费者向注册中心获取服务提供者地址列表,并根据负载算法直接调用提供者,同时汇报调用时间到监控中心,此时间包含网络开销

注册中心,服务提供者,服务消费者三者之间均为长连接,监控中心除外

注册中心通过长连接感知服务提供者的存在,服务提供者宕机,注册中心将立即推送事件通知消费者

注册中心和监控中心全部宕机,不影响已运行的提供者和消费者,消费者在本地缓存了提供者列表

注册中心和监控中心都是可选的,服务消费者可以直连服务提供者

(2) 健状性:

监控中心宕掉不影响使用,只是丢失部分采样数据

数据库宕掉后,注册中心仍能通过缓存提供服务列表查询,但不能注册新服务

注册中心对等集群,任意一台宕掉后,将自动切换到另一台

注册中心全部宕掉后,服务提供者和服务消费者仍能通过本地缓存通讯

服务提供者无状态,任意一台宕掉后,不影响使用

服务提供者全部宕掉后,服务消费者应用将无法使用,并无限次重连等待服务提供者恢复

(3) 伸缩性:

注册中心为对等集群,可动态增加机器部署实例,所有客户端将自动发现新的注册中心

服务提供者无状态,可动态增加机器部署实例,注册中心将推送新的服务提供者信息给消费者

(4) 升级性:

当服务集群规模进一步扩大,带动IT治理结构进一步升级,需要实现动态部署,进行流动计算,现有分布式服务架构不会带来阻力:

可以得出,注册中心若宕机,只要没有注册新的服务,服务提供者和消费者还是可以根据本地缓存进行沟通。
并且注册中心不是请求转发中心,所以压力是比较小的。

搭建Dubbo工程

1、ZooKeeper的配置安装

2、Dubbo-admin配置安装

3、工程API工程创建

工程使用Maven+SpringMVC的方式构建

ITestService.java

1
2
3
4
5
6
<code class="language-java hljs ">package com.gege.service;
 
public interface ITestService {
    public String getName();
}
</code>

DubboInit.java

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
<code class="language-java hljs ">package com.gege.tool;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
 
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class DubboInit extends HttpServlet {
 
    /**
     *
     */
    private static final long serialVersionUID = 8279515033200832L;
    private Logger logger = (Logger) LoggerFactory.getLogger(DubboInit.class);
    /**
     * 启动dubbo容器
     */
    public void init() throws ServletException {
        try {
            startApplicationContext();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        }
    }
 
    public static ApplicationContext applicationContext = null;
 
    /**
     * 启动spring容器
     * @return
     */
    public static ApplicationContext startApplicationContext() {
        if (applicationContext == null) {
            applicationContext = new ClassPathXmlApplicationContext("classpath*:applicationContext.xml");
        }
        return applicationContext;
    };
 
}
</code>

pom.xml

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
    <modelversion>4.0.0</modelversion>
    <groupid>com.gege</groupid>
    <artifactid>api</artifactid>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <project.build.sourceencoding>UTF-8</project.build.sourceencoding>
        <spring.version>3.2.8.RELEASE</spring.version>
    </properties>
 
    <dependencies>
        <dependency>
            <groupid>junit</groupid>
            <artifactid>junit</artifactid>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupid>javax.servlet</groupid>
            <artifactid>servlet-api</artifactid>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <!--{cke_protected}{C}%3C!%2D%2D%20spring%E7%9B%B8%E5%85%B3%20%2D%2D%3E-->
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-core</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-beans</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-context</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-jdbc</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-web</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-webmvc</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-aop</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-tx</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-orm</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-context-support</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-test</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-jms</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.slf4j</groupid>
            <artifactid>slf4j-log4j12</artifactid>
            <version>1.7.2</version>
        </dependency>
        <dependency>
            <groupid>commons-lang</groupid>
            <artifactid>commons-lang</artifactid>
            <version>2.5</version>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupid>org.apache.maven.plugins</groupid>
                <artifactid>maven-compiler-plugin</artifactid>
                <configuration>
                    <source>1.7
                    <target>1.7</target>
                    <encoding>utf8</encoding>
                    <filenamemapping>no-version</filenamemapping>
                </configuration><source><source><source>
            </plugin><source><source><source><source><source><source>
        </plugins><source><source><source><source><source><source><source><source><source><source>
    </build><source><source><source><source><source><source><source><source><source><source><source><source><source><source><source>
</project></code>

4、Dubbo服务提供者工程创建与配置

工程使用Maven+SpringMVC的方式构建

pom.xml

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
    <modelversion>4.0.0</modelversion>
    <groupid>com.gege</groupid>
    <artifactid>provider</artifactid>
    <packaging>jar</packaging>
    <version>0.0.1-SNAPSHOT</version>
 
    <properties>
        <spring.version>3.2.8.RELEASE</spring.version>
        <project.build.sourceencoding>UTF-8</project.build.sourceencoding>
    </properties>
 
    <dependencies>
        <dependency>
            <groupid>com.alibaba</groupid>
            <artifactid>dubbo</artifactid>
            <version>2.5.3</version>
            <exclusions>
                <exclusion>
                    <groupid>org.springframework</groupid>
                    <artifactid>spring</artifactid>
                </exclusion>
            </exclusions>
        </dependency>
        <!--{cke_protected}{C}%3C!%2D%2D%20%E6%B7%BB%E5%8A%A0zk%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%BE%9D%E8%B5%96%20%2D%2D%3E-->
        <dependency>
            <groupid>com.github.sgroschupf</groupid>
            <artifactid>zkclient</artifactid>
            <version>0.1</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-core</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-beans</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-context</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-jdbc</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-web</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-webmvc</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-aop</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-tx</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-orm</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-context-support</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-test</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-jms</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>javax.servlet</groupid>
            <artifactid>jstl</artifactid>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupid>com.gege</groupid>
            <artifactid>api</artifactid>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupid>javax.servlet</groupid>
            <artifactid>servlet-api</artifactid>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupid>log4j</groupid>
            <artifactid>log4j</artifactid>
            <version>1.2.14</version>
        </dependency>
 
        <dependency>
            <groupid>cglib</groupid>
            <artifactid>cglib</artifactid>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupid>commons-lang</groupid>
            <artifactid>commons-lang</artifactid>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupid>taglibs</groupid>
            <artifactid>standard</artifactid>
            <version>1.1.2</version>
        </dependency>
        <dependency>
            <groupid>org.slf4j</groupid>
            <artifactid>slf4j-log4j12</artifactid>
            <version>1.7.2</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
              //静态资源文件扫描地址
                <directory>${project.basedir}/src/main/resources</directory>
                <includes>
                    <include>*.*</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
    <profiles>
        <profile>
            <id>local</id>
            <properties>
              //这是zookeeper的地址,要根据实际情况进行配置,默认zookeeper://127.0.0.1:2181
                <dubbo.gege.address>zookeeper://127.0.0.1:2181</dubbo.gege.address>
              //dubbo暴露的端口配置
                <dubbo.gege.port>29014</dubbo.gege.port>
              //工程的服务集合,${user.name}表示当前PC的UserName
                <dubbo.gege.group>${user.name}</dubbo.gege.group>
              //dubbo服务版本号
                <dubbo.gege.version>1.0.0</dubbo.gege.version>
            </properties>
            <activation>
                <activebydefault>true</activebydefault>
            </activation>
        </profile>
        <profile>
            <id>dev</id>
            <properties>
                <dubbo.gege.address>zookeeper://127.0.0.1:2181</dubbo.gege.address>
                <dubbo.gege.port>29014</dubbo.gege.port>
                <dubbo.gege.group>${user.name}</dubbo.gege.group>
                <dubbo.gege.version>1.0.0</dubbo.gege.version>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <dubbo.gege.address>zookeeper://127.0.0.1:2181</dubbo.gege.address>
                <dubbo.gege.port>29014</dubbo.gege.port>
                <dubbo.gege.group>${user.name}</dubbo.gege.group>
                <dubbo.gege.version>1.0.0</dubbo.gege.version>
            </properties>
        </profile>
        <profile>
            <id>product</id>
            <properties>
                <dubbo.gege.address>zookeeper://127.0.0.1:2181</dubbo.gege.address>
                <dubbo.gege.port>29014</dubbo.gege.port>
                <dubbo.gege.group>${user.name}</dubbo.gege.group>
                <dubbo.gege.version>1.0.0</dubbo.gege.version>
            </properties>
        </profile>
    </profiles>
 
</project>
</code>

TestServiceImpl.java

1
2
3
4
5
6
7
8
9
10
11
12
13
<code class="language-java hljs ">package com.gege.service.impl;
 
import org.springframework.stereotype.Service;
 
import com.gege.service.ITestService;
 
@Service
public class TestServiceImpl implements ITestService {
    public String getName() {
        return "gege";
    }
}
</code>

applicationContext.xml

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
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/task 
        http://www.springframework.org/schema/task/spring-task-3.0.xsd">
 
    <task:annotation-driven>
        //注解扫描
        <context:component-scan base-package="com.gege">
    <context:annotation-config>
 
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor">
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" id="propertyConfigurer">
        <property name="locations">
            <list>
                <value>classpath*:statics.properties</value>
            </list>
        </property>
    </bean>
 
 
    <import resource="dubbo-provider.xml">
</import></bean></context:annotation-config></context:component-scan></task:annotation-driven></beans>
</code>

dubbo-provider.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<code class="language-xml hljs "><!--?xml version="1.0" encoding="UTF-8"?-->
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.alibabatech.com/schema/dubbo
 
    <dubbo:application name="provider">
    <dubbo:registry address="${dubbo.gege.address}" port="${dubbo.gege.port}" protocol="dubbo">
    <dubbo:protocol name="dubbo" port="${dubbo.gege.port}">
    <dubbo:monitor protocol="registry">
 
    <dubbo:service group="${dubbo.gege.group}" interface="com.gege.service.ITestService" ref="testServiceImpl" version="${dubbo.gege.version}">
</dubbo:service></dubbo:monitor></dubbo:protocol></dubbo:registry></dubbo:application></beans>
</code>

log4j.properties

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<code class="language-xml hljs ">log4j.rootLogger=debug, stdout, R
 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
 
#Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=[%d] %t (%F:%L) %-5p - %m%n
 
log4j.appender.R.Encoding=UTF-8
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.DatePattern='_'yyyy-MM-dd_HH'.log'
log4j.appender.R.File=${catalina.home}/logs/1.0_order_provider
log4j.appender.R.ImmediateFlush=true
log4j.appender.R.MaxFileSize=10000KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=20
 
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=[%d] %t %c (%F:%L) %-5p - %m%n
 
log4j.category.com.cheguo=info
log4j.category.org.springframework=info
log4j.category.freemarker=info</code>

statics.properties

1
2
3
4
5
6
7
<code class="language-xml hljs ">dubbo.gege.address=${dubbo.gege.address}
dubbo.gege.port=${dubbo.gege.port}
dubbo.gege.version=${dubbo.gege.version}
dubbo.gege.group=${dubbo.gege.group}
dubbo.gege.check=false
dubbo.gege.retries=0
dubbo.gege.timeout=10000</code>

web.xml

1
2
3
4
5
6
7
8
9
10
11
<code class="language-xml hljs ">
 
<web-app>
    <display-name>provider</display-name>
    <servlet>
        <servlet-name>dubboService</servlet-name>
        <servlet-class>com.gege.tool.DubboInit</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
</web-app>
</code>

5、Dubbo服务消费者工程创建与配置

pom.xml

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
    <modelversion>4.0.0</modelversion>
    <groupid>com.gege</groupid>
    <artifactid>consumer</artifactid>
    <packaging>jar</packaging>
    <version>0.0.1-SNAPSHOT</version>
 
    <properties>
        <spring.version>3.2.8.RELEASE</spring.version>
        <project.build.sourceencoding>UTF-8</project.build.sourceencoding>
    </properties>
 
    <dependencies>
        <dependency>
            <groupid>com.alibaba</groupid>
            <artifactid>dubbo</artifactid>
            <version>2.5.3</version>
            <exclusions>
                <exclusion>
                    <groupid>org.springframework</groupid>
                    <artifactid>spring</artifactid>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupid>com.github.sgroschupf</groupid>
            <artifactid>zkclient</artifactid>
            <version>0.1</version>
        </dependency>
        <!--{cke_protected}{C}%3C!%2D%2D%20spring%E7%9B%B8%E5%85%B3%20%2D%2D%3E-->
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-core</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-beans</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-context</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-jdbc</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-web</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-webmvc</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-aop</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-tx</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-orm</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-context-support</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-test</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-jms</artifactid>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupid>com.gege</groupid>
            <artifactid>provider</artifactid>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupid>com.gege</groupid>
            <artifactid>api</artifactid>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupid>javax.servlet</groupid>
            <artifactid>servlet-api</artifactid>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupid>log4j</groupid>
            <artifactid>log4j</artifactid>
            <version>1.2.14</version>
        </dependency>
 
        <dependency>
            <groupid>cglib</groupid>
            <artifactid>cglib</artifactid>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupid>commons-lang</groupid>
            <artifactid>commons-lang</artifactid>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupid>taglibs</groupid>
            <artifactid>standard</artifactid>
            <version>1.1.2</version>
        </dependency>
        <dependency>
            <groupid>org.slf4j</groupid>
            <artifactid>slf4j-log4j12</artifactid>
            <version>1.7.2</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
                <includes>
                    <include>*.*</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
    <profiles>
        <profile>
            <id>local</id>
            <properties>
                <dubbo.gege.address>zookeeper://127.0.0.1:2181</dubbo.gege.address>
                <dubbo.gege.group>${user.name}</dubbo.gege.group>
                <dubbo.gege.version>1.0.0</dubbo.gege.version>
            </properties>
            <activation>
                <activebydefault>true</activebydefault>
            </activation>
        </profile>
        <profile>
            <id>dev</id>
            <properties>
                <dubbo.gege.address>zookeeper://127.0.0.1:2181</dubbo.gege.address>
                <dubbo.gege.group>${user.name}</dubbo.gege.group>
                <dubbo.gege.version>1.0.0</dubbo.gege.version>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <dubbo.gege.address>zookeeper://127.0.0.1:2181</dubbo.gege.address>
                <dubbo.gege.group>${user.name}</dubbo.gege.group>
                <dubbo.gege.version>1.0.0</dubbo.gege.version>
            </properties>
        </profile>
        <profile>
            <id>product</id>
            <properties>
                <dubbo.gege.address>zookeeper://127.0.0.1:2181</dubbo.gege.address>
                <dubbo.gege.group>${user.name}</dubbo.gege.group>
                <dubbo.gege.version>1.0.0</dubbo.gege.version>
            </properties>
        </profile>
    </profiles>
 
</project>
</code>

applicationContext.xml

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
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/task 
        http://www.springframework.org/schema/task/spring-task-3.0.xsd">
 
 
    <task:annotation-driven>
        <context:component-scan base-package="com.gege">
    <context:annotation-config>
 
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" id="propertyConfigurer">
        <property name="locations">
            <list>
                <value>classpath*:statics.properties</value>
            </list>
        </property>
    </bean>
 
    <import resource="dubbo-customer.xml">
</import></context:annotation-config></context:component-scan></task:annotation-driven></beans> </code>

dubbo-customer.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.alibabatech.com/schema/dubbo
 
    <dubbo:application name="consumer"></dubbo:application>
    <dubbo:registry address="${dubbo.gege.address}" id="gege_center" protocol="dubbo">
    <dubbo:provider group="${dubbo.gege.group}" registry="gege_center" version="${dubbo.gege.version}">
    <dubbo:consumer check="false" group="${dubbo.gege.group}" registry="gege_center" version="${dubbo.gege.version}">
 
    <dubbo:reference id="testService" interface="com.gege.service.ITestService" retries="${dubbo.gege.retries}" timeout="${dubbo.gege.timeout}">
</dubbo:reference></dubbo:consumer></dubbo:provider></dubbo:registry></beans>
</code>

log4j.properties

同上

statics.properties

1
2
3
4
5
<code class="language-xml hljs ">dubbo.gege.address=${dubbo.gege.address}
dubbo.gege.group=${dubbo.gege.group}
dubbo.gege.version=${dubbo.gege.version}
dubbo.gege.retries=0
dubbo.gege.timeout=10000</code>

ConsumerServiceTest.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<code class="language-java hljs ">package com.gege.service;
 
import java.io.IOException;
 
import org.apache.log4j.Logger;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class ConsumerServiceTest {
    Logger logger=Logger.getLogger(ConsumerServiceTest.class);
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "applicationContext.xml" });
        context.start();
        ITestService testService = (ITestService) context.getBean("testService");
        System.out.println(testService.getName());
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        
 
    }
}
</code>

6、运行测试

运行zookeeper,双击zkServer.cmd

运行dubbo-admin,双击Tomcat7w.exe,点击start

在eclipse中用tomcat运行服务提供者,可能存在超时,配置tomcat的timeout配置就可以了

在eclipse中用tomcat运行服务消费者

消费者工程的ConsumerServiceTest下,右键

就可以查看是否成功

正常结果:

gege

161130、Dubbo+SpringMVC工程创建详解的更多相关文章

  1. SpringMVC 常用注解 详解

    SpringMVC 常用注解 详解 SpringMVC 常用注解 1.@RequestMapping                                      路径映射 2.@Requ ...

  2. Dubbo微容器(Cooma)详解

    ExtensionLoader ExtensionLoader是Dubbo中的SPI的实现方法,它是Dubbo框架的微容器,也为框架提供各种组件的扩展点 三种注解 SPI Adaptive Activ ...

  3. SpringMvc测试框架详解----服务端测试

    随着RESTful Web Service的流行,测试对外的Service是否满足期望也变的必要的.从Spring 3.2开始Spring了Spring Web测试框架,如果版本低于3.2,请使用sp ...

  4. Dubbo 原理和机制详解 (非常全面)

    Dubbo 是一款Java RPC框架,致力于提供高性能的 RPC 远程服务调用方案.作为主流的微服务框架之一,Dubbo 为开发人员带来了非常多的便利. 大家好,我是 mikechen,专注分享「互 ...

  5. springMVC的注解详解

    springmvc常用注解标签详解 1.@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业 ...

  6. SpringMVC异常处理机制详解[附带源码分析]

    目录 前言 重要接口和类介绍 HandlerExceptionResolver接口 AbstractHandlerExceptionResolver抽象类 AbstractHandlerMethodE ...

  7. springMVC(1)---@RequestMapping详解

    @RequestMapping详解 RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径.这句话,太熟悉了.   ...

  8. springmvc常用注解详解

    1.@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ...

  9. SpringMVC @RequestMapping 用法详解之地址映射

    @RequestMapping 用法详解之地址映射 http://blog.csdn.net/walkerjong/article/details/7994326

随机推荐

  1. Vim编辑器-批量注释与反注释

    标签:linuxLinuxLINUXvimVIMVim编程 2013-01-10 09:52 27517人阅读 评论(3) 收藏 举报  分类: Linux(18)  版权声明:本文为博主原创文章,未 ...

  2. Fiddler学习纪要

    一.Fiddler支持功能 1.查看几乎所有的浏览器.客户端应用或服务器之间的WEB数据流 2.手动或自动修改任意的请求或响应 3.解密HTTPS数据流以便查看或修改 4.归档捕获到的数据流,支持再不 ...

  3. PHP 数组和字符串互相转换实现方法

    $array=explode(separator,$string); $string=implode(glue,$array);

  4. Android --设置Toast消失时间

    参考博客:Android开发,Toast重复显示(显示时间过长)解决方法 package com.dr.app.drseamoniter.toast; import android.content.C ...

  5. 第三篇 Replication:事务复制-发布服务器

    本篇文章是SQL Server Replication系列的第三篇,详细内容请参考原文. 发布服务器是所有复制数据的源头.每一个发布服务器上可以定义多个发布.每一个发布包含一组项目(项目在同一个数据库 ...

  6. Java基础之处理事件——使窗口处理自己的事件(Skethcer 1 handing its own closing event)

    控制台程序. 为表示事件的常量使用标识符可以直接启用组件对象的特定事件组.调用组件的enableEvent()方法,并把想要启用事件的标识符传送为参数,但这只在不使用监视器的情况下有效.注册监听器会自 ...

  7. G面经prepare: Data Stream Average

    给一个datastream和一个fixed window size, 让我design一个class可以完成add number还有find average in the window. 就是不能用v ...

  8. Mac下无法安装Dragon Bones的解决方案

    在Mac下安装dragon bones 然后很郁闷的发现 没有 Exchange Manger 然后就去官网下载了一个 不过下载以后发现 怎么都认不出我的Flash CC来 一安装zxp扩展就提示我没 ...

  9. 树链剖分(单点更新,求区间最值,区间求和Bzoj1036)

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 5759  Solved: 2383 [Submi ...

  10. [转]数据库高可用架构(MySQL、Oracle、MongoDB、Redis)

    一.MySQL   MySQL小型高可用架构 方案:MySQL双主.主从 + Keepalived主从自动切换 服务器资源:两台PC Server 优点:架构简单,节省资源 缺点:无法线性扩展,主从失 ...