Dubbo入门简单示例


   之前用过EJB做分布式系统,前段时间跟人闲聊,发现还是Dubbo+ZK用的比较多,so,自己玩玩儿。


    先安装一个zk作为服务注册中心,之后,建个maven工程,pom里面加入如下配置:


    

<dependencies>
		<!-- dubbo -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.0.13</version>
		</dependency>
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.3.6</version>
			<exclusions>
				<exclusion>
					<groupId>log4j</groupId>
					<artifactId>log4j</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.16</version>
		</dependency>


	</dependencies>


     之后,加入服务提供方的配置文件:


    

<?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"
	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">

	<!-- 提供方应用信息,用于计算依赖关系 -->
	<dubbo:application name="hello-world-app" />

	<!-- 配置zookeeper注册中心-->
	<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />

	<!-- 用dubbo协议在20880端口暴露服务 -->
	<dubbo:protocol name="dubbo" port="20880" />

	<!-- 声明需要暴露的服务接口 -->
	<dubbo:service interface="com.alibaba.dubbo.demo.DemoService"
		ref="demoService" />

	<!-- 和本地bean一样实现服务 -->
	<bean id="demoService" class="com.alibaba.dubbo.demo.DemoServiceImpl" />

</beans>


  服务提供方的接口和类:


 

package com.alibaba.dubbo.demo;

/**
 * 定义服务接口:该接口需要单独打包,在服务提供方和消费方共享
 * @author LiuHuiChao
 *
 */
public interface DemoService {
	String sayHello(String name);
}

 

/**
 * 在服务提供方实现接口:对服务消费方隐藏实现
 * @author LiuHuiChao
 *
 */
public class DemoServiceImpl implements DemoService {

	public String sayHello(String name) {
		
		return "Hello,"+name;
	}

}



之后是服务消费者:


<?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"
	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">

	<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
	<dubbo:application name="consumer-of-helloworld-app" />

	<!-- 配置zookeeper注册中心-->
	<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />

	<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
	<dubbo:reference id="demoService"
		interface="com.alibaba.dubbo.demo.DemoService" />

</beans>

消费者类:


package com.alibaba.dubbo.demo;

import org.springframework.context.support.ClassPathXmlApplicationContext;



public class Consumer {
	
	public static void main(String[] args) {
		ClassPathXmlApplicationContext  context=new ClassPathXmlApplicationContext("consumer.xml");
		context.start();
		
		//获取bean
		DemoService demoService=(DemoService)context.getBean("demoService");
		String hello=demoService.sayHello("world");
		
		System.out.println(hello);//显示调用结果
		
	}
	

}



  在测试的时候,先运行服务提供者,将服务注册到注册中心,之后再运行服务消费者进行调用。

  原理性的东西会之后跟进。大笑







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

水田如雅

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

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

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

打赏作者

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

抵扣说明:

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

余额充值