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依赖
  1. <dependencies>
  2. <!-- zk客户端依赖:curator -->
  3. <dependency>
  4. <groupId>org.apache.curator</groupId>
  5. <artifactId>curator-recipes</artifactId>
  6. <version>2.13.0</version>
  7. </dependency>
  8. <dependency>
  9. <groupId>org.apache.curator</groupId>
  10. <artifactId>curator-framework</artifactId>
  11. <version>2.13.0</version>
  12. </dependency>
  13. <!-- dubbo依赖 -->
  14. <dependency>
  15. <groupId>org.apache.dubbo</groupId>
  16. <artifactId>dubbo</artifactId>
  17. <version>2.7.0</version>
  18. </dependency>
  19. <!-- Spring依赖 -->
  20. <dependency>
  21. <groupId>org.springframework</groupId>
  22. <artifactId>spring-beans</artifactId>
  23. <version>${spring-version}</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework</groupId>
  27. <artifactId>spring-core</artifactId>
  28. <version>${spring-version}</version>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework</groupId>
  32. <artifactId>spring-context</artifactId>
  33. <version>${spring-version}</version>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework</groupId>
  37. <artifactId>spring-expression</artifactId>
  38. <version>${spring-version}</version>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.springframework</groupId>
  42. <artifactId>spring-aop</artifactId>
  43. <version>${spring-version}</version>
  44. </dependency>
  45. <dependency>
  46. <groupId>org.springframework</groupId>
  47. <artifactId>spring-aspects</artifactId>
  48. <version>${spring-version}</version>
  49. </dependency>
  50. <dependency>
  51. <groupId>org.springframework</groupId>
  52. <artifactId>spring-tx</artifactId>
  53. <version>${spring-version}</version>
  54. </dependency>
  55. <dependency>
  56. <groupId>org.springframework</groupId>
  57. <artifactId>spring-jdbc</artifactId>
  58. <version>${spring-version}</version>
  59. </dependency>
  60. <dependency>
  61. <groupId>org.springframework</groupId>
  62. <artifactId>spring-web</artifactId>
  63. <version>${spring-version}</version>
  64. </dependency>
  65. <dependency>
  66. <groupId>org.springframework</groupId>
  67. <artifactId>spring-webmvc</artifactId>
  68. <version>${spring-version}</version>
  69. </dependency>
  70. <!-- commons-logging依赖 -->
  71. <dependency>
  72. <groupId>commons-logging</groupId>
  73. <artifactId>commons-logging</artifactId>
  74. <version>1.2</version>
  75. </dependency>
  76. <!--业务接口工程依赖-->
  77. <dependency>
  78. <groupId>00-api</groupId>
  79. <artifactId>00-api</artifactId>
  80. <version>1.0-SNAPSHOT</version>
  81. </dependency>
  82. </dependencies>

(3) 定义web.xml

  1. <web-app>
  2. <!--注册Spring配置文件-->
  3. <context-param>
  4. <param-name>contextConfigLocation</param-name>
  5. <param-value>classpath:spring-*.xml</param-value>
  6. </context-param>
  7. <!--注册ServletContext监听器-->
  8. <listener>
  9. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  10. </listener>
  11. </web-app>

(4) 修改spring-provider.xml

在src/main/resources目录下

 

二、创建消费者03-consumer-web

(1) 创建工程

创建Maven的web工程

(2) 导入依赖

与提供者工程中的依赖相同

(3) 定义处理器

(4) 定义欢迎页面

在src/main/webapp目录下定义欢迎页面

(5) 定义web.xml

  1. <!--字符编码过滤器-->
  2. <filter>
  3. <filter-name>CharacterEncodingFilter</filter-name>
  4. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  5. <init-param>
  6. <param-name>encoding</param-name>
  7. <param-value>utf-8</param-value>
  8. </init-param>
  9. <init-param>
  10. <param-name>forceEncoding</param-name>
  11. <param-value>true</param-value>
  12. </init-param>
  13. </filter>
  14. <filter-mapping>
  15. <filter-name>CharacterEncodingFilter</filter-name>
  16. <url-pattern>/*</url-pattern>
  17. </filter-mapping>
  18. <!--注册中央调度器-->
  19. <servlet>
  20. <servlet-name>springmvc</servlet-name>
  21. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  22. <init-param>
  23. <param-name>contextConfigLocation</param-name>
  24. <param-value>classpath:spring-*.xml</param-value>
  25. </init-param>
  26. <load-on-startup>1</load-on-startup>
  27. </servlet>
  28. <servlet-mapping>
  29. <servlet-name>springmvc</servlet-name>
  30. <!--不能写/*,不建议写/,建议扩展名方式-->
  31. <url-pattern>*.do</url-pattern>
  32. </servlet-mapping>

(6) 修改spring-consumer.xml

在src/main/resources目录下

三、部署

浏览器访问

后台消息

posted @ 2020-07-07 11:33  edda_huang  阅读(244)  评论(0编辑  收藏  举报