标题:
基于Dubbo框架构建分布式服务(5)
[打印本页]
作者:
look_w
时间:
2018-12-16 15:29
标题:
基于Dubbo框架构建分布式服务(5)
●服务消费方
服务消费方就容易了,只需要知道注册中心地址,并引用服务提供方提供的接口,消费方调用服务实现如下所示:
[url=]
[/url]
package
org.shirdrn.dubbo.consumer;
import
java.util.Arrays;
import
java.util.List;
import
org.apache.commons.logging.Log;
import
org.apache.commons.logging.LogFactory;
import
org.shirdrn.dubbo.api.ChatRoomOnlineUserCounterService;
import
org.springframework.context.support.AbstractXmlApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public
class
ChatRoomDubboConsumer {
private
static
final
Log LOG = LogFactory.getLog(ChatRoomDubboConsumer.
class
);
public
static
void
main(String[] args)
throws
Exception { AbstractXmlApplicationContext context
=
new
ClassPathXmlApplicationContext("classpath:consumer.xml"
);
try
{ context.start(); ChatRoomOnlineUserCounterService chatRoomOnlineUserCounterService
= (ChatRoomOnlineUserCounterService)
context.getBean("chatRoomOnlineUserCounterService"
); getMaxOnlineUserCount(chatRoomOnlineUserCounterService); getRealtimeOnlineUserCount(chatRoomOnlineUserCounterService); System.in.read(); }
finally
{ context.close(); } }
private
static
void
getMaxOnlineUserCount(ChatRoomOnlineUserCounterService liveRoomOnlineUserCountService) { List
<String> maxUserCounts =
liveRoomOnlineUserCountService.getMaxOnlineUserCount( Arrays.asList(
new
String[] {"1482178010" , "1408492761", "1430546839",
"1412517075", "1435861734"}), "20150327", "yyyyMMdd"
); LOG.info(
"After getMaxOnlineUserCount invoked: maxUserCounts= " +
maxUserCounts); }
private
static
void
getRealtimeOnlineUserCount(ChatRoomOnlineUserCounterService liveRoomOnlineUserCountService)
throws
InterruptedException { String rooms
= "1482178010,1408492761,1430546839,1412517075,1435861734"
; String onlineUserCounts
=
liveRoomOnlineUserCountService.queryRoomUserCount(rooms); LOG.info(
"After queryRoomUserCount invoked:color: #000000;"> onlineUserCounts); }}[url=]
[/url]
对应的配置文件为consumer.xml,内容如下所示:
[url=]
[/url]
<?
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-3.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
"
>
<
dubbo:application
name
="chatroom-consumer"
/>
<
dubbo:registry
address
="zookeeper://zk1:2181?backup=zk2:2181,zk3:2181"
/>
<
dubbo:reference
id
="chatRoomOnlineUserCounterService"
interface
="org.shirdrn.dubbo.api.ChatRoomOnlineUserCounterService"
version
="1.0.0"
>
<
dubbo:method
name
="queryRoomUserCount"
retries
="2"
/>
</
dubbo:reference
>
</
beans
>
[url=]
[/url]
也可以根据需要配置dubbo:reference相关的属性值,也可以配置dubbo:method指定调用的方法的配置信息,详细配置属性可以参考Dubbo官方文档。
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0