Dubbo服务端/客户端demo

本文详细介绍如何从零开始搭建一个包含服务端与客户端的Dubbo微服务架构,涵盖Maven项目搭建、接口定义与实现、Spring配置及Zookeeper注册中心的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    项目组采用分布式服务,线上有几十个应用,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>  
    整个工程是可以运行的。

   四 完整工程

    服务端代码请移步  https://github.com/bruce256/dubbo-server
    客户端代码请移步  https://github.com/bruce256/dubbo.test.client




评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bruce128

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值