java搭建分布式项目

1 分布式

1.1 什么是分布式

  1. 分布式系统一定是由多个节点组成的系统。其中,节点指的是计算机服务器,而且这些节点一般不是孤立的,而是互通的。
  2. 这些连通的节点上部署了我们的节点,并且相互的操作会有协同。分布式系统对于用户而言,他们面对的就是一个服务器,提供用户需要的服务而已,而实际上这些服务是通过背后的众多服务器组成的一个分布式系统,因此分布式系统看起来像是一个超级计算机一样。

1.2 分布式与集群的区别

  1.  集群是同一个系统部在不同的服务器上,例如一个登陆系统部在不同的服务器上.
  2. 分布式是不同的系统部在不同的服务器上,服务器之间相互调用.

小饭店原来只有一个厨师,切菜洗菜备料炒菜全干。后来客人多了,厨房一个厨师忙不过来,又请了个厨师,两个厨师都能炒一样的菜,这两个厨师的关系是集群。为了让厨师专心炒菜,把菜做到极致,又请了个配菜师负责切菜,备菜,备料,厨师和配菜师的关系是分布式,一个配菜师也忙不过来了,又请了个配菜师,两个配菜师关系是集群.

2 搭建分布式项目

准备工具:eclipse,装有CentOS7系统的VMwarm,zookeeper.......最重要的,一台三年高龄的老人机.

1 首先创建一个父类的maven项目,打包方式为pom.

在eclipse中创建一个父类maven项目,打包方式为pom.为什么要创建一个父类的maven项目呢?因为要使用这个maven项目进行各个jar包版本的管理,子类想要jar包直接跟父类要就可以. xml

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

 <modelVersion>4.0.0</modelVersion>

 <groupId>com.itqf</groupId>

 <artifactId>sping-parent</artifactId>

 <version>0.0.1-SNAPSHOT</version>

 <packaging>pom</packaging>

  

 <!-- 定义所有jar包的版本 -->

     <properties>

     <junit.version>4.12</junit.version>

        <spring.version>4.2.4.RELEASE</spring.version>

        <mybatis.version>3.2.8</mybatis.version>

        <mybatis.spring.version>1.2.2</mybatis.spring.version>

        <mybatis.paginator.version>1.2.15</mybatis.paginator.version>

        <mysql.version>5.1.32</mysql.version>

        <slf4j.version>1.6.4</slf4j.version>

        <jackson.version>2.4.2</jackson.version>

        <druid.version>1.0.9</druid.version>

        <httpclient.version>4.3.5</httpclient.version>

        <jstl.version>1.2</jstl.version>

        <servlet-api.version>2.5</servlet-api.version>

        <jsp-api.version>2.0</jsp-api.version>

        <joda-time.version>2.5</joda-time.version>

        <commons-lang3.version>3.3.2</commons-lang3.version>

        <commons-io.version>1.3.2</commons-io.version>

        <commons-net.version>3.3</commons-net.version>

        <pagehelper.version>3.4.2-fix</pagehelper.version>

        <jsqlparser.version>0.9.1</jsqlparser.version>

        <commons-fileupload.version>1.3.1</commons-fileupload.version>

        <jedis.version>2.7.2</jedis.version>

        <solrj.version>4.10.3</solrj.version>

        <dubbo.version>2.5.3</dubbo.version>

        <zookeeper.version>3.4.7</zookeeper.version>

        <zkclient.version>0.1</zkclient.version>

        <activemq.version>5.11.2</activemq.version>

        <freemarker.version>2.3.23</freemarker.version>

        <quartz.version>2.2.2</quartz.version>

     </properties>

     <!-- 管理所有项目中用到的jar包,并不做真正的依赖 -->

     <dependencyManagement>

      <dependencies>

            <!-- 时间操作组件 -->

            <dependency>

                <groupId>joda-time</groupId>

2 创建一个maven的聚合工程.

2.1 创建maven聚合工程,继承父工程.

聚合工程:可以将项目中的controller层,view层等都独立成一个工程,最终运行的时候整合到一起运行.

pom.xml

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

 <modelVersion>4.0.0</modelVersion>

 <parent>

 <groupId>com.itqf</groupId>

 <artifactId>sping-parent</artifactId>

 <version>0.0.1-SNAPSHOT</version>

 </parent>

 <groupId>com.itqf</groupId>

 <artifactId>sping-manager</artifactId>

 <version>0.0.1-SNAPSHOT</version>

 <packaging>pom</packaging>

  

 <dependencies>

  <dependency>

   <groupId>com.itqf</groupId>

   <artifactId>sping-common</artifactId>

   <version>0.0.1-SNAPSHOT</version>

  </dependency>

 </dependencies>

  

 <build>

        <plugins>

            <plugin>

                <groupId>org.apache.tomcat.maven</groupId>

                <artifactId>tomcat7-maven-plugin</artifactId>

                <configuration>

                    <port>8083</port>

                    <path>/</path>

                </configuration>

            </plugin>

        </plugins>

 </build>

  

 <modules>

    <module>sping-manager-pojo</module>

    <module>sping-manager-interface</module>

    <module>sping-manager-service</module>

    <module>sping-manager-mapper</module>

 </modules>

</project>

2.2 在聚合工程中创建maven Module,命名sping-manager-pojo(实体类层).

pojo是一个普通的jar格式,不需要依赖父工程.

pom.xml

?

1

2

3

4

5

6

7

8

9

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

 <modelVersion>4.0.0</modelVersion>

 <parent>

 <groupId>com.itqf</groupId>

 <artifactId>sping-manager</artifactId>

 <version>0.0.1-SNAPSHOT</version>

 </parent>

 <artifactId>sping-manager-pojo</artifactId>

</project>

2.3 在聚合工程中创建maven Module,命名sping-manager-mapper(dao层). 在pom.xml文件中依赖pojo.因为mapper层的方法返回的是一个实体类对象的话,那么需要用到pojo.

导入依赖jar包.

xml

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

 <modelVersion>4.0.0</modelVersion>

 <parent>

 <groupId>com.itqf</groupId>

 <artifactId>sping-manager</artifactId>

 <version>0.0.1-SNAPSHOT</version>

 </parent>

 <artifactId>sping-manager-mapper</artifactId>

  

 <!-- 依赖pojo -->

 <dependencies>

  <dependency>

   <groupId>com.itqf</groupId>

   <artifactId>sping-manager-pojo</artifactId>

   <version>0.0.1-SNAPSHOT</version>

  </dependency>

   

  <!-- Mybatis -->

            <dependency>

                <groupId>org.mybatis</groupId>

                <artifactId>mybatis</artifactId>

                <version>${mybatis.version}</version>

            </dependency>

            <dependency>

                <groupId>org.mybatis</groupId>

                <artifactId>mybatis-spring</artifactId>

                <version>${mybatis.spring.version}</version>

            </dependency>

            <dependency>

                <groupId>com.github.miemiedev</groupId>

                <artifactId>mybatis-paginator</artifactId>

                <version>${mybatis.paginator.version}</version>

            </dependency>

            <dependency>

                <groupId>com.github.pagehelper</groupId>

                <artifactId>pagehelper</artifactId>

                <version>${pagehelper.version}</version>

            </dependency>

            <!-- MySql -->

            <dependency>

                <groupId>mysql</groupId>

                <artifactId>mysql-connector-java</artifactId>

                <version>${mysql.version}</version>

            </dependency>

            <!-- 连接池 -->

            <dependency>

                <groupId>com.alibaba</groupId>

                <artifactId>druid</artifactId>

                <version>${druid.version}</version>

            </dependency>

   

 </dependencies>

</project>

2.4 在聚合工程中创建sping-manager-interface(接口),将所有的service接口都放到独立的工程当中. 接口中方法返回值如果是实体类,需要用到pojo.所以在pom中依赖pojo xml

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

 <modelVersion>4.0.0</modelVersion>

 <parent>

 <groupId>com.itqf</groupId>

 <artifactId>sping-manager</artifactId>

 <version>0.0.1-SNAPSHOT</version>

 </parent>

 <artifactId>sping-manager-interface</artifactId>

  

 <dependencies>

  <dependency>

   <groupId>com.itqf</groupId>

   <artifactId>sping-manager-pojo</artifactId>

   <version>0.0.1-SNAPSHOT</version>

  </dependency>

 </dependencies>

  

</project>

2.5 在聚合项目中创建sping-manager-service(interface的实现类).打包方式为war

因为将controller层与service层分开了,所以在运行启动的时候要讲controller和service单独使用tomcat发布,将聚合工程中所需要的配置文件都放入service中,这样在Tomcat启动的时候回将配置文件都进行加载整合.

  1. service需要用到接口,所以依赖接口sping-manager-interface.
  2. service需要用到pojo,也需要调用到mapper,所以直接依赖mapper就可以,以为mapper已经依赖了pojo (依赖传递) .
  3. service需要被spring管理,让spring给service创建对象
  4. service需要dubbo的包(后面对dubbo进行介绍)
  5. service需要使用到SOA,将service当成一个服务发布出去.

配置文件

SqlMapConfig.xml

?

1

2

3

4

5

6

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE configuration

        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

</configuration>

db.properties

?

1

2

3

4

db.driver=com.mysql.jdbc.Driver

db.url=jdbc:mysql://localhost:3306/taotao?characterEncoding=UTF-8

db.username=root

db.password=root

applicationContext-tx.xml

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

<?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:tx="http://www.springframework.org/schema/tx"

    xmlns:aop="http://www.springframework.org/schema/aop"

    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://www.springframework.org/schema/tx

  http://www.springframework.org/schema/tx/spring-tx-4.2.xsd

  http://www.springframework.org/schema/aop

  http://www.springframework.org/schema/aop/spring-aop-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">

  

 <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

  <property name="dataSource" ref="dataSource"></property>

 </bean

  

 <tx:advice id="adviceId" transaction-manager="txManager">

  <tx:attributes>

   <tx:method name="add*" propagation="REQUIRED"/>

   <tx:method name="save*" propagation="REQUIRED"/>

   <tx:method name="insert*" propagation="REQUIRED"/>

   <tx:method name="update*" propagation="REQUIRED"/>

   <tx:method name="del*" propagation="REQUIRED"/>

   <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>

   <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>

   

  </tx:attributes>

 </tx:advice>

  

 <aop:config>

  <aop:advisor advice-ref="adviceId" pointcut="execution(* com.itqf.service..*.*(..))"/>

 </aop:config

   

   

</beans>

applicationContext-dao.xml

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

<?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:property-placeholder location="classpath:resource/*.properties"/>

 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">

  <property name="driverClassName" value="${db.driver}"></property>

  <property name="url" value="${db.url}"></property>

  <property name="username" value="${db.username}"></property>

  <property name="password" value="${db.password}"></property>

  <property name="maxActive" value="10"></property>

  <property name="minIdle" value="5"></property>

 </bean>

  

  

 <bean class="org.mybatis.spring.SqlSessionFactoryBean">

  <property name="dataSource" ref="dataSource"></property>

  <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>

 </bean>

  

 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

  <property name="basePackage" value="com.itqf.mapper"></property>

 </bean>

</beans>

applicationContext-service.xml

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<?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.itqf.service"></context:component-scan>

</beans>

pom.xml

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

 <modelVersion>4.0.0</modelVersion>

 <parent>

 <groupId>com.qianfeng</groupId>

 <artifactId>sping-manager</artifactId>

 <version>0.0.1-SNAPSHOT</version>

 </parent>

 <artifactId>sping-manager-service</artifactId>

  

 <packaging>war</packaging>

  

  

 <dependencies>

  <dependency>

   <groupId>com.qianfeng</groupId>

   <artifactId>sping-manager-interface</artifactId>

   <version>0.0.1-SNAPSHOT</version>

  </dependency>

   

  <dependency>

   <groupId>com.qianfeng</groupId>

   <artifactId>sping-manager-mapper</artifactId>

   <version>0.0.1-SNAPSHOT</version>

  </dependency>

   

  <!-- Spring -->

            <dependency>

                <groupId>org.springframework</groupId>

                <artifactId>spring-context</artifactId>

                <version>${spring.version}</version>

            </dependency>

            <dependency>

                <groupId>org.springframework</groupId>

                <artifactId>spring-beans</artifactId>

                <version>${spring.version}</version>

            </dependency>

            <dependency>

                <groupId>org.springframework</groupId>

                <artifactId>spring-webmvc</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-aspects</artifactId>

                <version>${spring.version}</version>

            </dependency>

            <dependency>

                <groupId>org.springframework</groupId>

                <artifactId>spring-jms</artifactId>

                <version>${spring.version}</version>

            </dependency>

            <dependency>

                <groupId>org.springframework</groupId>

                <artifactId>spring-context-support</artifactId>

                <version>${spring.version}</version>

            </dependency>

       

 </dependencies>

</project>

最后工程结构

 

3 使用dubbo将service发布服务

首先思考?

像淘宝京东这样的商城类网站,不但可以从PC端登录,还能从手机端登录,那么是怎么实现的?写两个controller?那肯定不会,谁会闲着没事去找事情做,那么这就需要使用到SOA(面向服务的架构).那么我们在写一个项目使用分布式的时候,会有很多系统来相互调用,如果来回调用会使代码结构非常混乱.这里我们使用dubbo来管理,解决发布服务太多,搞不清楚的问题.

什么是SOA

SOA是一种设计方法,其中包含多个服务,而服务之间通过配合最终会提供一系列功能。一个服务通常以独立的形式存在于操作系统进程中。服务之间通过网络调用,而非采用进程内调用的方式进行通信。

比如你现在有很多服务:新闻服务(提供新闻的发布,查看,修改,删除),订单服务(订单添加,订单修改,订单查看,订单删除等)财务服务(收入,支出,统计等等)员工服务(新增,修改,查看,统计)考勤服务(签到,签退,导出,统计等)销售服务(卖出上报,销售统计。)

dubbo

什么是dubbo(是资源调度和治理中心的管理工具)

随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进。

 

单一应用架构

  1.  当网站流量很小时,只需一个应用,将所有功能都部署在一起,以减少部署节点和成本。
  2. 此时,用于简化增删改查工作量的 数据访问框架(ORM) 是关键。

垂直应用架构

  1.  当访问量逐渐增大,单一应用增加机器带来的加速度越来越小,将应用拆成互不相干的几个应用,以提升效率。
  2. 此时,用于加速前端页面开发的 Web框架(MVC) 是关键。

分布式服务架构

  1.  当垂直应用越来越多,应用之间交互不可避免,将核心业务抽取出来,作为独立的服务,逐渐形成稳定的服务中心,使前端应用能更快速的响应多变的市场需求。
  2. 此时,用于提高业务复用及整合的 分布式服务框架(RPC) 是关键。

流动计算架构

  1.  当服务越来越多,容量的评估,小服务资源的浪费等问题逐渐显现,此时需增加一个调度中心基于访问压力实时管理集群容量,提高集群利用率。
  2. 此时,用于提高机器利用率的 资源调度和治理中心(SOA) 是关键。

Dubbo环境的搭建:

节点角色说明:

  1.  Provider: 暴露服务的服务提供方。
  2. Consumer: 调用远程服务的服务消费方。
  3. Registry: 服务注册与发现的注册中心。
  4. Monitor: 统计服务的调用次数和调用时间的监控中心。
  5. Container: 服务运行容器。

调用关系说明:

  1. 服务容器负责启动,加载,运行服务提供者。
  2. 服务提供者在启动时,向注册中心注册自己提供的服务。
  3. 服务消费者在启动时,向注册中心订阅自己所需的服务。
  4. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
  5. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

这里我们主要来部注册中心(Registry),我们使用Zookeeper来充当注册中心.

在linux环境下部署注册中心,Zookeeper

第一步当然是开虚拟机啦,我还是在CentOS7中来搞.

上网上搞一个Zookeeper的压缩包,我的是zookeeper-3.4.6.tar.gz

粘贴到/opt目录下,解压.(需要jdk环境,如果没有jdk环境先安装一个jdk)

进入zookeeper-3.4.6目录,创建一个叫data的文件夹。

把./conf目录下的zoo_sample.cfg改名为zoo.cfg 修改zoo.cfg中的data属性:dataDir=/opt/zookeeper-3.4.6/data

第七步:

  1. 启动zookeeper.
  2. 启动:./zkServer.sh start
  3. 关闭:./zkServer.sh stop
  4. 查看状态:./zkServer.sh status

注意zookeeper启动后一定要将防火墙关闭!!!这样就搞定啦.

在service的applicationContext-service.xml中添加配置文件进行发布服务

?

1

2

3

4

5

6

7

8

9

10

11

12

<!-- 使用dubbo发布服务 -->

 <!-- 指明服务所在的工程 -->

 <dubbo:application name="sping-manager"/>

  

 <!-- 指明注册中心 adress地址是linux中的ip地址加上端口号,zookeeper的默认端口号是2181 -->

 <dubbo:registry protocol="zookeeper" address="10.0.117.198:2181" ></dubbo:registry>

   

 <!-- 把服务暴露在某个端口 port是端口号,选择一个没有被占用的端口 -->

 <dubbo:protocol name="dubbo" port="20888"></dubbo:protocol>

  

 <!-- 发布服务,ref是Spring容器创建的Service对象的名称 -->            

 <dubbo:service interface="com.itqf.service.ItemService" ref="itemServiceImpl" timeout="6000000"></dubbo:service>

在service的pom.xml中导入包

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

<!-- 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>

4 创建一个sping-manager-controller,与聚合项目平级.导入依赖.

contoller需要使用dubbo来访问service层发布的服务.要使用Tomcat服务器进行发布,当然还需要用到springmvc.

xml

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

<dependencies>

 <!-- 只需依赖业务的接口 -->

 <dependency>

  <groupId>com.qianfeng</groupId>

  <artifactId>sping-manager-interface</artifactId>

  <version>0.0.1-SNAPSHOT</version>

 </dependency>

  

 <!-- Spring -->

           <dependency>

               <groupId>org.springframework</groupId>

               <artifactId>spring-context</artifactId>

               <version>${spring.version}</version>

           </dependency>

           <dependency>

               <groupId>org.springframework</groupId>

               <artifactId>spring-beans</artifactId>

               <version>${spring.version}</version>

           </dependency>

           <dependency>

               <groupId>org.springframework</groupId>

               <artifactId>spring-webmvc</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-aspects</artifactId>

               <version>${spring.version}</version>

           </dependency>

           <dependency>

               <groupId>org.springframework</groupId>

               <artifactId>spring-jms</artifactId>

               <version>${spring.version}</version>

           </dependency>

           <dependency>

               <groupId>org.springframework</groupId>

               <artifactId>spring-context-support</artifactId>

               <version>${spring.version}</version>

           </dependency>

  

 <!-- JSP相关 -->

           <dependency>

               <groupId>jstl</groupId>

               <artifactId>jstl</artifactId>

                

           </dependency>

           <dependency>

               <groupId>javax.servlet</groupId>

               <artifactId>servlet-api</artifactId>

                

               <scope>provided</scope>

           </dependency>

           <dependency>

               <groupId>javax.servlet</groupId>

               <artifactId>jsp-api</artifactId>

               <scope>provided</scope>

           </dependency>

            

       <!-- 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>

  

</dependencies>

<build>

       <plugins>

           <plugin>

               <groupId>org.apache.tomcat.maven</groupId>

               <artifactId>tomcat7-maven-plugin</artifactId>

               <configuration>

                   <port>8081</port>

                   <path>/</path>

               </configuration>

           </plugin>

       </plugins>

</build>

spingmvc.xml

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

<?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:tx="http://www.springframework.org/schema/tx"

    xmlns:aop="http://www.springframework.org/schema/aop"

    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://www.springframework.org/schema/tx

  http://www.springframework.org/schema/tx/spring-tx-4.2.xsd

  http://www.springframework.org/schema/aop

  http://www.springframework.org/schema/aop/spring-aop-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.itqf.controller"></context:component-scan>

   

  <mvc:annotation-driven></mvc:annotation-driven>

   

  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

   <property name="prefix" value="/WEB-INF/jsp/"></property>

   <property name="suffix" value=".jsp"></property>

  </bean>

   

  <!--指明所在工程 -->

  <dubbo:application name="sping-manager-controller"/>

   

  <!-- 指明注册中心 -->

  <dubbo:registry protocol="zookeeper" address="10.0.117.198:2181"></dubbo:registry>

   

  <!--调用服务 -->

  <dubbo:reference interface="com.itqf.service.ItemService" id="itemService"></dubbo:reference>

   

</beans>

结构图:

5 创建sping-common

这个是我需要用到的一个专门放工具的地方,这里用来放一些公共需要的东西; pom.xml

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

 <modelVersion>4.0.0</modelVersion>

 <parent>

 <groupId>com.itqf</groupId>

 <artifactId>sping-parent</artifactId>

 <version>0.0.1-SNAPSHOT</version>

 </parent>

 <groupId>com.itqf</groupId>

 <artifactId>sping-common</artifactId>

 <version>0.0.1-SNAPSHOT</version>

  

 <dependencies>

            <!-- 时间操作组件 -->

            <dependency>

                <groupId>joda-time</groupId>

                <artifactId>joda-time</artifactId>

                <version>${joda-time.version}</version>

            </dependency>

            <!-- Apache工具组件 -->

            <dependency>

                <groupId>org.apache.commons</groupId>

                <artifactId>commons-lang3</artifactId>

                <version>${commons-lang3.version}</version>

            </dependency>

            <dependency>

                <groupId>org.apache.commons</groupId>

                <artifactId>commons-io</artifactId>

                <version>${commons-io.version}</version>

            </dependency>

            <dependency>

                <groupId>commons-net</groupId>

                <artifactId>commons-net</artifactId>

                <version>${commons-net.version}</version>

            </dependency>

            <!-- Jackson Json处理工具包 -->

            <dependency>

                <groupId>com.fasterxml.jackson.core</groupId>

                <artifactId>jackson-databind</artifactId>

                <version>${jackson.version}</version>

            </dependency>

    </dependencies>

  

</project>

6 测试

好啦,这样一个伪分布式项目就搭建好了,接下来非常重要的一点就是需要将父工程,sping-manager的聚合工程,sping-common都install一下放到本地仓库中,否则的话在启动项目的时候会报错说找不到父工程或其他工程.

最终工程图:

结束。

原文链接:https://juejin.im/post/5ad85368f265da50472fc95a

猜你喜欢

转载自blog.csdn.net/xiaomojun/article/details/87949021
今日推荐