NIUCLOUD是一款SaaS管理后台框架多应用插件+云编译。上千名开发者、服务商正在积极拥抱开发者生态。欢迎开发者们免费入驻。一起助力发展! 广告
1.4.1. 拆分工程 1)将表现层工程独立出来: buy-manager-web 2)将原来的 buy-manager 改为如下结构 buy-manager |--buy-manager-dao |--buy-manager-interface |--buy-manager-pojo |--buy-manager-service(打包方式改为 war) 1.4.2. 服务层工程 第一步:把 buy-manager 的 pom 文件中删除 buy-manager-web 模块。 ~~~ <modules> <module>buy-manager-pojo</module> <module>buy-manager-dao</module> <module>buy-manager-interface</module> <module>buy-manager-service</module> </modules> ~~~ 第二步:把 buy-manager-web 文件夹移动到 buy-manager 同一级目录。 第三步:buy-manager-service 的 pom 文件修改打包方式 `<packaging>war</packaging> ` 第四步:在 buy-manager-service 工程中添加 web.xml 文件 第五步:把 buy-manager-web 的配置文件复制到 buy-manager-service 中。 删除 springmvc.xml 第六步:web.xml 中只配置 spring 容器。删除前端控制器 第七步:发布服务 1、在 buy-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 的配置文件 aplicationContext-service.xml 中添加 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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.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://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"> <context:component-scan base-package="com.igeek.service"/> <!-- 发布dubbo服务 --> <!-- 配置application名称 --> <dubbo:application name="buy-manager"/> <!-- 配置注册中心的信息 --> <dubbo:registry protocol="zookeeper" address="192.168.11.190:2181"/> <!-- 配置dubbo服务以及端口号 --> <dubbo:protocol name="dubbo" port="20880"/> <!-- 申明需要暴露的服务的接口 --> <dubbo:service interface="com.igeek.service.ItemService" ref="itemServiceImpl"/> </beans> ~~~ 1.4.3. 表现层工程 改造 buy-manager-web 工程。 第一步:删除 mybatis、和 spring 的配置文件。只保留 springmvc.xml 第二步:修改 buy-manager-web 的 pom 文件, 1、修改 parent 为 buy-parent 2、添加 spring 和 springmvc 的 jar 包的依赖 3、删除 buy-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、buy-mangager-web 添加对 buy-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: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-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="com.igeek.egobuy.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="buy-manager-web"/> <!-- 配置注册中心的相关信息 --> <dubbo:registry protocol="zookeeper" address="192.168.11.101:2181"/> <!-- 配置dubbo服务和端口号 --> <dubbo:protocol name="dubbo" port="20881"/> <!—引用指定的服务 --> <dubbo:reference interface="com.igeek.egobuy.service.ItemService" id="itemService"/> </beans> ~~~ 第四步:在 buy-manager-web 工程中添加 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> ~~~ 1.5. Dubbo 监控中心 ![](https://box.kancloud.cn/4f5e4ee7d7a5c16fd39c8c7061ace6b5_372x150.png) 需要安装 tomcat,然后部署监控中心即可。 1、把dubbo-admin-2.5.4war放到apache-tomcat-7.0.57/webapps 目录下 2、启动 tomcat ![](https://box.kancloud.cn/85baba07abbbfb251b0eba5460dd37c0_859x153.png) 3、访问 http://192.168.11.190:8080/dubbo-admin-2.5.4/ 用户名:root 密码:root ![](https://box.kancloud.cn/91913ffa94dacba4ddde66f0a71f9b3a_954x319.png) 如果监控中心和注册中心在同一台服务器上,可以不需要任何配置。 如果不在同一台服务器,需要修改配置文件: /root/apache-tomcat-7.0.47/webapps/dubbo-admin/WEB-INF/dubbo.properties ![](https://box.kancloud.cn/a058837bf6a56ed078b17385713adcdb_963x91.png) tail -f ../logs/catalina.out