1. 拆分工程
1)將表現層工程獨立出來:
e3-manager-web
2)將原來的e3-manager改為如下結構
e3-manager
|--e3-manager-dao
|--e3-manager-interface
|--e3-manager-pojo
|--e3-manager-service(打包方式改為war)
1.1. 服務層工程
第一步:把e3-manager的pom文件中刪除e3-manager-web模塊。
第二步:把e3-manager-web文件夾移動到e3-manager同一級目錄。
第三步:e3-manager-service的pom文件修改打包方式
<packaging>war</packaging>
第四步:在e3-manager-service工程中添加web.xml文件
第五步:把e3-manager-web的配置文件復制到e3-manager-service中。
刪除springmvc.xml
第六步:web.xml 中只配置spring容器。刪除前端控制器
第七步:發布服務
1、在e3-manager-Service工程中添加dubbo依賴的jar包。
<!-- dubbo相關 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> <exclusion> <groupId>org.jboss.netty</groupId> <artifactId>netty</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> </dependency>
2、在spring的配置文件中添加dubbo的約束,然后使用dubbo:service發布服務。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"> <context:component-scan base-package="cn.e3mall.service"></context:component-scan> <!-- 使用dubbo發布服務 --> <!-- 提供方應用信息,用於計算依賴關系 --> <dubbo:application name="e3-manager" /> <dubbo:registry protocol="zookeeper" address="192.168.25.154:2181,192.168.25.154:2182,192.168.25.154:2183" /> <!-- 用dubbo協議在20880端口暴露服務 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 聲明需要暴露的服務接口 --> <dubbo:service interface="cn.e3mall.service.ItemService" ref="itemServiceImpl" /> </beans>
1.1.2. 表現層工程
改造e3-manager-web工程。
第一步:刪除mybatis、和spring的配置文件。只保留springmvc.xml
第二步:修改e3-manager-web的pom文件,
1、修改parent為e3-parent
2、添加spring和springmvc的jar包的依賴
3、刪除e3-mangager-service的依賴
4、添加dubbo的依賴
<!-- dubbo相關 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> <exclusion> <groupId>org.jboss.netty</groupId> <artifactId>netty</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> </dependency>
5、e3-mangager-web添加對e3-manager-Interface的依賴。
第三步:修改springmvc.xml,在springmvc的配置文件中添加服務的引用。
<?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:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.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-4.2.xsd"> <context:component-scan base-package="cn.e3mall.controller" /> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 引用dubbo服務 --> <dubbo:application name="e3-manager-web"/> <dubbo:registry protocol="zookeeper" address="192.168.25.154:2181,192.168.25.154:2182,192.168.25.154:2183"/> <dubbo:reference interface="cn.e3mall.service.ItemService" id="itemService" /> </beans>
第四步:在e3-manager-web工程中添加tomcat插件配置。
(需要在e3-manager-service的pom文件中也加入一個tomcat的配置,只是兩個的端口號不能相同)
<build> <plugins> <!-- 配置Tomcat插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <configuration> <path>/</path> <port>8081</port> </configuration> </plugin> </plugins> </build>
在完成上面所有配置后,需要對e3-manager分別工程進行maven install,然后對e3-manager和e3-manager-web分別進行maven build
本博客針對我個人搭建的maven項目進行工程的拆分,但是基本的dubbo搭建,基本都是這樣。
如果要啟動該項目,先需要在linux系統上安裝好zookeeper,並且啟動,這樣dubbo服務才能進行注冊
2.dubbo監控中心
需要安裝tomcat,然后部署監控中心即可。
1、部署監控中心:
[root@localhost ~]# cp dubbo-admin-2.5.4.war apache-tomcat-7.0.47/webapps/dubbo-admin.war
2、啟動tomcat
3、訪問http://192.168.25.167:8080/dubbo-admin/
用戶名:root
密碼:root
如果監控中心和注冊中心在同一台服務器上,可以不需要任何配置。
如果不在同一台服務器,需要修改配置文件:
/root/apache-tomcat-7.0.47/webapps/dubbo-admin/WEB-INF/dubbo.properties