浅谈分布式框架的搭建(一) 简单工程的搭建

谁借莪1个温暖的怀抱¢ 2022-05-28 07:51 308阅读 0赞
  1. 在之前学习springssm框架时,我通常都是将工程发布到apache/tomcat 服务器下的。也就是说,一个的工程的controller层,service层还有dao层都需要在一个服务器下进行工作。这无疑会降低工作效率,如果我们可以将一个工程的业务发布到多个服务器下进行工作,这会使我们的工作效率提升很多。
  2. 那么这就需要引入分布式工程。其中需要的工具有dubbozookeeper.
  3. 首先,dubbo是一个分布式服务开发框架。在dubbo这个框架中,有许多重要的节点。
  4. Provider: 暴露服务的服务提供方。
  5. Consumer: 调用远程服务的服务消费方。
  6. Registry: 服务注册与发现的注册中心。
  7. Monitor: 统计服务的调用次调和调用时间的监控中心。
  8. Container: 服务运行容器。

大家或许都知道生产者-消费者的问题。其中的Provider就是生产者,将服务进行暴露,也就是进行提供服务。而Consumer则是消费者,用来获取Provider提供的服务。注意,这些服务其实是一个一个的bean对象,也就是我们service层的业务逻辑处理对象。 Registry,服务注册和发现的注册中心。

服务容器负责启动,加载,运行服务提供者。

服务提供者在启动时,向注册中心注册自己提供的服务。

服务消费者在启动时,向注册中心订阅自己所需的服务。

关于服务提供者和服务消费者之间的逻辑协调,需要交由zookeeper进行管理。

zookeeper是一个注册服务中心,它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、名字服务、分布式同步、组服务等。目标就是封装好复杂易出错的关键服务,将简单易用的接口和性能高效、功能稳定的系统提供给用户。

之后我们还需要服务器来发布我们的web工程,这个服务器我们需要的是jetty服务器.

下面我以我自己的cube.shop工程为例,来首先介绍一下分布式工程的初步搭建。

首先我们需要先确定我们的工程的层次。因为我们要将视图层和业务层放在两个不同的服务器上,所以需要对我们的工程进行分层。

首先是总的父工程cube.shop

其下的三个主要的子工程cube.shop.common,cube.shop.manager.web, cube.shop.manager

首先来配置cube.shop总的父工程,它应该打包成一个pom文件。

在cube.shop这个工程里面,我们需要配置所有子工程需要的jar包的版本号,以及需要的jar包.

在group Id 需要设置成统一的com.zzxt,当然所有的子工程的group Id都需要设置成com.zzxt。

并且需要在pom.xml文件里面进行设置。具体的设置如下

首先,我们需要把需要的版本号设置一下.

  1. <properties>
  2. <junit.version>4.12</junit.version>
  3. <spring.version>4.3.14.RELEASE</spring.version>
  4. <mybatis.version>3.4.5</mybatis.version>
  5. <mybatis.spring.version>1.3.1</mybatis.spring.version>
  6. <!-- 分页使用 -->
  7. <mybatis.paginator.version>1.2.15</mybatis.paginator.version>
  8. <pagehelper.version>5.1.2</pagehelper.version>
  9. <mysql.version>5.1.32</mysql.version>
  10. <slf4j.version>1.6.4</slf4j.version>
  11. <!-- mvc中返回json字符串中使用 -->
  12. <jackson.version>2.9.4</jackson.version>
  13. <!-- 连接池 -->
  14. <druid.version>1.1.8</druid.version>
  15. <!-- 模拟浏览器 -->
  16. <httpclient.version>4.3.5</httpclient.version>
  17. <jstl.version>1.2</jstl.version>
  18. <servlet-api.version>2.5</servlet-api.version>
  19. <jsp-api.version>2.0</jsp-api.version>
  20. <!-- 工具类对于时间的处理 -->
  21. <joda-time.version>2.9.9</joda-time.version>
  22. <commons-lang3.version>3.3.2</commons-lang3.version>
  23. <commons-io.version>1.3.2</commons-io.version>
  24. <!-- 分页工具 -->
  25. <commons-net.version>3.3</commons-net.version>
  26. <jsqlparser.version>0.9.1</jsqlparser.version>
  27. <commons-fileupload.version>1.3.1</commons-fileupload.version>
  28. <jedis.version>2.7.2</jedis.version>
  29. <!-- 搜索 -->
  30. <solrj.version>4.10.3</solrj.version>
  31. <!-- 分布式 -->
  32. <dubbo.version>2.5.3</dubbo.version>
  33. <!-- 注册中心的 -->
  34. <zookeeper.version>3.4.7</zookeeper.version>
  35. <zkclient.version>0.1</zkclient.version>
  36. <!-- 消息队列 -->
  37. <activemq.version>5.11.2</activemq.version>
  38. <!-- 当页面使用,可以生产网页,用代码生成html页面 -->
  39. <freemarker.version>2.3.23</freemarker.version>
  40. <quartz.version>2.2.2</quartz.version>
  41. </properties>

然后我们需要对我们的依赖进行管理,

  1. <dependencies>
  2. <!-- 单元测试 -->
  3. <dependency>
  4. <groupId>junit</groupId>
  5. <artifactId>junit</artifactId>
  6. <version>${junit.version}</version>
  7. <scope>test</scope>
  8. </dependency>
  9. <!-- 日志处理 -->
  10. <dependency>
  11. <groupId>org.slf4j</groupId>
  12. <artifactId>slf4j-log4j12</artifactId>
  13. <version>${slf4j.version}</version>
  14. </dependency>
  15. <!-- Apache COMMONS工具组件 -->
  16. <dependency>
  17. <groupId>org.apache.commons</groupId>
  18. <artifactId>commons-lang3</artifactId>
  19. <version>${commons-lang3.version}</version>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.apache.commons</groupId>
  23. <artifactId>commons-io</artifactId>
  24. <version>${commons-io.version}</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>commons-net</groupId>
  28. <artifactId>commons-net</artifactId>
  29. <version>${commons-net.version}</version>
  30. </dependency>
  31. <!-- 时间操作组件 -->
  32. <dependency>
  33. <groupId>joda-time</groupId>
  34. <artifactId>joda-time</artifactId>
  35. <version>${joda-time.version}</version>
  36. </dependency>
  37. <!-- JSP相关 -->
  38. <dependency>
  39. <groupId>jstl</groupId>
  40. <artifactId>jstl</artifactId>
  41. <version>${jstl.version}</version>
  42. </dependency>
  43. <dependency>
  44. <groupId>javax.servlet</groupId>
  45. <artifactId>servlet-api</artifactId>
  46. <version>${servlet-api.version}</version>
  47. <scope>provided</scope>
  48. </dependency>
  49. <dependency>
  50. <groupId>javax.servlet</groupId>
  51. <artifactId>jsp-api</artifactId>
  52. <version>${jsp-api.version}</version>
  53. <scope>provided</scope>
  54. </dependency>
  55. <!-- 文件上传组件 -->
  56. <dependency>
  57. <groupId>commons-fileupload</groupId>
  58. <artifactId>commons-fileupload</artifactId>
  59. <version>${commons-fileupload.version}</version>
  60. </dependency>
  61. <!-- Jackson Json处理工具包 -->
  62. <dependency>
  63. <groupId>com.fasterxml.jackson.core</groupId>
  64. <artifactId>jackson-databind</artifactId>
  65. <version>${jackson.version}</version>
  66. </dependency>
  67. <!-- httpclient -->
  68. <dependency>
  69. <groupId>org.apache.httpcomponents</groupId>
  70. <artifactId>httpclient</artifactId>
  71. <version>${httpclient.version}</version>
  72. </dependency>
  73. <!-- quartz任务调度框架 -->
  74. <dependency>
  75. <groupId>org.quartz-scheduler</groupId>
  76. <artifactId>quartz</artifactId>
  77. <version>${quartz.version}</version>
  78. </dependency>
  79. <!-- Mybatis -->
  80. <dependency>
  81. <groupId>org.mybatis</groupId>
  82. <artifactId>mybatis</artifactId>
  83. <version>${mybatis.version}</version>
  84. </dependency>
  85. <dependency>
  86. <groupId>org.mybatis</groupId>
  87. <artifactId>mybatis-spring</artifactId>
  88. <version>${mybatis.spring.version}</version>
  89. </dependency>
  90. <dependency>
  91. <groupId>com.github.miemiedev</groupId>
  92. <artifactId>mybatis-paginator</artifactId>
  93. <version>${mybatis.paginator.version}</version>
  94. </dependency>
  95. <dependency>
  96. <groupId>com.github.pagehelper</groupId>
  97. <artifactId>pagehelper</artifactId>
  98. <version>${pagehelper.version}</version>
  99. </dependency>
  100. <!-- MySql -->
  101. <dependency>
  102. <groupId>mysql</groupId>
  103. <artifactId>mysql-connector-java</artifactId>
  104. <version>${mysql.version}</version>
  105. </dependency>
  106. <!-- 连接池 -->
  107. <dependency>
  108. <groupId>com.alibaba</groupId>
  109. <artifactId>druid</artifactId>
  110. <version>${druid.version}</version>
  111. </dependency>
  112. <!-- Spring -->
  113. <dependency>
  114. <groupId>org.springframework</groupId>
  115. <artifactId>spring-context</artifactId>
  116. <version>${spring.version}</version>
  117. </dependency>
  118. <dependency>
  119. <groupId>org.springframework</groupId>
  120. <artifactId>spring-beans</artifactId>
  121. <version>${spring.version}</version>
  122. </dependency>
  123. <dependency>
  124. <groupId>org.springframework</groupId>
  125. <artifactId>spring-webmvc</artifactId>
  126. <version>${spring.version}</version>
  127. </dependency>
  128. <dependency>
  129. <groupId>org.springframework</groupId>
  130. <artifactId>spring-jdbc</artifactId>
  131. <version>${spring.version}</version>
  132. </dependency>
  133. <dependency>
  134. <groupId>org.springframework</groupId>
  135. <artifactId>spring-aspects</artifactId>
  136. <version>${spring.version}</version>
  137. </dependency>
  138. <dependency>
  139. <groupId>org.springframework</groupId>
  140. <artifactId>spring-jms</artifactId>
  141. <version>${spring.version}</version>
  142. </dependency>
  143. <dependency>
  144. <groupId>org.springframework</groupId>
  145. <artifactId>spring-context-support</artifactId>
  146. <version>${spring.version}</version>
  147. </dependency>
  148. <!-- Redis客户端 -->
  149. <dependency>
  150. <groupId>redis.clients</groupId>
  151. <artifactId>jedis</artifactId>
  152. <version>${jedis.version}</version>
  153. </dependency>
  154. <!-- solr客户端 -->
  155. <dependency>
  156. <groupId>org.apache.solr</groupId>
  157. <artifactId>solr-solrj</artifactId>
  158. <version>${solrj.version}</version>
  159. </dependency>
  160. <!-- dubbo相关 -->
  161. <dependency>
  162. <groupId>com.alibaba</groupId>
  163. <artifactId>dubbo</artifactId>
  164. <version>${dubbo.version}</version>
  165. </dependency>
  166. <dependency>
  167. <groupId>org.apache.zookeeper</groupId>
  168. <artifactId>zookeeper</artifactId>
  169. <version>${zookeeper.version}</version>
  170. </dependency>
  171. <dependency>
  172. <groupId>com.github.sgroschupf</groupId>
  173. <artifactId>zkclient</artifactId>
  174. <version>${zkclient.version}</version>
  175. </dependency>
  176. <dependency>
  177. <groupId>org.apache.activemq</groupId>
  178. <artifactId>activemq-all</artifactId>
  179. <version>${activemq.version}</version>
  180. </dependency>
  181. <dependency>
  182. <groupId>org.freemarker</groupId>
  183. <artifactId>freemarker</artifactId>
  184. <version>${freemarker.version}</version>
  185. </dependency>
  186. <!-- maven安装插件的配置 -->
  187. <dependency>
  188. <groupId>org.apache.maven.plugins</groupId>
  189. <artifactId>maven-install-plugin</artifactId>
  190. <version>2.4</version>
  191. </dependency>

这些都是我们需要的一些jar包,之后我们还需要对我们的需要的一些工程插件进行管理。

  1. <build>
  2. <!-- 工程插件管理 -->
  3. <pluginManagement>
  4. <plugins>
  5. <plugin>
  6. <groupId>org.eclipse.jetty</groupId>
  7. <artifactId>jetty-maven-plugin</artifactId>
  8. <version>9.0.0.v20130308</version>
  9. </plugin>
  10. </plugins>
  11. </pluginManagement>
  12. <plugins>
  13. <!-- 资源文件拷贝插件 -->
  14. <plugin>
  15. <groupId>org.apache.maven.plugins</groupId>
  16. <artifactId>maven-resources-plugin</artifactId>
  17. <version>2.6</version>
  18. <configuration>
  19. <encoding>UTF-8</encoding>
  20. </configuration>
  21. </plugin>
  22. <!-- java编译插件 -->
  23. <plugin>
  24. <groupId>org.apache.maven.plugins</groupId>
  25. <artifactId>maven-compiler-plugin</artifactId>
  26. <version>3.1</version>
  27. <configuration>
  28. <source>1.8</source>
  29. <target>1.8</target>
  30. <encoding>UTF-8</encoding>
  31. </configuration>
  32. </plugin>
  33. </plugins>
  34. </build>

这个就是对我们的总的父工程cube.shop的配置。

另外就是对我们的cube.shop.commons工程的配置。

首先,要配置的是一个简单的工程(Maven Project),其中group Id也是com.zzxt。artfact Id设置为cube.shop.commons.在设置Parent Project时将相应的 artfact Id设置为cube.shop。将这个工程打包成jar包。

之后需要配置的是cube.shop.manager工程.

同样配置一个简单的工程,其中group Id也是com.zzxt。artfact Id设置为cube.shop.manager.在设置Parent Project时将相应的 artfact Id设置为cube.shop。将这个工程打包成pom文件.

接下来就是要创建关于cube.shop.manager的模块

包含的是

cube.shop.manager.enetity

cube.shop.manager.service

cube.shop.manager.dao

cube.shop.manager.inter

cube.shop.manager.web.

下面我们来一个一个创建这几个模块.

首先是cube.shop.manager.enetity ,在cube.shop.manager这个工程下面,我们创建的是一个maven module项目。设置成一个简单的项目。

cube.shop.manager.dao

cube.shop.manager.inter这两个模块的创建和cube.shop.manager.enetity是相同的.

接下来是cube.shop.manager.service这个模块,它是要发布到服务器上进行业务逻辑的处理,所以将它打成war包。

而cube.shop.manager.web,把它放到cube.shop的工程下作为一个子模块,同样是打成war包.

我们把一个工程分成了多个子工程及其模块,为了是在多台服务器,同时进行业务的处理。

其实我们是将子工程或者子模块打成jar包引入到其他我们需要的子工程中。也就是所谓的依赖注入,我们来一个一个看。

在cube.shop.manager中,我们需要引入cube.shop.commons

  1. <dependency>
  2. <groupId>com.zzxt</groupId>
  3. <artifactId>cube.shop.commons</artifactId>
  4. <version>0.0.1-SNAPSHOT</version>
  5. </dependency>

在cube.shop.manager.dao中需要引入cube.shop.manager.entity

  1. <dependency>
  2. <groupId>com.zzxt</groupId>
  3. <artifactId>cube.shop.manager.entity</artifactId>
  4. <version>0.0.1-SNAPSHOT</version>
  5. </dependency>

在cube.shop.manager.service需要引入cube.shop.manager.inter,cube.shop.manager.dao

  1. <dependency>
  2. <groupId>com.xtit</groupId>
  3. <artifactId>cube.xt.shop.manager.inter</artifactId>
  4. <version>0.0.1-SNAPSHOT</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.xtit</groupId>
  8. <artifactId>cube.xt.shop.manager.dao</artifactId>
  9. <version>0.0.1-SNAPSHOT</version>
  10. </dependency>

在cube.shop.manager.web中需要引入cube.shop.manager.service

  1. <dependency>
  2. <groupId>com.zzxt</groupId>
  3. <artifactId>cube.shop.manager.service</artifactId>
  4. <version>0.0.1-SNAPSHOT</version>
  5. </dependency>

工程的配置都已经完毕,剩下就是一些需要的插件的配置。

在我们的cube.shop.manager.web中引入jetty的插件

  1. <plugin>
  2. <groupId>org.eclipse.jetty</groupId>
  3. <artifactId>jetty-maven-plugin</artifactId>
  4. <configuration>
  5. <httpConnector>
  6. <port>7000</port>
  7. </httpConnector>
  8. </configuration>
  9. </plugin>

发表评论

表情:
评论列表 (有 0 条评论,308人围观)

还没有评论,来说两句吧...

相关阅读

    相关 ROS()工程

    1、创建Catkin的WorkSpace catkin是ROS的一个官方的编译构建系统,是原本的ROS的编译构建系统rosbuild的替代者。一个Catkin WorkSp