• dubbo项目实战代码展示


    最近公司项目使用dubbo服务,于是就去网上搜索关于dubbo的相关资料,真的很多,但是对于很多人并不是很了解框架或者

    不是太适合新手的片段代码,于是我就根据项目的相关内容把dubbo部分单独切出来,所有代码可以运行。推荐先看一下

    dubbo的一篇文章http://doc.okbase.net/congcong68/archive/112508.html 。

    dubbo的介绍网上已经有很多了,我就不介绍它是什么了,我主要介绍怎么在项目中使用,怎么让一个小白会在项目中运用起

    来,就像买一辆汽车,你肯定是先让他跑起来,不会先把所有组件拆开来看看是怎么安装的吧。(如果你是,那么恭喜你,你

    已经是大神了),文字内容比较肤浅,适合初学者,如果有不当的地方欢迎指出来,留言

    下面我就介绍一下我的项目结构

    整个项目结构分为3个部分,dubbo-client 是消费者或者是调用方的代码(可以理解你自己的代码),dubbo-service存放的是

    dubbo的对外接口,只是接口的定义和基础实体类已经一些公共工具类,如果你想做的更细,可以把基础实体类和公共工具类

    提取出来放在dubbo-domain中,dubbo-web存放的是dubbo的对外接口的实现和dubbo的生产者的配置信息。

    那我们就开始对每个模块单独解释,首先从最基础的dubbo-service模块解释,一层层往上。

    这里我定一个一个User实体类,用于存放用户基本信息,一个返回结果集的封装类(可根据自己项目需求而定,不是必须要

    有),最后定义一个接口UserService.java和平常的接口并没有什么差异,看下具体内容

    package com.lwl.dubbo.service;
    
    import com.lwl.dubbo.domain.User;
    import com.lwl.dubbo.rpc.RPCResponse;
    /**
     * 用户的接口
     * @author Administrator
     * @create 2016-8-9 下午2:42:56
     * @version 1.0
     */
    public interface UserService {
    
    	/**
    	 * 正常调用
    	 * @param id
    	 * @return
    	 * @author Administrator
    	 * @create 2016-8-9 上午10:16:50
    	 */
    	public RPCResponse findUserById(long id);
    	
    	/**
    	 * 调用该方法会抛出异常
    	 * @return
    	 * @author Administrator
    	 * @create 2016-8-9 上午10:17:33
    	 */
    	public RPCResponse findUserThrowsException();
    	
    }
    
    • 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

    定义了2个接口方法,第一个方法是根据ID 获取用户信息,第二个方法比较特殊,因为我在实现的时候是让他直接抛出异常,

    这样比较更加有代表性。好了,dubbo-service这个项目已经结束了,如果你需要更多的接口,请自行添加即可。然后把这项目

    maven打包成JAR包供dubbo-web和dubbo-client依赖。

    那接下来我们就要让dubbo服务先启起来,开启dubbo-web的项目,首先介绍一下dubbo-web的项目结构

    dubbo-web的结构要比dubbo-service复杂一点,但是对于已经开发过J2EE的各位看客来说,这个其实很简单的。还记得上一

    个dubbo-service项目的完成之后我们把他打包成一个JAR包了嘛,这时候我们就有使用这个JAR了,在pom文件中引入这个文

    件即可。

    然后UserServiceImpl实现dubbo-service接口中的UserService.java接口,接下来就是UserDao和UserDaoImpl这2个和数据库打

    交道的类了,但是我为了项目整洁去除了和数据库的交流,如果看客需要则自行添加即可。

    那我们看下UserServiceImpl.java干了什么事情呢?

    package com.lwl.dubbo.service.impl;
    
    
    import javax.annotation.Resource;
    
    import org.springframework.stereotype.Service;
    
    import com.lwl.dubbo.dao.UserDao;
    import com.lwl.dubbo.domain.User;
    import com.lwl.dubbo.rpc.RPCResponse;
    import com.lwl.dubbo.service.UserService;
    
    @Service("userService")
    public class UserServiceImpl implements UserService {
    
    	@Resource(name="userDao")
    	private UserDao userDao;
    	
    	@Override
    	public RPCResponse findUserById(long id) {
    		//这里使用封装类,而不是DAO继续使用封装类,是为了DAO层更好的复用起来
    		RPCResponse response = new RPCResponse();
    		try {
    			//DAO层和我们之前开发的模式一样,没有使用封装类
    			User result = userDao.findUserById(id);
    			response.setResult(result);
    		} catch (Exception e) {
    			e.printStackTrace();
    			response.setSuccess(false);
    			response.setErrorMessage(e.getMessage());
    		}
    		
    		return response;
    	}
    
    	//调用这个方法 会抛出异常
    	@Override
    	public RPCResponse findUserThrowsException() {
    		RPCResponse response = new RPCResponse();
    		try {
    			User result = userDao.findUserThrowsException();
    			response.setResult(result);
    		} catch (Exception e) {
    			e.printStackTrace();
    			response.setSuccess(false);
    			response.setErrorMessage(e.getMessage());
    		}
    		return response;
    	}
    
    }
    
    • 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

    看到这个类调用了DAO层的接口,但是DAO层并没有使用封装类,这是因为为了更好的复用DAO层的代码,所有就在接口实

    现层做封装。

    那DAO层的实现层是怎么实现的呢?

    package com.lwl.dubbo.dao.impl;
    
    import org.springframework.stereotype.Repository;
    
    import com.lwl.dubbo.dao.UserDao;
    import com.lwl.dubbo.domain.User;
    /**
     * DAO数据层操作
     * @author Administrator
     * @create 2016-8-9 上午10:30:03
     * @version 1.0
     */
    @Repository("userDao")
    public class UserDaoImpl implements UserDao {
    
    	/**
    	 * 通过模拟数据库数据,返回结果
    	 * 		看客可以根据自己需要 从数据库获取数据然后返回
    	 * @param id
    	 * @return
    	 * @author Administrator
    	 * @create 2016-8-9 上午10:31:34
    	 */
    	@Override
    	public User findUserById(long id) {
    		User info = new User();
    		info.setId(id);
    		info.setEmail("xxxxxxxxxx@163.com");
    		info.setMobile("13844445555");
    		info.setUsername("宇宙最帅");
    		info.setPassword("12345600");
    		return info;
    	}
    
    	@Override
    	public User findUserThrowsException() {
    		//让程序出错,便于返回测试
    		int i = 1/0;
    		System.out.println(i);
    		return null;
    	}
    
    }
    
    • 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

    和想像的一样吧,就是我们平常开发中的那些代码,并没有什么特殊。

    好了代码都写完了,接下来才是重点,怎么让他和dubbo服务有关联,首先当然要引入dubbo的jar包,然后在给服务添加配置

    文件。

    下面是dubbo的配置文件dubbo_config.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

    每个都有注释,如果对dubbo的标签不是很明白的看客,可以看看本文一开始推荐的那篇文章看看。

    接下里就是在applicationContext.xml文件中引入dubbo的配置文件即可:

    好了,dubbo-web的开发已经完成了,现在运行一下,如果没有问题就打包成WAR包,发布服务吧!

    最后一个dubbo-client项目了,然后你发布服务了,肯定要有人来订阅吧,就好像你在卖东西,没有人来你这里买,你是不是很

    不开心啊。所以服务起来了,我们就要利用起来。

    dubbo-client项目看上去和平常项目没什么区别,只是多了一个dubbo的配置文件,用于订阅dubbo服务而已,当然dubbo-clien

    t项目也要依赖dubbo的JAR 和 dubbo-service ,将这2个添加到pom文件中。

    来我们先看一下项目的结构

    项目的关键至于dubbo-config配置文件,该文件告诉你订阅了哪些接口和哪个dubbo服务。项目主要有一个控制器和前端返回封装类,各自的代码是:

    package com.lwl.dubbo.controller;
    
    import javax.annotation.Resource;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import com.lwl.dubbo.domain.User;
    import com.lwl.dubbo.respone.ResultRespone;
    import com.lwl.dubbo.rpc.RPCResponse;
    import com.lwl.dubbo.service.UserService;
    
    @Controller
    @RequestMapping("/user")
    public class UserController {
    
    	@Resource(name="userService")
    	private UserService userService;
    	
    	@ResponseBody
    	@RequestMapping(value="/id",method=RequestMethod.POST)
    	public ResultRespone findUserById(long id){
    		ResultRespone respone = new ResultRespone();
    		 RPCResponse  result = userService.findUserById(id);
    		 if(result.isSuccess()){
    			 respone.setData(result.getResult());
    		 }else{
    			 respone.setSuccess(false);
    			 respone.setMsg(result.getErrorMessage());
    		 }
    		return respone;
    	}
    	
    	@ResponseBody
    	@RequestMapping(value="/exception",method=RequestMethod.POST)
    	public ResultRespone findUserThrowsException(){
    		ResultRespone respone = new ResultRespone();
    		 RPCResponse  result = userService.findUserThrowsException();
    		 if(result.isSuccess()){
    			 respone.setData(result.getResult());
    		 }else{
    			 respone.setSuccess(false);
    			 respone.setMsg(result.getErrorMessage());
    		 }
    		return respone;
    	}
    	
    }
    
    
    
    
    package com.lwl.dubbo.respone;
    /**
     * 用于将结果集返回给前端
     * @author Administrator
     * @create 2016-8-9 下午12:21:24
     * @version 1.0
     */
    public class ResultRespone {
    
    	
    	private boolean success = true;//是否成功
    	
    	private String msg;//错误信息或者提示信息
    	
    	private Object data;//数据结果集
    
    	public boolean isSuccess() {
    		return success;
    	}
    
    	public void setSuccess(boolean success) {
    		this.success = success;
    	}
    
    	public String getMsg() {
    		return msg;
    	}
    
    	public void setMsg(String msg) {
    		this.msg = msg;
    	}
    
    	public Object getData() {
    		return data;
    	}
    
    	public void setData(Object data) {
    		this.data = data;
    	}
    	
    }
    
    • 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

    然后前端使用html + ajax 请求后台控制器,代码如下index.html:

    
    
        
            
            
            
              
            
    
    
     
    
    
     
    
    
    
    
    
    
    
    • 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

    运行结果:

    好了,整个dubbo的服务以及完成了,稍后会将使用的代码上传到GIT上,到时候如果有需要的看客可以自己去查看,我也只是

    一个初学者,如果有不当的地方请指出来,文字内容比较浅,肤浅,没什么深奥的,我只是希望能帮到那些不知道怎么用的

    人。

    项目已经上传到github上,有需要的可以移步到https://github.com/1181888200/dubbo.git

  • 相关阅读:
    MySQL实战实战系列 07 行锁功过:怎么减少行锁对性能的影响?
    Android中JNI与NDK
    EMC原理-传导(共模、差模)与辐射(近场、远场)详解
    地平线GitLab使用指导
    SpringBoot 快速实现 api 接口加解密
    金球奖提名!5家自主品牌「争夺」年度高阶智能辅助驾驶系统
    高速公路和 TCP/IP 的负载均衡和拥塞
    UJNOJ_1287-1293-水题
    22-09-19 西安 谷粒商城(03)分布式锁、ab测试、LUA脚本、看门狗自动续期
    记录一次线上gitlab11.x升级gitlab14.x版本操作
  • 原文地址:https://blog.csdn.net/m0_54866636/article/details/126358020