您的位置:首页 > 编程语言 > Java开发

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

2018-01-15 17:07 162 查看
本文主要用了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了,容易冲突。 

谢谢观赏!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: