哪个框架能代替dubbo_dubbo框架_dubbo能做什么(5)

电脑杂谈  发布时间:2017-05-02 00:05:09  来源:网络整理

下面,开发部署我们的应用,通过如下4个步骤来完成:

服务接口定义

服务接口将服务提供方(Provider)和服务消费方(Consumer)连接起来,服务提供方实现接口中定义的服务,即给出服务的实现,而服务消费方负责调用服务。我们接口中给出了2个方法,一个是实时查询获取当前内人数,另一个是查询一天中某个/某些中人数峰值,接口定义如下所示:

package org.shirdrn.dubbo.api;

import java.util.List;

public intece ChatRoomOnlineUserCounterService {

     String queryRoomUserCount(String rooms);
    
     List<String> getMaxOnlineUserCount(List<String> rooms, String date, String dateFormat);
}

接口是服务提供方和服务消费方公共遵守的协议,一般情况下是服务提供方将接口定义好后提供给服务消费方。

服务提供方

服务提供方实现接口中定义的服务,其实现和普通的服务没什么区别,我们的实现类为ChatRoomOnlineUserCounterServiceImpl,代码如下所示:

package org.shirdrn.dubbo.provider.service;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.shirdrn.dubbo.api.ChatRoomOnlineUserCounterService;
import org.shirdrn.dubbo.common.utils.DateTimeUtils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;

public class ChatRoomOnlineUserCounterServiceImpl implements ChatRoomOnlineUserCounterService {

     private static final Log LOG = LogFactory.getLog(ChatRoomOnlineUserCounterServiceImpl.class);
     private JedisPool jedisPool;
     private static final String KEY_USER_COUNT = "chat::room::play::user::cnt";
     private static final String KEY_MAX_USER_COUNT_PREFIX = "chat::room::max::user::cnt::";
     private static final String DF_YYYYMMDD = "yyyyMMdd";

     public String queryRoomUserCount(String rooms) {
          LOG.info("Params[Server|Recv|REQ] rooms=" + rooms);
          StringBuffer builder = new StringBuffer();
          if(!Strings.isNullOrEmpty(rooms)) {
               Jedis jedis = null;
               try {
                    jedis = jedisPool.getResource();
                    String[] fields = rooms.split(",");
                    List<String> results = jedis.hmget(KEY_USER_COUNT, fields);
                    builder.append(StringUtils.join(results, ","));
               } catch (Exception e) {
                    LOG.error("", e);
               } finally {
                    if(jedis != null) {
                         jedis.close();
                    }
               }
          }
          LOG.info("Result[Server|Recv|RES] " + builder.toString());
          return builder.toString();
     }
    
     @Override
     public List<String> getMaxOnlineUserCount(List<String> rooms, String date, String dateFormat) {
          // HGETALL chat::room::max::user::cnt::20150326
          LOG.info("Params[Server|Recv|REQ] rooms=" + rooms + ",date=" + date + ",dateFormat=" + dateFormat);
          String whichDate = DateTimeUtils.format(date, dateFormat, DF_YYYYMMDD);
          String key = KEY_MAX_USER_COUNT_PREFIX + whichDate;
          StringBuffer builder = new StringBuffer();
          if(rooms != null && !rooms.isEmpty()) {
               Jedis jedis = null;
               try {
                    jedis = jedisPool.getResource();
                    return jedis.hmget(key, rooms.toArray(new String[rooms.size()]));
               } catch (Exception e) {
                    LOG.error("", e);
               } finally {
                    if(jedis != null) {
                         jedis.close();
                    }
               }
          }
          LOG.info("Result[Server|Recv|RES] " + builder.toString());
          return Lists.newArrayList();
     }
    
     public void setJedisPool(JedisPool jedisPool) {
          this.jedisPool = jedisPool;
     }

}


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/shumachanpin/article-44653-5.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...