客户端和服务端都得加入 Dubbo
第一步:
e3-manager-service 配置 pom.xml
发布服务 找到 applicationContext-service.xml
image.PNG
前面是前缀, 后面是约束
- xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
- http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
有的时候, 如果没有配置约束, 在 xml 文件中就没有快捷提示, 开发很不方便, 下面讲一讲如何配置约束
一, DTD 类型约束文件
以配置 dubbo 约束为例
1,Windows--->Preferences--->xml--->xml Catalog--->User Specified Entries 窗口, 点击 Add 按钮
2. 在 Add xml Catalog Entry 对话框中选择或输入以下内容:
- Location: F:\soft\programmingSoft\Framework\Ibatis\sql-map-config-2.dtd
- Key Type: URI
- KEY: http://ibatis.apache.org/dtd/sql-map-config-2.dtd
二, XSD 类型约束文件
1,Windows--->Preferences--->xml--->xml Catalog--->User Specified Entries 窗口, 点击 Add 按钮
image.PNG
2. 在 Add xml Catalog Entry 对话框中选择或输入以下内容:
- Location:D:\txd\dubbo.xsd
- Key Type: Schema Location
- KEY: http://code.alibabatech.com/schema/dubbo/dubbo.xsd
image.PNG
加载之后 出现下载 Spring2.5.6 这个 service 依赖于 spring 容器, dubbo 框架的依赖比较老, 所以不需要它下载
image.PNG
所以配置 dubbo 的时候把 spring 的依赖排除
- <!-- 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>
- <!-- 使用 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" />
引用服务
e3-manager-web 中的 pom.xml
- <!-- 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>
springmvc.xml 配置 dubbo 的前缀和约束
image.PNG
- <?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>
当时为什么把接口提出一个 JAR 包?
因为在 e3-manager-Web 可以直接依赖 e3-manager-interface
依赖后 定义的接口 两方都有
如果不配置 jar 包. 那么我们要把所有的接口都复制到 e3-manager-Web 中
要在 e3-manager-Web pom.xml 中
image.PNG
来源: http://www.jianshu.com/p/90d84b47b7a8