idea搭建dubbo+zookeeper+springMVC+mybatis的分布式开发环境

 

1、环境准备

操作系统:win10

开发工具:IntelliJ IDEA

数据库:MySQL

底层工具:dubbo admin(没有也完全可以) + zookeeper

框架:springMVC+mybatis

工程:maven

2、工程搭建后的结构图

开始搭建前,先看一下搭建好后的结构

idea搭建dubbo+zookeeper+springMVC+mybatis的分布式开发环境_第1张图片

其中:tea-api模块:接口和实体类的信息,也是暴露给服务调用者的模块

 tea-service模块下的2个子模块:basic-service和biz-server,是服务提供者,basic是提供数据库层的服务,biz提供其他的,比如redis 的调用等,示例中,只是写了一段输出

tea-web模块下的crm-web模块,是服务调用者,对外展示的通道(crm嘛,当然就能想到后台管理系统了)

3、搭建前的准备

3.1、安装zookeeper

   3.1.1、下载zookeeper

直接官网下载:https://www.apache.org/dyn/closer.cgi/zookeeper/

3.1.2:修改zk 的配置文件

下载并且解压后,进入zk 的conf目录,将zoo_sample.cfg文件复制并命名为zoo.cfg,修改其中的配置:idea搭建dubbo+zookeeper+springMVC+mybatis的分布式开发环境_第2张图片

如图中标红的,一个是zk 的节点存放目录,clientPort是zk 的端口

3.1.3:启动zk服务

改完后,保存,进入bin目录,执行zkServer.cmd文件,启动zk服务

idea搭建dubbo+zookeeper+springMVC+mybatis的分布式开发环境_第3张图片

3.2:其他的准备

安装maven和idea,这些部分就不一一介绍了,度娘一下就好

4、搭建环境

4.1、新建project

idea和eclipse 的不同点其中一个就是project+module,idea下新建project需要新打开窗口(一个窗口=一个项目),但是project下面可以new module

因为是一个总的父类工程,所以这个下面,核心的就是一个pom文件,如下




  4.0.0

  cn.chuanyi
  tea
  1.0-SNAPSHOT
  pom

  tea

  

    tea-api
    tea-service
    tea-web
  

  
  
    UTF-8
    
    4.0.2.RELEASE
    
    1.7.7
    1.2.17
    3.3.2
    3.4.12
    0.10
    3.8.1
    3.2.2
    5
    1.3.0
    3.17.1-GA
    2.2.2
    0.9.1.2
    12.1.0.1-atlassian-hosted
    5.1.30
    1.2.2
    1.2
    1.1.41
    1.9.13
    1.3.1
    2.4
    1.9
    2.9.0
    1.6.6.RELEASE
    1.0.0
    1.0.18
    RELEASE
    2.2.3
    2.5.7
    2.8.8
    3.9.5.Final
    4.1.16.Final
    3.1
  

  
    
      site
      project website
      scp://local.company.com/websites/project.company.com/
    
  


  
    
      
        cn.chuanyi
        logging
        1.0-SNAPSHOT
      
      
        cn.chuanyi
        primary-source
        1.0-SNAPSHOT
      
      
        cn.chuanyi
        servlet
        1.0-SNAPSHOT
        war
      
      
        cn.chuanyi
        ejbs
        1.0-SNAPSHOT
        ejb
      
      
        junit
        junit
        ${junit.version}
        test
      
      
        javaee
        javaee-api
        ${javaee.api.version}
      

      
      
        org.springframework
        spring-core
        ${spring.version}
      

      
        org.springframework
        spring-web
        ${spring.version}
      
      
        org.springframework
        spring-oxm
        ${spring.version}
      
      
        org.springframework
        spring-tx
        ${spring.version}
      

      
        org.springframework
        spring-jdbc
        ${spring.version}
      

      
        org.springframework
        spring-webmvc
        ${spring.version}
      
      
        org.springframework
        spring-aop
        ${spring.version}
      

      
        org.springframework
        spring-context-support
        ${spring.version}
      
      
        org.springframework
        spring-beans
        ${spring.version}
      
      
        org.springframework
        spring-context
        ${spring.version}
      


      
        org.springframework
        spring-test
        ${spring.version}
      

      
        org.springframework
        spring-expression
        ${spring.version}
      

      
        org.mybatis
        mybatis
        ${mybatis.version}
      

      
        org.mybatis
        mybatis-spring
        ${mybatis.spring.version}
      

      
        org.javassist
        javassist
        ${javassist.version}
      

      
        cglib
        cglib
        ${cglib.version}
      

      
        c3p0
        c3p0
        ${c3p0.version}
      

      
      
        com.oracle
        ojdbc6
        ${ojdbc.version}
        test
      

      
      
        mysql
        mysql-connector-java
        ${mysql.connector.java.version}
      
      
      
        commons-dbcp
        commons-dbcp
        ${commons.dbcp.version}
      
      
      
        jstl
        jstl
        ${jstl.version}
      
      
      
      
        log4j
        log4j
        ${log4j.version}
      


      
      
        com.alibaba
        fastjson
        ${fastjson.version}
      


      
        org.slf4j
        slf4j-api
        ${slf4j.version}
      

      
        org.slf4j
        slf4j-log4j12
        ${slf4j.version}
      
      
      
      
        org.codehaus.jackson
        jackson-mapper-asl
        ${jackson.mapper.asl.version}
      
      
      
        commons-fileupload
        commons-fileupload
        ${commons.fileupload.version}
      
      
        commons-io
        commons-io
        ${commons.io.version}
      
      
        commons-codec
        commons-codec
        ${commons.codec.version}
      

      
        redis.clients
        jedis
        ${jedis.version}
      

      
      
        org.springframework.data
        spring-data-redis
        ${spring.data.redis.version}
      
      
        io.netty
        netty
        ${netty.version}
      
      
        io.netty
        netty-all
        ${netty-all.version}
      

      
      
        org.mybatis
        mybatis-ehcache
        ${mybatis.ehcache.version}
      
      
        cglib
        cglib
        3.2.5
      

      
      
        com.alibaba
        druid
        ${druid.version}
      
      
        org.junit.jupiter
        junit-jupiter-api
        ${junit.jupiter.api.version}
      
      
      
      
        org.quartz-scheduler
        quartz
        ${quartz.version}
      
      
      
        org.apache.commons
        commons-lang3
        ${commons-lang3.version}
      
      
      
      
        org.apache.zookeeper
        zookeeper
        ${zookeeper.version}
      
      
      
      
        com.101tec
        zkclient
        ${zkclient.version}
      
      
      
        com.fasterxml.jackson.core
        jackson-core
        ${jackson-core.version}
      
      
        com.alibaba
        dubbo
        ${dubbo.version}
        
          
            sprincg
            org.springframework
          
          
            httpclient
            org.apache.httpcomponents
          
          
            httpcore
            org.apache.httpcomponents
          
          
            javax.servlet
            javax.servlet-service
          
          
            spring-web
            org.springframework
          
          
            spring-context
            org.springframework
          
          
            spring-aop
            org.springframework
          
          
            spring-expression
            org.springframework
          
          
            spring-core
            org.springframework
          
          
            spring-beans
            org.springframework
          
          
            commons-logging
            commons-logging
          
          
            slf4j-api
            org.slf4j
          

          
            org.jboss.netty
            netty
          
        
      
      
        commons-httpclient
        commons-httpclient
        ${commons.httpclient.version}
      
    

  

  
    
      
        
          maven-clean-plugin
          3.0.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.7.0
        
        
          maven-surefire-plugin
          2.20.1
        
        
          maven-jar-plugin
          3.0.2
        
        
          maven-war-plugin
          3.2.0
        
        
          maven-ear-plugin
          2.10.1
        
        
          maven-ejb-plugin
          3.0.0
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
        
          maven-javadoc-plugin
          3.0.0
        
      
    
  

4.2、新建模块api

api模块下开发实体类和接口,这也是对服务消费者直接暴露,因为这个对外只是提供jar包,直接展示个模块的结构好了

idea搭建dubbo+zookeeper+springMVC+mybatis的分布式开发环境_第4张图片

4.3、新建服务提供者模块

这个模块下面暂时提供了两个模块:basic和biz,分别对应数据库服务和其他(还没想好具体放哪些服务)

4.3.1、搭建basic服务者模块

idea搭建dubbo+zookeeper+springMVC+mybatis的分布式开发环境_第5张图片

basicService下面,写接口的实现类;mapper写的是mybatis的dao层接口,resources下面的conf存放配置文件,mapper文件下存放的mybatis的映射文件,META-INF/spring下面存放的就是spring和dubbo的xml配置文件

idea搭建dubbo+zookeeper+springMVC+mybatis的分布式开发环境_第6张图片

dubbo.properties配置文件内容:

##################### \u751f\u4ea7\u73af\u5883\u7528 ######################
#dubbo.container=spring,log4j
dubbo.application.name=basic-service-provider
dubbo.application.owner=chuanyi
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=48321
dubbo.log4j.file=/chuanyi/logs/basic-service/dubbo.log
#dubbo.log4j.level=INFO

 

jdbc.properties配置文件内容:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/Tea?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=!QAZ2wsx
#\u5B9A\u4E49\u521D\u59CB\u8FDE\u63A5\u6570
jdbc.initialSize=0
#\u5B9A\u4E49\u6700\u5927\u8FDE\u63A5\u6570
jdbc.maxActive=20
#\u5B9A\u4E49\u6700\u5927\u7A7A\u95F2
jdbc.maxIdle=20
#\u5B9A\u4E49\u6700\u5C0F\u7A7A\u95F2
jdbc.minIdle=1
#\u5B9A\u4E49\u6700\u957F\u7B49\u5F85\u65F6\u95F4
jdbc.maxWait=60000
jdbc.testSql=SELECT 1 FROM dual

log4j.properties配置文件内容:

#log4j.rootLogger = [ level ] , appenderName, appenderName, ...
log4j.rootLogger = DEBUG, console, R
#level=INFO,all can be output
#console is set to be a ConsoleAppender
log4j.appender.console = org.apache.log4j.ConsoleAppender
#console have four patterns
#org.apache.log4j.HTMLLayout
#org.apache.log4j.PatternLayout
#org.apache.log4j.SimpleLayout
#org.apache.log4j.TTCCLayout
log4j.appender.console.layout = org.apache.log4j.PatternLayout
#define the output type
log4j.appender.console.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n
#file is set to output to a extra file
log4j.appender.R = org.apache.log4j.DailyRollingFileAppender
#the absolute route of the log4j file
#/root/log/log.txt
#j:/log/log.txt
log4j.appender.R.File =d:/whiteTea/basicServer/log.txt
log4j.appender.R.DatePattern = '.'yyyy-MM-dd 
#the size
log4j.appender.R.MaxFileSize = 500KB
#back up a file
log4j.appender.R.MaxBackupIndex = 1
log4j.appender.R.layout = org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] - %m%n
log4j.logger.org.hibernate=debug
###????SQL??????·?
log4j.logger.com.ibatis=DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
log4j.logger.Java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

mybatis-config.xml:





	
	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		  
		
		
		
		
		
		
		
		
		
        
		
        
        

		
		

		
	

	

spring-context-service.xml:



    
    
        
            
                classpath:jdbc.properties
                classpath:dubbo.properties
            
        
    

    
    
    

    
    
        
        

        
        
        
        

        
        
        
        

        
        

        
        

        
        

        
        
        
        

        

        
        
    

    
    
        
        
    

    
    
        
        
        
        
        
    

    
    
        
    

    
    

spring-context-dubbo.xml配置的文件内容:




    
    
    
    
    
    

4.3.2、编码开始

首先,需要在basic模块下写数据库的dao层接口和对应的mapper文件

package com.cn.chuanyi.mapper.sysUser;

import com.cn.chuanyi.entity.basic.sysUser.SysUserEntity;

import java.util.List;

/**
 * Created by guxuhua on 2018/7/31.
 */
public interface SysUserDao {
    /**
     * 查询系统中所有的sysuser
     *
     * */
    List getSysUsers() throws Exception;
}



    
        
        
        
        
    

    


接着在api模块下,新写一个service接口:

package com.cn.chuanyi.service.basic.sysUser;

import com.cn.chuanyi.entity.basic.sysUser.SysUserEntity;

import java.util.List;

/**
 * Created by guxuh on 2018/7/31.
 */
public interface SysUserService {
    /**
     * 查询sysuser信息
     * SELECT * FROM sys_user;
     */
    List getSysUsers() throws Exception;
}

然后,在basic模块下,实现这个service 接口

package com.cn.chuanyi.basicService.sysUser;

import com.cn.chuanyi.entity.basic.sysUser.SysUserEntity;
import com.cn.chuanyi.mapper.sysUser.SysUserDao;
import com.cn.chuanyi.service.basic.sysUser.SysUserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by guxuh on 2018/7/31.
 */
@Service
public class SysUserServiceImpl implements SysUserService {
    /*log4j的输出*/
    private static final Logger logger = Logger.getLogger(SysUserServiceImpl.class);
    @Autowired
    private SysUserDao sysUserDao;//操作数据库

    /**
     * 查询sysuser信息
     * SELECT * FROM sys_user;
     */
    @Override
    public List getSysUsers() throws Exception {
        logger.debug(">>>>>查询所有的sys用户<<<<<");
        return sysUserDao.getSysUsers();
    }
}

最后配置在spring-context-dubbo.xml这个文件里头(配置对外提供服务的service)




    
    
    
    
    
    
,这部分,就是注册一个服务到dubbo,好了,basic这部分的服务提供者的代码和配置文件完成了,启动下看下

4.3.3:启动服务

前提:zk已经启动了

dubbo服务的启动可以使用dubbo提供的container进行启动,我这里就是用的container启动的,不过,对于通过container启动,需要注意:spring和dubbo对应的xml配置文件,必须放在META-INF/spring下面,idea搭建dubbo+zookeeper+springMVC+mybatis的分布式开发环境_第7张图片

 

idea搭建dubbo+zookeeper+springMVC+mybatis的分布式开发环境_第8张图片

有兴趣的同学,可以读下dubbo的这块源码,

好了,接着,就是以main方法的形式启动服务提供者,如下结果,表示启动成功

idea搭建dubbo+zookeeper+springMVC+mybatis的分布式开发环境_第9张图片

启动的同时,zk 的cmd窗口也会有消息滑动。

4.4、新建服务消费者模块(crm模块)

结构如下图所示:

idea搭建dubbo+zookeeper+springMVC+mybatis的分布式开发环境_第10张图片

4.4.1、配置内容

dubbo.properties


dubbo.container=log4j,spring
dubbo.application.name=chuanyi_crm
dubbo.application.owner=com.cn.chuanyi.crmWeb
dubbo.registry.address=zookeeper://127.0.0.1:2181
#dubbo.registry.address=zookeeper://10.191.24.225:48347?backup=10.191.24.226:48347,10.191.24.227:48347
#/usr/local/redis/bin/redis-trib.rb create --replicas 1 192.168.0.104:6379 192.168.0.104:6389 192.168.0.104:6399 192.168.0.104:7379 192.168.0.104:7389 192.168.0.104:7399
dubbo.protocol.port=20000
dubbo.registry.check = false
#dubbo.registry.address=zookeeper://10.180.211.45:2181
#dubbo.registry.address=zookeeper://10.191.131.97:50032
#dubbo.registry.address=redis://127.0.0.1:6379
#dubbo.registry.address=dubbo://127.0.0.1:9090
dubbo.protocol.name=dubbo
#dubbo.protocol.port=20002
#dubbo.log4j.file=logs/ecs_goods_service.log
dubbo.log4j.level=DEBUG
dubbo.provider.timeout=15000
dubbo.registry.timeout=15000

ecsite.properties

# call dubbo services configuration
dubbo.container=spring,log4j
dubbo.application.name=dubbo-service
dubbo.application.owner=chuanyi
dubbo.registry.address=zookeeper://127.0.0.1:2181
session.sessionTimeout=1800000
session.sessionTimeoutClean=120000
redis.keyPrefix=h5


web.view.prefix=view
web.view.suffix=jsp

log4j.properties

#log4j.rootLogger = [ level ] , appenderName, appenderName, ...
log4j.rootLogger = DEBUG, console, R
#level=INFO,all can be output
#console is set to be a ConsoleAppender
log4j.appender.console = org.apache.log4j.ConsoleAppender
#console have four patterns
#org.apache.log4j.HTMLLayout
#org.apache.log4j.PatternLayout
#org.apache.log4j.SimpleLayout
#org.apache.log4j.TTCCLayout
log4j.appender.console.layout = org.apache.log4j.PatternLayout
#define the output type
log4j.appender.console.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n
#file is set to output to a extra file
log4j.appender.R = org.apache.log4j.DailyRollingFileAppender
#the absolute route of the log4j file
#/root/log/log.txt
#j:/log/log.txt
log4j.appender.R.File =d:/whiteTea/crm/log.txt
log4j.appender.R.DatePattern = '.'yyyy-MM-dd 
#the size
log4j.appender.R.MaxFileSize = 500KB
#back up a file
log4j.appender.R.MaxBackupIndex = 1
log4j.appender.R.layout = org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] - %m%n
log4j.logger.org.hibernate=debug
###显示SQL语句部分
log4j.logger.com.ibatis=DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
log4j.logger.Java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

spring-context-dubbo.xml:需要调用的服务




    
    
    
        
    
    
    
    
    
    

spring-context-web.xml



    
   


    
    
        
    

springmvc-servlet.xml:spring的核心配置文件



    
    

    
    
        
    

    
    
    
        
            
                
                    
                        
                            text/html; charset=UTF-8
                            application/json;charset=UTF-8
                        
                    
                
                
                    
                        
                            text/html; charset=UTF-8
                            application/json;charset=UTF-8
                        
                    
                
            
        
    

    
    

    
    
        
        
        
    

    
    
    
    
    

    
    
    
    
    
    
    
    



    
    
        
        
        
        
        
        
    

最后,作为web工程,就必须使用web.xml了




  app-crm
  
  
    springmvc
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:META-INF/spring/springmvc-servlet.xml
    
    1
  

  
  
    springmvc
    /
  

  
  
    org.springframework.web.context.ContextLoaderListener
  

  
  
    contextConfigLocation
    classpath*:META-INF/spring/spring-context-*.xml
  


  
  
    encodeFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      UTF-8
    
    
      forceEncoding
      true
    
  

  
  
    encodeFilter
    /*
  
  
    400
    /error.jsp
  
  
    401
    /error.jsp
  
  
    403
    /error.jsp
  
  
    404
    /error.jsp
  
  
    405
    /error.jsp
  
  
    412
    /error.jsp
  
  
    413
    /error.jsp
  
  
    500
    /error.jsp
  

4.4.2、编写测试用代码

package com.cn.chuanyi.view.sysUser;

import com.alibaba.fastjson.JSONObject;
import com.cn.chuanyi.entity.basic.sysUser.SysUserEntity;
import com.cn.chuanyi.service.basic.sysUser.SysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by guxuh on 2018/7/31.
 * 操作系统管理员数据
 */
@Controller
@RequestMapping("/sysUser")
public class SysUserController {
    /*log4j输出*/
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    /*注入待操作的服务*/
    @Autowired
    private SysUserService sysUserService;

    /**
     * 获取所有的sysuser
     * */
    @RequestMapping("getAllSysUsers")
    @ResponseBody
    public JSONObject getAllSysUsers() throws Exception{
        JSONObject returnValues = new JSONObject();
        logger.debug(">>>>>查询所有的sys用户<<<<<");
        List entities = sysUserService.getSysUsers();
        if(null!=entities && !entities.isEmpty()){
            returnValues.put("count",entities.size());
        }else{
            returnValues.put("count",0);
        }
        returnValues.put("sysUsers",entities);
        return returnValues;
    }
}

4.4.3、启动web应用

idea搭建dubbo+zookeeper+springMVC+mybatis的分布式开发环境_第11张图片

如图,就表示启动成功了

5、测试

直接浏览器输入:http://localhost:8181/crm/sysUser/getAllSysUsers

就可以看到获取到的数据了

如上,就是一个基础的dubbo环境的搭建,在应用搭建和启动的过程中出现的问题,很多场景下通过分析日志就可以解决,要是还是不行,还有万能的度娘。

你可能感兴趣的:(java)