dubbo+zookeeper+spring+springMVC+mybatis

dubbo+zookeeper+spring+springMVC+mybatis的使用,dubbozookeeper


读前声明:由于本人水平有限,有错误或者描述不恰当的地方请指出来,勿喷!第一次写博客。

 源码下载链接:http://files.cnblogs.com/files/la-tiao-jun-blog/dubbo%E4%BE%8B%E5%AD%90.rar

使用条件:

     eclipse-jee-luna-SR2-win32-x86_64

     jdk1.7,Apache Tomcat v8.0,maven

     zookeeper3.4.6

     dubbo-2.4.10

     dubbo-monitor-simple-2.5.4-SNAPSHOT-assembly.tar.gz(dubbo简易监控中心)

服务提供者的工程目录(下左):                               服务消费者的工程目录(下右):

         

 

服务提供者的pom.xml:

banksteel-yesen:


  4.0.0

  bankmineral
  banksteel-yesen
   pom
  0.0.1-SNAPSHOT

  banksteel-yesen
  http://maven.apache.org

  
  
    ../banksteel-yesen-api
    ../banksteel-yesen-provider
  
  
  
      
         
           bankmineral
           banksteel-user-api
           
           ${maven.version}
           compile
        
        
           bankmineral
           banksteel-user-provider
           
           ${maven.version}
          
  
  
View Code

banksteel-yesen-api:


  4.0.0

  bankmineral
  banksteel-yesen-api
  0.0.1-SNAPSHOT
  jar

  banksteel-yesen-api
  http://maven.apache.org

  
    UTF-8
        
        4.0.2.RELEASE
        
        3.2.6
        
        1.7.7
        1.2.17
    
      
       bankmineral
       banksteel-yesen
        0.0.1-SNAPSHOT
        
       ../banksteel-yesen/pom.xml  
  
  
      
          junit
          junit
          3.8.1
          test
         
    
            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-test
            ${spring.version}
        
            
            org.aspectj
            aspectjrt
            1.6.12
        
        
            org.aspectj
            aspectjweaver
            1.6.12
            
           
                org.javassist  
                javassist  
                3.18.1-GA  
            
        
              org.jboss.netty
              netty
              3.2.5.Final
        
        
              org.apache.zookeeper
              zookeeper
              3.4.6
            
        
        
            org.mybatis
            mybatis
            ${mybatis.version}  
            
            
        
        
            log4j
            log4j
            ${log4j.version}
        
            
            org.slf4j
            slf4j-api
            ${slf4j.version}
        
        
            org.slf4j
            slf4j-log4j12
            ${slf4j.version}
        
              
  

View Code

banksteel-yesen-provider:


  4.0.0
  bankmineral
  banksteel-yesen-provider
  jar
  0.0.1-SNAPSHOT
  banksteel-yesen-provider Maven Webapp
  http://maven.apache.org
    
       bankmineral
       banksteel-yesen
        0.0.1-SNAPSHOT
        
       ../banksteel-yesen/pom.xml  
  

  
    
         bankmineral
          banksteel-yesen-api
           0.0.1-SNAPSHOT
     
   
        
        
            org.mybatis
            mybatis-spring
            1.2.2
        
        
        
            javax
            javaee-api
            7.0
        
        
        
            mysql
            mysql-connector-java
            5.1.30
        
        
        
            commons-dbcp
            commons-dbcp
            1.2.2
        
        
        
            jstl
            jstl
            1.2
        
    
        
        
            com.alibaba
            fastjson
            1.1.41
        

        
        
            org.codehaus.jackson
            jackson-mapper-asl
            1.9.13
        
        
        
            commons-fileupload
            commons-fileupload
            1.3.1
        
        
            commons-io
            commons-io
            2.4
        
        
            commons-codec
            commons-codec
            1.9
            
        
            com.alibaba
            dubbo
            2.4.10
        
        
            com.101tec
            zkclient
            0.4
    
  
  
          
            
                ${project.build.directory}/classes
                src/main/resources
                true
                
                **/*.xml
                *.properties
                
            
            
            
                ${project.build.directory}/classes/META-INF/spring
                src/main/resources/spring
                true
                
                    spring-mybatis.xml
                
            
        
        
        
            
                
                
                    org.eclipse.m2e
                    lifecycle-mapping
                    1.0.0
                    
                        
                            
                                
                                    
                                        org.apache.maven.plugins
                                        maven-dependency-plugin
                                        [2.0,)
                                        
                                            copy-dependencies
                                        
                                    
                                    
                                        
                                    
                                
                            
                        
                    
                
            
        
        
            
            
                org.apache.maven.plugins
                maven-jar-plugin
                
                    target/classes/
                    
                        
                            com.alibaba.dubbo.container.Main
                            
                            false
                            true
                            lib/
                        
                        
                            .
                        
                    
                
            
            
            
                org.apache.maven.plugins
                maven-dependency-plugin
                
                    
                        copy-dependencies
                        package
                        
                            copy-dependencies
                        
                        
                            jar
                            jar
                            false
                            
                                ${project.build.directory}/lib
                            
                        
                    
                
            
        
    
View Code

服务消费者的pom.xml

banksteel-user-app:


4.0.0
  bankmineral
  banksteel-userapp
  war
  0.0.1-SNAPSHOT
  banksteel-userapp Maven Webapp
  http://maven.apache.org

    
        bankmineral
        banksteel-yesen
        0.0.1-SNAPSHOT
    

  
    
      junit
      junit
      3.8.1
      test
    
    
        bankmineral
        banksteel-yesen-api
        0.0.1-SNAPSHOT
    

    
            com.alibaba
            dubbo
            2.4.10
        
        
            com.101tec
            zkclient
            0.4
    
    
        
            jstl
            jstl
            1.2
        
  
  
    banksteel-user-app
  
View Code

服务提供者的java代码:

-----------banksteel-yesen-api-------------

com.yesen.entity

package com.yesen.entity;

import java.io.Serializable;

public class User implements Serializable{
    private long id;
    private String username;
    private String password;
    private int age;
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}
View Code

com.yesen.service

package com.yesen.service;

import java.util.List;
import java.util.Map;

import com.yesen.entity.User;

public interface UserService {

    List getUserByMap(Map paramMap);

    void insertUser(User user);
}
View Code

--------banksteel-yesen-provider-------------

com.yesen.dao

package com.yesen.dao;

import java.util.List;
import java.util.Map;

import com.yesen.entity.User;

public interface UserDAO {

    List getUserByMap(Map paramMap);

    void insertUser(User user);
}
View Code

com.yesen.dao.impl

package com.yesen.dao.impl;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.yesen.dao.UserDAO;
import com.yesen.entity.User;

@Repository("userDAO")
public class UserDAOImpl implements UserDAO {

    private static final String INSERT = "com.yesen.model.userMapper.insertUser";

    private static final String LISTBY = "com.yesen.model.userMapper.listBy";

    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    public List getUserByMap(Map paramMap) {

        SqlSession session = sqlSessionTemplate.getSqlSessionFactory().openSession();

        return session.selectList(LISTBY, paramMap);
    }

    public void insertUser(User user) {

        SqlSession session = sqlSessionTemplate.getSqlSessionFactory().openSession();

        session.insert(INSERT, user);
    }
}
View Code

com.yesen.service.impl

package com.yesen.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yesen.dao.UserDAO;
import com.yesen.entity.User;
import com.yesen.service.UserService;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDAO userDAO;

    public List getUserByMap(Map paramMap) {
        
        return userDAO.getUserByMap(paramMap);
    }

    public void insertUser(User user) {
        
        userDAO.insertUser(user);
    }
}
View Code

服务提供者的其他配置:

目录结构只看src/main/resources

 

userMapper.xml:





user

    
        
        
        
        
    

    
        ID, USERNAME, PASSWORD, AGE
    

    
    
        INSERT INTO
        
        (USERNAME, PASSWORD, AGE)
        VALUES
        (#{username},#{password}, #{age})
    


    


    
        
            AND USERNAME = #{username}
        
        
            AND PASSWORD = #{password}
        
    
    
View Code

mybatis-config.xml:





    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    

    
        
    
View Code

dubbo-service.xml:





    
    
     
    
    
    

    

View Code

spring-mybatis.xml:





    
    
    
        
    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    

    
    
        
        
        
        
    

    
        
    

    
    
        
    
    
View Code

 服务消费者的java代码:

com.yesen.controller

package com.yesen.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yesen.entity.User;
import com.yesen.service.UserService;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/create")
    public String createUser(HttpServletRequest request, Model model){
        
        String username =request.getParameter("username");
        String password =request.getParameter("password");
        String age =request.getParameter("age");
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setAge(Integer.parseInt(age));
        userService.insertUser(user);
        model.addAttribute("message", user.getUsername());

        return "insertSuccess";
    }

    @RequestMapping(value = "/query")
    public String queryUser(String username, Model model,HttpServletRequest request){

        Map paramMap = new HashMap();
        
        List userList = userService.getUserByMap(paramMap);
        System.out.println("------------");
        for(User user: userList){
            System.out.println("姓名:"+user.getUsername()+"  ,密码:"+user.getPassword());
        }

        model.addAttribute(userList);

        return "userInfo";
    }
}
View Code

服务消费者的其他配置:

注:服务消费者工程目录中的dubbo.properties没有用到,请忽略。

dubbo-user.xml:




    
    
 
    

    
    
View Code

root-config.xml:





    
View Code

spring-mvc.xml:





    
    
    
    
    
    
    
    
        
        
        
    

View Code

web.xml:



  Archetype Created Web Application
    
        contextConfigLocation  
        classpath:META-INF/spring/root-config.xml  
      
      
      
        encodingFilter  
        org.springframework.web.filter.CharacterEncodingFilter  
          
            encoding  
            UTF-8  
          
      
      
        encodingFilter  
        /*  
      
      
      
        org.springframework.web.context.ContextLoaderListener  
      
      
      
        org.springframework.web.util.IntrospectorCleanupListener  
      
  
      
      
        SpringMVC  
        org.springframework.web.servlet.DispatcherServlet  
          
            contextConfigLocation  
            classpath:META-INF/spring/spring-mvc.xml  
          
        1  
      
      
        SpringMVC  
          
        /  
     
   
    
        /index.jsp
     
View Code

 windows下运行项目:

1.启动zookeeper:找到zookeeper的bin目录,双击zkServer.cmd。执行完效果如下:

 

(注:我的第二步,启动dubbo服务,用执行jar的方式启动,比较繁琐。并不适用,最好是改变pom文件,打包编译的配置,用到assembly,install之后,把.tar.gz包上传到服务器,解压,里面就包含了lib目录,直接运行类似于./start.sh start命令就好了,不用像我这样启动dubbo服务,可我一直没搞出来,知道的请告诉我,再此先谢谢大神)

2.启动dubbo服务:选中banksteel-yesen(服务提供方)右键--->Run As---> Maven clean--->Maven install一下。

没有报错的话,在target目录下找到banksteel-yesen-provider-0.0.1-SNAPSHOT.jar 和 lib,然后一起copy出来,比如放d盘下,执行jar。  

命令: java -jar banksteel-yesen-provider-0.0.1-SNAPSHOT.jar

 

我们看到dubbo服务已经启动了。

3.连接zookeeper客户端,我们可以看到会多出一个/dubbo节点。

双击zkCli.cmd,然后输入 h ,按enter键,然后查看zookeeper的一些命令。

4.启动服务消费者web工程。

tomcat启动,我就不说了,运行的效果如下:

5.dubbo监控中心的使用:

dubbo-monitor-simple-2.5.4-SNAPSHOT-assembly.tar.gz  解压后。进入conf目录,找到dubbo.properties,编辑,保存。

进入bin目录,双击start.bat。

然后打开浏览器,输入: http:locahost:8080/services.html。  (localhost其实是zookeeper服务器的地址),效果如下:

由上图可了解有哪些dubbo服务,以及服务的消费情况。

 

你可能感兴趣的:(JAVA,WEB)