基于Dubbo框架构建分布式服务(二)

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
          <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
          <property name="ignoreResourceNotFound" value="true" />
          <property name="locations">
               <list>
                    <value>classpath*:jedis.properties</value>
               </list>
          </property>
     </bean>
     <dubbo:application name="chatroom-cluster-provider" />
     <dubbo:registry address="zookeeper://zk1:2181?backup=zk2:2181,zk3:2181" />
    
     <dubbo:protocol name="dubbo" port="20880" />
    
     <dubbo:service interface="org.shirdrn.dubbo.api.ChatRoomOnlineUserCounterService" version="1.0.0"
          cluster="failover" retries="2" timeout="1000" loadbalance="random" actives="100" executes="200"
          ref="chatRoomOnlineUserCounterService" protocol="dubbo" >
          <dubbo:method name="queryRoomUserCount" timeout="500" retries="2" loadbalance="roundrobin" actives="50" />
     </dubbo:service>
    
     <bean id="chatRoomOnlineUserCounterService" class="org.shirdrn.dubbo.provider.service.ChatRoomOnlineUserCounterServiceImpl" >
          <property name="jedisPool" ref="jedisPool" />
     </bean>
    
     <bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="destroy">
          <constructor-arg index="0">
               <bean class="org.apache.commons.pool2.impl.GenericObjectPoolConfig">
                    <property name="maxTotal" value="${redis.pool.maxTotal}" />
                    <property name="maxIdle" value="${redis.pool.maxIdle}" />
                    <property name="minIdle" value="${redis.pool.minIdle}" />
                    <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" />
                    <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
                    <property name="testOnReturn" value="${redis.pool.testOnReturn}" />
                    <property name="testWhileIdle" value="true" />
               </bean>
          </constructor-arg>
          <constructor-arg index="1" value="${redis.host}" />
          <constructor-arg index="2" value="${redis.port}" />
          <constructor-arg index="3" value="${redis.timeout}" />
     </bean>
    
</beans>

上面配置中,使用dubbo协议,集群容错模式为failover,服务级别负载均衡策略为random,方法级别负载均衡策略为roundrobin(它覆盖了服务级别的配置内容),其他一些配置内容可以参考Dubbo文档。我们这里是从Redis读取数据,所以使用了Redis连接池。启动服务示例代码如下所示:

package org.shirdrn.dubbo.provider;

import org.shirdrn.dubbo.provider.common.DubboServer;

public class ChatRoomClusterServer {

     public static void main(String[] args) throws Exception {
          DubboServer.startServer("classpath:provider-cluster.xml");
     }

}

上面调用了DubboServer类的静态方法startServer,如下所示:

  public static void startServer(String config) {
          ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config);
          try {
               context.start();
               System.in.read();
          } catch (IOException e) {
               e.printStackTrace();
          } finally {
               context.close();
          }
     }

方法中主要是初始化Spring IoC容器,全部对象都交由容器来管理。 

  • ●服务消费方

服务消费方就容易了,只需要知道注册中心地址,并引用服务提供方提供的接口,消费方调用服务实现如下所示:

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: onlineUserCounts= " + onlineUserCounts); } }

 

对应的配置文件为consumer.xml,内容如下所示:

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

也可以根据需要配置dubbo:reference相关的属性值,也可以配置dubbo:method指定调用的方法的配置信息,详细配置属性可以参考Dubbo官方文档。

  • ●部署与验证

开发完成提供方服务后,在本地开发调试的时候可以怎么简单怎么做,如果是要部署到生产环境,则需要打包后进行部署,可以参考下面的Maven POM配置:

   
  <build>
          <plugins>
               <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>1.4</version>
                    <configuration>
                         <createDependencyReducedPom>true</createDependencyReducedPom>
                    </configuration>
                    <executions>
                         <execution>
                              <phase>package</phase>
                              <goals>
                                   <goal>shade</goal>
                              </goals>
                              <configuration>
                                   <transformers>
                                        <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                                        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                             <mainClass>org.shirdrn.dubbo.provider.ChatRoomClusterServer</mainClass>
                                        </transformer>
                                   </transformers>
                              </configuration>
                         </execution>
                    </executions>
               </plugin>
          </plugins>
     </build>
 

这里也给出Maven POM依赖的简单配置:

     <dependencies>
          <dependency>
               <groupId>org.shirdrn.dubbo</groupId>
               <artifactId>dubbo-api</artifactId>
               <version>0.0.1-SNAPSHOT</version>
          </dependency>
     </dependencies>
 

我们开发的服务应该是分布式的,首先是通过配置内容来决定,例如设置集群模式、设置负载均衡模式等,然后在部署的时候,可以在多个节点上同一个服务,这样多个服务都会注册到Dubbo注册中心,如果某个节点上的服务不可用了,可以根据我们配置的策略来选择其他节点上的可用服务,后面通过Dubbo服务管理中心和监控中心就能更加清楚明了。 

Dubbo服务管理与监控

我们需要在安装好管理中心和监控中心以后,再将上面的开发的提供方服务部署到物理节点上,然后就能够通过管理中心和监控中心来查看对应的详细情况。

  • ●Dubbo服务管理中心

安装Dubbo服务管理中心,需要选择一个Web容器,我们使用Tomcat服务器。首先下载Dubbo管理中心安装文件dubbo-admin-2.5.3.war,或者直接从源码构建得到该WAR文件。这里,我们已经构建好对应的WAR文件,然后进行安装,执行如下命令:

cd apache-tomcat-6.0.35
rm -rf webapps/ROOT
unzip ~/dubbo-admin-2.5.3.war -d webapps/ROOT
 
修改配置文件~/apache-tomcat-6.0.35/webapps/ROOT/WEB-INF/dubbo.properties,指定我们的注册中心地址以及登录密码,内容如下所示:
dubbo.registry.address=zookeeper://zk1:2181?backup=zk2:2181,zk3:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest

然后,根据需要修改~/apache-tomcat-6.0.35/conf/server.xml配置文件,主要是Tomcat HTTP 端口号(我这里使用8083端口),完成后可以直接启动Tomcat服务器:

cd ~/apache-tomcat-6.0.35/
bin/catalina.sh start

然后访问地址 http://10.10.4.130:8083/ 即可,根据配置文件指定的root用户密码,就可以登录Dubbo管理控制台。

posted @ 2016-08-02 16:33  _1900  阅读(733)  评论(0编辑  收藏  举报