java实现分布式项目搭建

1 分布式

1.1 什么是分布式

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

1.2 分布式与集群的区别
集群是同一个系统部在不同的服务器上,例如一个登陆系统部在不同的服务器上.
分布式是不同的系统部在不同的服务器上,服务器之间相互调用.
小饭店原来只有一个厨师,切菜洗菜备料炒菜全干。后来客人多了,厨房一个厨师忙不过来,又请了个厨师,两个厨师都能炒一样的菜,这两个厨师的关系是集群。为了让厨师专心炒菜,把菜做到极致,又请了个配菜师负责切菜,备菜,备料,厨师和配菜师的关系是分布式,一个配菜师也忙不过来了,又请了个配菜师,两个配菜师关系是集:更多Java教程可以关注小编+转发文章+私信【架构资料】获取,最后祝大家都能拿到自己心仪的offer**

2 搭建分布式项目
准备工具:eclipse,装有CentOS7系统的VMwarm,zookeeper.......最重要的,一台三年高龄的老人机.

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


4.0.0
com.itqf
sping-parent
0.0.1-SNAPSHOT
pom

  
    4.12
    4.2.4.RELEASE
    3.2.8
    1.2.2
    1.2.15
    5.1.32
    1.6.4
    2.4.2
    1.0.9
    4.3.5
    1.2
    2.5
    2.0
    2.5
    3.3.2
    1.3.2
    3.3
    3.4.2-fix
    0.9.1
    1.3.1
    2.7.2
    4.10.3
    2.5.3
    3.4.7
    0.1
    5.11.2
    2.3.23
    2.2.2
  
  
  
      
        
        
            joda-time

复制代码
2 创建一个maven的聚合工程.
2.1 创建maven聚合工程,继承父工程.

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

pom.xml


4.0.0

com.itqf
sping-parent
0.0.1-SNAPSHOT


com.itqf
sping-manager
0.0.1-SNAPSHOT
pom

  
      com.itqf
      sping-common
      0.0.1-SNAPSHOT
  

    
        
            org.apache.tomcat.maven
            tomcat7-maven-plugin
            
                8083
                /
            
        
    

  sping-manager-pojo
  sping-manager-interface
  sping-manager-service
  sping-manager-mapper



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

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

4.0.0

com.itqf
sping-manager
0.0.1-SNAPSHOT


sping-manager-pojo

复制代码
2.3 在聚合工程中创建maven Module,命名sping-manager-mapper(dao层).

在pom.xml文件中依赖pojo.因为mapper层的方法返回的是一个实体类对象的话,那么需要用到pojo.
导入依赖jar包.
xml

4.0.0

com.itqf
sping-manager
0.0.1-SNAPSHOT


sping-manager-mapper


  
      com.itqf
      sping-manager-pojo
      0.0.1-SNAPSHOT
  
  
  
        
            org.mybatis
            mybatis
            ${mybatis.version}
        
        
            org.mybatis
            mybatis-spring
            ${mybatis.spring.version}
        
        
            com.github.miemiedev
            mybatis-paginator
            ${mybatis.paginator.version}
        
        
            com.github.pagehelper
            pagehelper
            ${pagehelper.version}
        
        
        
            mysql
            mysql-connector-java
            ${mysql.version}
        
        
        
            com.alibaba
            druid
            ${druid.version}
        
  



复制代码
2.4 在聚合工程中创建sping-manager-interface(接口),将所有的service接口都放到独立的工程当中.

接口中方法返回值如果是实体类,需要用到pojo.所以在pom中依赖pojo xml

4.0.0

com.itqf
sping-manager
0.0.1-SNAPSHOT


sping-manager-interface

  
      com.itqf
      sping-manager-pojo
      0.0.1-SNAPSHOT
  


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

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

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


    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">



复制代码
db.properties

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


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

   

  
       
       
       
       
       
       
       
  
  

  


复制代码
applicationContext-dao.xml


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

    
    
    
    
    
    

   
   

    


复制代码
applicationContext-service.xml


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

   

复制代码
pom.xml


4.0.0

com.qianfeng
sping-manager
0.0.1-SNAPSHOT


sping-manager-service

war

   
       com.qianfeng
       sping-manager-interface
       0.0.1-SNAPSHOT
   
   
   
       com.qianfeng
       sping-manager-mapper
       0.0.1-SNAPSHOT
   
 
   
        
            org.springframework
            spring-context
            ${spring.version}
        
        
            org.springframework
            spring-beans
            ${spring.version}
        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        
        
            org.springframework
            spring-jdbc
            ${spring.version}
        
        
            org.springframework
            spring-aspects
            ${spring.version}
        
        
            org.springframework
            spring-jms
            ${spring.version}
        
        
            org.springframework
            spring-context-support
            ${spring.version}
        
     


复制代码
最后工程结构

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

什么是SOA

SOA是一种设计方法,其中包含多个服务,而服务之间通过配合最终会提供一系列功能。一个服务通常以独立的形式存在于操作系统进程中。服务之间通过网络调用,而非采用进程内调用的方式进行通信。
比如你现在有很多服务:新闻服务(提供新闻的发布,查看,修改,删除),订单服务(订单添加,订单修改,订单查看,订单删除等)财务服务(收入,支出,统计等等)员工服务(新增,修改,查看,统计)考勤服务(签到,签退,导出,统计等)销售服务(卖出上报,销售统计。)

SOA的优缺点

dubbo
什么是dubbo(是资源调度和治理中心的管理工具)
随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进。

单一应用架构
当网站流量很小时,只需一个应用,将所有功能都部署在一起,以减少部署节点和成本。
此时,用于简化增删改查工作量的 数据访问框架(ORM) 是关键。
垂直应用架构
当访问量逐渐增大,单一应用增加机器带来的加速度越来越小,将应用拆成互不相干的几个应用,以提升效率。
此时,用于加速前端页面开发的 Web框架(MVC) 是关键。
分布式服务架构
当垂直应用越来越多,应用之间交互不可避免,将核心业务抽取出来,作为独立的服务,逐渐形成稳定的服务中心,使前端应用能更快速的响应多变的市场需求。
此时,用于提高业务复用及整合的 分布式服务框架(RPC) 是关键。
流动计算架构
当服务越来越多,容量的评估,小服务资源的浪费等问题逐渐显现,此时需增加一个调度中心基于访问压力实时管理集群容量,提高集群利用率。
此时,用于提高机器利用率的 资源调度和治理中心(SOA) 是关键。 Dubbo环境的搭建:

节点角色说明:

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

服务容器负责启动,加载,运行服务提供者。
服务提供者在启动时,向注册中心注册自己提供的服务。
服务消费者在启动时,向注册中心订阅自己所需的服务。
注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
这里我们主要来部注册中心(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
第七步:启动zookeeper.
启动:./zkServer.sh start
关闭:./zkServer.sh stop
查看状态:./zkServer.sh status
注意zookeeper启动后一定要将防火墙关闭!!!这样就搞定啦.

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

  




    
  
  

                 

复制代码
在service的pom.xml中导入包

    
        com.alibaba
        dubbo
        
        
            
                org.springframework
                spring
            
            
                org.jboss.netty
                netty
            
        
    
    
        org.apache.zookeeper
        zookeeper
    
    
        com.github.sgroschupf
        zkclient
    

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

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

  
  
      com.qianfeng
      sping-manager-interface
      0.0.1-SNAPSHOT
  
  
  
        
            org.springframework
            spring-context
            ${spring.version}
        
        
            org.springframework
            spring-beans
            ${spring.version}
        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        
        
            org.springframework
            spring-jdbc
            ${spring.version}
        
        
            org.springframework
            spring-aspects
            ${spring.version}
        
        
            org.springframework
            spring-jms
            ${spring.version}
        
        
            org.springframework
            spring-context-support
            ${spring.version}
        
  
  
        
            jstl
            jstl
            
        
        
            javax.servlet
            servlet-api
            
            provided
        
        
            javax.servlet
            jsp-api
            provided
        
        
    
    
        com.alibaba
        dubbo
        
        
            
                org.springframework
                spring
            
            
                org.jboss.netty
                netty
            
        
    
    
        org.apache.zookeeper
        zookeeper
    
    
        com.github.sgroschupf
        zkclient
    
 

    
        
            org.apache.tomcat.maven
            tomcat7-maven-plugin
            
                8081
                /
            
        
    


复制代码
spingmvc.xml


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


复制代码
5 创建sping-common
这个是我需要用到的一个专门放工具的地方,这里用来放一些公共需要的东西; pom.xml


4.0.0

com.itqf
sping-parent
0.0.1-SNAPSHOT


com.itqf
sping-common
0.0.1-SNAPSHOT

        
        
            joda-time
            joda-time
            ${joda-time.version}
        
        
        
            org.apache.commons
            commons-lang3
            ${commons-lang3.version}
        
        
            org.apache.commons
            commons-io
            ${commons-io.version}
        
        
            commons-net
            commons-net
            ${commons-net.version}
        
        
        
            com.fasterxml.jackson.core
            jackson-databind
            ${jackson.version}
        


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

总结:

经过几个小时的艰苦奋战终于搭建好了,都要给我的老人机累炸了.如果有什么错误或不足之处请不吝之处.

你可能感兴趣的:(java实现分布式项目搭建)