您的位置:首页 > 其它

dubbox消费端和服务端

2015-06-30 18:08 169 查看
<h2>zookeeper.properties
</h2><div><pre name="code" class="html">zookeeper.address=zookeeper://10.100.142.30:2181


asyncall_consumer.xml

<context:property-placeholder location="zookeeper.properties" />
<dubbo:application name="asyn_call_demo" />
<dubbo:registry address="${zookeeper.address}" />
<!-- async 非阻塞实现并行调用,客户端不需要启动多线程即可完成并行调用多个远程服务,相对多线程开销较小。 -->
<dubbo:reference id="userService" interface="cn.creditease.service.UserService"
async="true" />
<dubbo:reference id="animalService"
interface="cn.creditease.service.AnimalService" group="*" async="true" />

clusterfail_consumer.xml

<context:property-placeholder location="zookeeper.properties" />
<dubbo:application name="cluster_fail_demo" />
<!-- 缺省主机IP查找顺序: 通过LocalHost.getLocalHost()获取本机地址。 
<span style="white-space:pre">	</span>如果是127.*等loopback地址,则扫描各网卡,获取网卡IP。 -->
<dubbo:protocol host="192.168.1.102" />
<!-- 注册中心服务器地址,如果地址没有端口缺省为9090,同一集群内的多个地址用逗号分隔,
<span style="white-space:pre">	</span> 如:ip:port,ip:port,不同集群的注册中心,请配置多个<dubbo:registry>标签 -->
<dubbo:registry address="${zookeeper.address}" />
<!-- cluster可选failover/failfast/failsafe/failback/forking 默认是failover。
<span style="white-space:pre">	</span> retries默认为2,第一次失败之后重新访问两次 -->
<dubbo:reference id="userService" interface="cn.creditease.service.UserService"
cluster="failsafe" retries="2" />

diffversions_consumer.xml

<context:property-placeholder location="zookeeper.properties" />
<dubbo:application name="diff_versions_demo"/>
<!-- 注册中心服务器地址,如果地址没有端口缺省为9090,同一集群内的多个地址用逗号分隔,
如:ip:port,ip:port,不同集群的注册中心,请配置多个<dubbo:registry>标签 -->
<dubbo:registry address="${zookeeper.address}"/>
<!--  -->
<dubbo:reference id="userService_1"
interface="cn.creditease.service.UserService" version="1.0.0"/>
<dubbo:reference id="userService_2"
interface="cn.creditease.service.UserService" version="2.0.0"/>
<dubbo:reference id="userService_3"
interface="cn.creditease.service.UserService" version="3.0.0"/>
<dubbo:reference id="userService_4"
interface="cn.creditease.service.UserService" version="4.0.0"/>
<dubbo:reference id="userService_5"
interface="cn.creditease.service.UserService" version="5.0.0"/>


eventnotice_consumer.xml

<context:property-placeholder location="zookeeper.properties" />
<dubbo:application name="event_notice_demo"/>
<dubbo:registry address="${zookeeper.address}"/>
<bean id="notify" class="cn.creditease.event.UserNotifyImpl"/>
<dubbo:reference id="userService" interface="cn.creditease.service.UserService">
<dubbo:method name="get"  onthrow="notify.onthrow" onreturn="notify.onreturn"/>
</dubbo:reference>

group_consumer.xml

<context:property-placeholder location="zookeeper.properties" />
<dubbo:application name="group_consumer_demo" />
<!-- 注册中心服务器地址,如果地址没有端口缺省为9090,同一集群内的多个地址用逗号分隔,
如:ip:port,ip:port,不同集群的注册中心,请配置多个<dubbo:registry>标签 -->
<dubbo:registry address="${zookeeper.address}"/>
<!-- group用于表示调用不同实现的接口 如果是*号 表示随机调取 -->
<!-- <dubbo:reference id="animalService" interface="cn.creditease.service.AnimalService"
group="horse"/> -->
<!-- <dubbo:reference id="animalService" interface="cn.creditease.service.AnimalService"
group="tiger"/> -->
<dubbo:reference id="animalService"
interface="cn.creditease.service.AnimalService" group="*" />

loadbalance_consumer.xml

<dubbo:application name="load_balance_demo" />
<dubbo:registry address="${zookeeper.address}"/>
<!-- loadbalance默认是random, 还可以有roundrobin、leastactive、consistenthash -->
<!-- <dubbo:reference id="userService" interface="cn.creditease.service.UserService"/> -->
<!-- <dubbo:reference id="userService" interface="cn.creditease.service.UserService"
loadbalance="roundrobin"/> -->
<dubbo:reference id="userService" interface="cn.creditease.service.UserService"
loadbalance="consistenthash" />

paramcallback_consumer.xml

<context:property-placeholder location="zookeeper.properties" />
<dubbo:application name="param_callback_demo"/>
<dubbo:registry address="${zookeeper.address}"/>
<!-- async 非阻塞实现并行调用,客户端不需要启动多线程即可完成并行调用多个远程服务,相对多线程开销较小。 -->
<dubbo:reference id="userService"  interface="cn.creditease.service.UserService" async="true"/>


quickstart_consumer.xml

<!-- name为必填项,一般为项目的名字  -->
<dubbo:application name="quict_start_demo"/>
<!-- id: 服务引用bean的id,方便spring容器使用(必填)
interface:服务接口名(必填  )
-->
<dubbo:reference id="userService"  interface="cn.creditease.service.UserService"
url="dubbo://127.0.0.1:20880"/>



<span style="font-family: Arial, Helvetica, sans-serif;">package consumer;</span>
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.creditease.dto.UserDTO;
import cn.creditease.listener.CallBackListener;
import cn.creditease.service.AnimalService;
import cn.creditease.service.UserService;

import com.alibaba.dubbo.rpc.RpcContext;

public class ConsumerTest {
/**
* 无注册中心,快速启动,体验dubbo
*/
@Test
public void quickStart() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"quickstart_consumer.xml");
context.start();
UserService service = (UserService) context.getBean("userService");
System.err.println(service.get(1));
}

/**
* 服务分组
*/
@Test
public void group() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"group_consumer.xml");
context.start();
AnimalService service = (AnimalService) context
.getBean("animalService");
System.err.println(service.eat());
}

/**
* 需要查看参照管理控制台
*/
@Test
public void diffversions() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"diffversions_consumer.xml");
context.start();
UserService service1 = (UserService) context.getBean("userService_1");
UserService service2 = (UserService) context.getBean("userService_2");
UserService service3 = (UserService) context.getBean("userService_3");
System.err.println(service1.get(1));
RpcContext context1 = RpcContext.getContext();
System.out.println(context1.getUrl());
System.err.println(service2.get(2));
RpcContext context2 = RpcContext.getContext();
System.out.println(context2.getUrl());
System.err.println(service3.get(3));
RpcContext context3 = RpcContext.getContext();
System.out.println(context3.getUrl());
}

/**
* 异步调用
*
* @throws ExecutionException
* @throws InterruptedException
*
*/
@Test
public void asynCall() throws InterruptedException, ExecutionException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"asyncall_consumer.xml");
context.start();
UserService userService = (UserService) context.getBean("userService");
AnimalService animalService = (AnimalService) context
.getBean("animalService");

UserDTO dto = userService.get(1);
/* 此调用会立即返回null */
Assert.assertNull(dto);
/* 拿到调用的Future引用,当结果返回后,会被通知和设置到此Future。 */
Future<UserDTO> dtoFuture = RpcContext.getContext().getFuture();
String eat = animalService.eat();
/* 拿到调用的Future引用,当结果返回后,会被通知和设置到此Future。 */
Future<String> eatFuture = RpcContext.getContext().getFuture();
/* 此调用会立即返回null */
Assert.assertNull(eat);
/*
* 此时get(1)和eat()的请求同时在执行,客户端不需要启动多线程来支持并行,而是借助NIO的非阻塞完成。
* 如果userDTO已返回,直接拿到返回值,否则线程wait住,等待userDTO返回后,线程会被notify唤醒。
*/
UserDTO userDTO = dtoFuture.get();
/* 同理等待eatStr返回。 */
String eatStr = eatFuture.get();
/* 如果userDTO需要5秒返回,eatStr需要6秒返回,实际只需等6秒,即可获取到userDTO和eatStr,进行接下来的处理。 */
System.out.println(userDTO);
System.out.println(eatStr);
}

/**
* 参数回调
*
* @throws Exception
*/
@Test
public void paramCallback() throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"paramcallback_consumer.xml");
context.start();
UserService userService = (UserService) context.getBean("userService");
userService.batchAddUsers(new CallBackListener() {
@Override
public void changed(String msg) {
System.out.println("进行到:" +
ab90
msg);
}
}, new ArrayList<UserDTO>());
System.in.read();
}

/**
* 集群容错
*/
@Test
public void clusterFail() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"clusterfail_consumer.xml");
context.start();
UserService service = (UserService) context.getBean("userService");
System.err.println(service.get(1));
}

/**
* 负载均衡
*/
@Test
public void loadBalance() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"loadbalance_consumer.xml");
context.start();
UserService service = (UserService) context.getBean("userService");
for (int i = 0; i < 50; i++) {
System.err.println(service.loadbalance());
}
}

/**
* 事件通知
*
* @throws IOException
*/
@Test
public void eventNotice() throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"eventnotice_consumer.xml");
context.start();
UserService service = (UserService) context.getBean("userService");
/* System.err.println(service.get(1)); */
System.err.println(service.get(100000));
System.in.read();
}
}
</pre><pre name="code" class="java"><pre name="code" class="java">package provider;

import java.io.IOException;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
*
* @author yzw 利用测试类来模拟服务器 启动服务
*/
public class ProviderServerTest {

/**
* 无注册中心,快速启动服务
*
* @throws IOException
*/
@Test
public void quitStartServer() throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"quickstart_provider.xml");
context.start();

// 阻塞线程退出

System.in.read();
}

/**
* 不同分组的服务
*
* @throws IOException
*/
@Test
public void groupServer() throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"group_provider.xml");
context.start();

System.in.read();
}

/**
* 多版本服务
*
* @throws IOException
*/
@Test
public void diffVersionsServer() throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"diffversions_provider.xml");
context.start();

System.in.read();
}

/**
* 异步调用
*
* @throws IOException
*/
@Test
public void asynCallServer() throws IOException {
System.out.println("............");

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"asyncall_provider.xml");
context.start();

System.in.read();
}

/**
* 参数回调
*
* @throws IOException
*/
@Test
public void paramCallbackServer() throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"paramcallback_provider.xml");
context.start();

System.in.read();
}

/**
* 集群容错 服务一
*
* @throws IOException
*/
@Test
public void clusterfailServer1() throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"clusterfail_provider1.xml");
context.start();

System.err.println("clusterfailServer1 启动。。");
System.in.read();
}

/**
* 集群容错 服务二
*
* @throws IOException
*/
@Test
public void clusterfailServer2() throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"clusterfail_provider2.xml");
context.start();

System.in.read();
}

/**
* 负载均衡 服务一
*
* @throws IOException
*/
@Test
public void loadbalanceServer1() throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"loadbalance_provider1.xml");
context.start();

System.in.read();
}

/**
* 负载均衡 服务二
*
* @throws IOException
*/
@Test
public void loadbalanceServer2() throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"loadbalance_provider2.xml");
context.start();

System.in.read();
}

/**
* 负载均衡 服务三
*
* @throws IOException
*/
@Test
public void loadbalanceServer3() throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"loadbalance_provider3.xml");
context.start();

System.in.read();
}

/**
* 事件通知
*
* @throws IOException
*/
@Test
public void eventNoticeServer() throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"eventnotice_provider.xml");
context.start();

System.in.read();
}

// /**
// * 运行所有测试类,看监控系统
// * @param args
// */
// public static void main(String[] args) {
// TestSuite suite = new TestSuite();
// suite.addTest(new ProviderServerTest());
// // suite.tests();
// junit.textui.TestRunner.run(suite);
// }
}

源码地址(包含word文档):http://download.csdn.net/detail/u011270470/9471587

github地址:https://github.com/yangzhenwei-java/dubbo_yzw.git   有丰富的dubbo资料


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: