Dubbo服务端/客户端demo

项目组采用分布式服务,线上有几十个应用,RPC调用完全依靠Dubbo。平时开发一直都是用其他人搭好的dubbo环境,最近自己抽空独立的搭建dubbo小demo,一个服务端,一个客户端。

服务端

服务端maven父工程

首先搭建一个maven父工程,引入dubbo和spring的依赖,dubbo可以和spring无缝集成。

<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<v.dubbo.ext>1.3.0-SNAPSHOT</v.dubbo.ext>
		<v.spring>3.1.2.RELEASE</v.spring>
		<v.plugin.jar>2.3.1</v.plugin.jar>
	</properties>

	<dependencies>
		<dependency>
			<groupId>com.jd.dubbo.ext</groupId>
			<artifactId>dubbo-ext-spi</artifactId>
			<version>${v.dubbo.ext}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${v.spring}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${v.spring}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${v.spring}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${v.spring}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${v.spring}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${v.spring}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${v.spring}</version>
		</dependency>
	</dependencies>

这些都是开发dubbo服务端必须要用的依赖包。

服务端接口子模块

这个模块负责向第三方应用暴露接口的定义,实现在其它的包里。third party 应用需要引入这个包,但无法看到接口的实现。

这个模块十分简单,只需提供接口定义。

package org.dubbo.server.api;

public interface DemoService {
	 public String sayHello(String name);
}

服务端接口实现子模块

重新创建一个maven module,这个模块负责实现上面模块定义的接口。

实现如下:

package org.dubbo.server.service;

import org.dubbo.server.api.DemoService;
import org.springframework.stereotype.Service;

@Service("demoService")
public class DemoServiceImpl implements DemoService{
	 public String sayHello(String name) {
         return "Hello " + name;
  }
}

同时服务端应该作为一个独立的应用部署起来,处理客户端的请求。

package org.dubbo.server.service;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DubboServer {
	public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
        context.start();  

        System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
    }
}

好,服务端跑起来了。

服务端dubbo配置

<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.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.xsd"
		default-autowire="byName">

	<context:component-scan base-package="org.dubbo.server.service.**" />
	<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
	<dubbo:application name="hehe_consumer" organization="risk.im.jd.com"
		owner="lvsheng" />

	<!-- 使用zookeeper注册中心暴露服务地址 -->
	<dubbo:registry id="registry" address="10.28.163.15:6060" />

	<dubbo:protocol id="protocol" name="dubbo" port="25000"
		heartbeat="0" threadpool="cached" threads="512" />

	<dubbo:provider id="im.riskctrl.dubbo.provider" timeout="5000"
		retries="5" loadbalance="roundrobin" cluster="failover" registry="registry">
	</dubbo:provider>

	<!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->
	<dubbo:service interface="org.dubbo.server.api.DemoService" ref="demoService"
		provider="im.riskctrl.dubbo.provider" >
	</dubbo:service>

</beans>  

注册中心

dubbo注册中心我使用的是dubbo官网的注册中心demo。

客户端

客户端简单的搭建一个普通的maven工程就行了。pom依赖跟服务端的一致。

客户端的调用代码如下:

package com.jd.lvsheng.dubbo.client.test;

import org.dubbo.server.api.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

@Service("dubboConsumer")
public class DubboConsumer {

	public static void main(String[] args) throws Exception {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
		context.start();
		DemoService demoService = context.getBean("demoService", DemoService.class);
		System.out.println(demoService.sayHello("aaa"));
		System.in.read();
	}
}

客户端的spring配置如下:

<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.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.xsd"
		default-autowire="byName">

	<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
	<dubbo:application name="dubbo_consumer" organization="risk.im.com"
		owner="lvsheng" />

	<!-- 使用zookeeper注册中心暴露服务地址 -->
	<dubbo:registry id="registry" address="10.28.163.15:6060" />

	<dubbo:protocol id="protocol" name="dubbo" port="25001"
		heartbeat="0" threadpool="cached" threads="512" />

	<dubbo:provider id="im.riskctrl.dubbo.provider" timeout="5000"
		retries="5" loadbalance="roundrobin" cluster="failover" registry="registry">
	</dubbo:provider>

	<!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->
	<dubbo:reference interface="org.dubbo.server.api.DemoService" id="demoService" registry="registry">
	</dubbo:reference>

</beans>  

整个工程是可以运行的。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-27 13:09:07

Dubbo服务端/客户端demo的相关文章

dubbo 服务端和客户端调用

下面是一种客户端和服务端的调用链,可能会走到其他分支,但是通过这一条链路,可以观察dubbo的整体脉络. 服务端: 客户端:

Android WebView与服务端交互Demo

使用WebView可以让Android端的开发工作大量减少,原因是在服务端可以为其做一定的工作,下面这个小Demo就实现了从Android客户端与服务端的交互.我这里客户端使用的工具是Eclipse,服务端使用MyEclipse. 实现效果图: 客户端: 点击登录按钮后,页面跳转,而在服务端Console中看到如下(只看最后一行即可): 可以看到服务端收到了客户端发过来的用户名:yao. 源代码: 客户端: activity_main: <RelativeLayout xmlns:android

手写一个模块化的 TCP 服务端客户端

前面的博客 基于 socket 手写一个 TCP 服务端及客户端 写过一个简单的 TCP 服务端客户端,没有对代码结构进行任何设计,仅仅是实现了相关功能,用于加深对 socket 编程的认识. 这次我们对整个代码结构进行一下优化,使其模块化,易扩展,成为一个简单意义上的“框架”. 对于 Socket 编程这类所需知识偏底层的情况(OS 协议栈的运作机制,TCP 协议的理解,多线程的理解,BIO/NIO 的理解,阻塞函数的运作原理甚至是更底层处理器的中断.网卡等外设与内核的交互.核心态与内核态的切

Zabbix 服务端&客户端,安装

文档整理中,虽然格式有点乱,但思路不乱~~ 见谅 Zabbix Server 第1章 安装环境 1.1 系统环境 [[email protected] ~]# cat /etc/redhat-release CentOS release 6.7 (Final) [[email protected] ~]# uname -r 2.6.32-573.el6.x86_64 [[email protected] ~]# ifconfig eth0|awk -F "[ :]+" 'NR==2 {

手机淘宝客应用(服务端+客户端+数据库)源码项目

欢迎大家光临我的个人网店:http://shop.662p.com/shop/view8.html 这是一款比较完整的应用源码,手机淘宝客应用(服务端+客户端+数据库),客户端是Java代码实现编程,另外服务端是采用了php的常见框架实现的,代码里面有一个sql文件是数据库导入文件 ,里面的apk是用来升级的版本 设置. 文件大小:10.94 MB 服务器:thinkphp 代码里面有一个sql文件是数据库导入文件 ,里面的apk是用来升级的版本 设置update.xml 用来跟之前安装的对比版

回显服务端/客户端

回显服务端/客户端 在这一章,我们将会实现一个小的客户端/服务端应用,这可能会是你写过的最简单的客户端/服务端应用.回显应用就是一个把客户端发过来的任何内容回显给其本身,然后关闭连接的的服务端.这个服务端可以处理任何数量的客户端.每个客户端连接之后发送一个消息,服务端接收到完成消息后把它发送回去.在那之后,服务端关闭连接. 因此,每个回显客户端连接到服务端,发送一个消息,然后读取服务端返回的结果,确保这是它发送给服务端的消息就结束和服务端的会话. 我们首先实现一个同步应用,然后实现一个异步应用,

WebService 服务端客户端 实例(一)

Delphi中WebService包含的组件解释(有7个)     (1) THTTPRIO-------:使用Http消息来调用远程使用SOAP的接口对象     (2) THTTPReqResp---:给服务器发送一个SOAP消息, THTTPReqResp在可调用接口上执行一个方法请求.       (3) TOPToSoapDomConvert ----:TOPToSoapDomConvert处理Soap方法请求的组合与分发     (4) TSoapConnection:TSoapCo

PHP-Socket服务端客户端发送接收通信实例详解

Socket介绍 什么是socket 所谓socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄.应用程序通常通过"套接字"向网络发出请求或者应答网络请求. 在Internet上的主机一般运行了多个服务软件,同时提供几种服务.每种服务都打开一个Socket,并绑定到一个端口上,不同的端口对应于不同的服务. Socket连接过程 根据连接启动的方式以及本地套接字要连接的目标,套接字之间的连接过程可以分为三个步骤:服务器监听,客户端请求,连接确认.

unity手游《摩卡世界online》全套源码(服务端+客户端+数据库)

unity手游<摩卡世界online>全套源码(服务端+客户端+数据库),客户端用unity3d开发,服务端用java开发,包含服务端源码.客户端源码.工具源码.数据库.配置搭建文档说明.编译好的文件等,解压后2.32G大小,要的速度下,过期不候. 摩卡世界是一款人气火热的休闲类手机网游,画面精致,出场人物造型各异,十分讨人喜爱,同时摩卡世界操作都是通过功能按钮的组合来完成,全拟真的操作规则和爽快的打击节奏让我们找回街机般的操作手感,华丽的必杀技.便捷的操作和流畅的画面,会让格斗爱好者爱不释手