Dubbo应用到web工程
一、创建提供者03-provider-web
(1) 创建工程
创建Maven的web工程,然后创建实现类。
(2) 导入依赖
Spring的版本为4.3.16
需要的依赖有:
- dubbo2.7.0版本依赖
- zk客户端curator依赖
- servlet与jsp依赖
- spring相关依赖
- spring需要的commons-logging依赖
- 自定义00-api依赖
- <dependencies>
- <!-- zk客户端依赖:curator -->
- <dependency>
- <groupId>org.apache.curator</groupId>
- <artifactId>curator-recipes</artifactId>
- <version>2.13.0</version>
- </dependency>
- <dependency>
- <groupId>org.apache.curator</groupId>
- <artifactId>curator-framework</artifactId>
- <version>2.13.0</version>
- </dependency>
-
- <!-- dubbo依赖 -->
- <dependency>
- <groupId>org.apache.dubbo</groupId>
- <artifactId>dubbo</artifactId>
- <version>2.7.0</version>
- </dependency>
-
- <!-- Spring依赖 -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-beans</artifactId>
- <version>${spring-version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${spring-version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>${spring-version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-expression</artifactId>
- <version>${spring-version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aop</artifactId>
- <version>${spring-version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aspects</artifactId>
- <version>${spring-version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-tx</artifactId>
- <version>${spring-version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- <version>${spring-version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- <version>${spring-version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>${spring-version}</version>
- </dependency>
-
- <!-- commons-logging依赖 -->
- <dependency>
- <groupId>commons-logging</groupId>
- <artifactId>commons-logging</artifactId>
- <version>1.2</version>
- </dependency>
-
- <!--业务接口工程依赖-->
- <dependency>
- <groupId>00-api</groupId>
- <artifactId>00-api</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
-
- </dependencies>
(3) 定义web.xml
- <web-app>
- <!--注册Spring配置文件-->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring-*.xml</param-value>
- </context-param>
-
- <!--注册ServletContext监听器-->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
-
- </web-app>
(4) 修改spring-provider.xml
在src/main/resources目录下
二、创建消费者03-consumer-web
(1) 创建工程
创建Maven的web工程
(2) 导入依赖
与提供者工程中的依赖相同
(3) 定义处理器
(4) 定义欢迎页面
在src/main/webapp目录下定义欢迎页面
(5) 定义web.xml
- <!--字符编码过滤器-->
- <filter>
- <filter-name>CharacterEncodingFilter</filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>utf-8</param-value>
- </init-param>
- <init-param>
- <param-name>forceEncoding</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>CharacterEncodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
-
- <!--注册中央调度器-->
- <servlet>
- <servlet-name>springmvc</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring-*.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>springmvc</servlet-name>
- <!--不能写/*,不建议写/,建议扩展名方式-->
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
(6) 修改spring-consumer.xml
在src/main/resources目录下
三、部署
浏览器访问
后台消息