• 161130、Dubbo+SpringMVC工程创建详解


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

    架构

    架构

    节点角色说明:

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

    调用关系说明:

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

    (1) 连通性:

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

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

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

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

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

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

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

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

    (2) 健状性:

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

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

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

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

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

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

    (3) 伸缩性:

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

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

    (4) 升级性:

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

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

    搭建Dubbo工程

    1、ZooKeeper的配置安装

    2、Dubbo-admin配置安装

    3、工程API工程创建

    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服务消费者工程创建与配置

    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

  • 相关阅读:
    homework2
    一件关于Bug的小事
    软件测试作业三:有关控制流图、覆盖内容
    用CSS改变select框的样式
    lab1--ideal + junit
    软件测试作业二
    记一次曾经项目中遇到的错误
    02组_现代软件工程_第04次作业——利用4象限原理分析自身CanTool项目的构成
    02组_现代软件工程_第03次作业——对于自身评价(原有水平以及长远目标分析总结)
    02组_现代软件工程_第02次作业——初谈GitHub使用详解以及设计
  • 原文地址:https://www.cnblogs.com/zrbfree/p/6171002.html
Copyright © 2020-2023  润新知