IDEA Dubbo+ZooKeeper+SpringMVC 多模块小程序

本文主要用了Dubbo+ZooKeeper+SpringMVC做的一个小程序,阅读请确保您的dubbo和zookperr的服务已经搭建好 
开始:新建一个空的MAVEN项目这里写图片描述
这里写图片描述
点击完成,建立最外层工程,该工程并无实际意义,只是为了装载其他工程 
新建Controller工程,同样也是Maven项目,这个选择模板 
这里写图片描述 
这里写图片描述
这里写图片描述

这里写图片描述
新建Service工程,这里的MAVEN不用选择模板,空创建就好 
这里写图片描述
这里写图片描述
这里写图片描述
同理,创建Interfenct工程,MAVEN也不用选择模板,空创建就好,命名自己看着合适就行,下面给出工程暂时结构 
Controller 里面在Main里面建个JAVA文件夹,设置成SourcesRoot,不然不能够创建包和类,然后在资源文件夹下创建xml文件 
这里写图片描述 
创建接口MAVEN工程 
这里写图片描述 
创建服务MAVEN工程,同Controller 
这里写图片描述 
接口的一个小方法就不给代码了 
这里写图片描述 
添加Service项目里面的MAVEN依赖项,这里先是依赖一下接口工程,因为要实现接口工程里面方法 
这里写图片描述
然后在Service里面实现接口工程里面的方法 
这里写图片描述
然后在Service的mavan配置文件添加dubbo和zookeeper的jar包,给出依赖的代码

?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">
    <parent>
        <artifactId>parentDemo</artifactId>
        <groupId>com.parent</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>MyService</artifactId>
   <dependencies>
       <dependency>
           <groupId>com.parent</groupId>
           <artifactId>MyInterfence</artifactId>
           <version>1.0-SNAPSHOT</version>
       </dependency>

       <dependency>
           <groupId>com.alibaba</groupId>
           <artifactId>dubbo</artifactId>
           <version>2.5.3</version>
       </dependency>
       <dependency>
           <groupId>org.javassist</groupId>
           <artifactId>javassist</artifactId>
           <version>3.18.2-GA</version>
       </dependency>
       <dependency>
           <groupId>org.apache.zookeeper</groupId>
           <artifactId>zookeeper</artifactId>
           <version>3.3.6</version>
       </dependency>
       <dependency>
           <groupId>com.github.sgroschupf</groupId>
           <artifactId>zkclient</artifactId>
           <version>0.1</version>
       </dependency>
   </dependencies>

</project>
 
 
  • 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

这里写图片描述

Conntroller工程maven配置文件的依赖

<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/maven-v4_0_0.xsd">
    <parent>
        <artifactId>parentDemo</artifactId>
        <groupId>com.parent</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>MyController</artifactId>
    <packaging>war</packaging>
    <name>MyController Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.parent</groupId>
            <artifactId>MyInterfence</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <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>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
        </dependency>


        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>

    </dependencies>
    <build>
        <finalName>MyController</finalName>
    </build>
</project>

 
 
  • 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

这里写图片描述
这里写图片描述
在服务工程新建一类,主要用来加载配置文件,开启提供者服务,这里zookeeper的服务要开启 
这里写图片描述
注册提供者,在配置文件中进行注册

<?xml version="1.0" encoding="ISO-8859-1"?>

<beans  xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:jee="http://www.springframework.org/schema/jee"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
             http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
        default-lazy-init="false" >


    <bean id="myInterfence" class="com.start.service.MyInterfenceImpl"></bean>

    <dubbo:application name="dubbo_provider"></dubbo:application>

    <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false" subscribe="false" ></dubbo:registry>


    <dubbo:service interface="com.start.interfence.IMyInterfence" ref="myInterfence" />



</beans>
 
 
  • 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

这里写图片描述
登录dubbo查看提供者,可以看到已经配好了,这个页面即之前下载过的dubbo.war放到Tomcat webAPP目录下打开的页面 
这里写图片描述
在Controller的xml配置文件中,配置SpringMVC注解,视图解析器,dubbo

<?xml version='1.0' encoding='UTF-8'?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
 http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <context:component-scan base-package="com.start.controller"/>
    <!-- 配置注解驱动 -->
    <mvc:annotation-driven/>
    <mvc:default-servlet-handler/>
    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <dubbo:application name="dubbo_consumer"></dubbo:application>
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false"></dubbo:registry>
    <!-- 要引用的服务 -->
    <dubbo:reference interface="com.start.interfence.IMyInterfence" id="myInterfence"></dubbo:reference>
</beans>
 
 
  • 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

这里写图片描述
在Controller工程里的web.xml添加配置信息,几个xml文件不要弄混了,MAVAWEN的,dubbo的,这还一个Controller工程的web.xml的 
这里写图片描述
这里写图片描述
然后在Controller开始我们的编码

package com.start.controller;

import com.start.interfence.IMyInterfence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by 123 on 2017/3/14.
 */
@Controller
public class MyController {
    @Autowired
    private IMyInterfence myInterfence;
    @RequestMapping("/test.do")
    public String getTest() {
        System.out.print("11111111111");
        String str = myInterfence.helloWolrd();
        System.out.print(str);
        return "welcome";
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

这里写图片描述
配置Tomcat,然后启动 
这里写图片描述 
这里报错了,因为我在后台开了一个Tomcat了,端口号为8080,开着的目的,主要是打开dubbo的admin页面,查看提供者和消费者的 
这里写图片描述 
这里写图片描述 
把端口号换一下,重新运行 
这里写图片描述
打开界面,输入配置的路径,然后输入请求路径,成功 
这里写图片描述
控制台也打印出相关信息 
这里写图片描述 
登录到dubbo的admin页面,发现消费者也出来了 
这里写图片描述
至此一个简单的小项目就完成了。 
注意,在写提供者那一块,Zookeeper的服务就要打开,放那不用管它,Tomcat也可以本地先启动,为的是能够打开dubbo的admin,当然,你在Tomcat本地上先要把dubbo.war包解压后放到webapp目录下,也可以解压替换root文件夹,这样一打开tomcat首页就是dubbo-admin页面,注意后来发布项目的时候,Tomcat的端口号不要再用8080了,容易冲突。 
谢谢观赏!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值