Dubbo入门之一:实例1

原文地址:http://blog.csdn.net/ruishenh/article/details/23180707?utm_source=tuicool

1.   概述

Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案

主要核心部件

Remoting: 网络通信框架,实现了sync-over-async 和 request-response 消息机制.

RPC: 一个远程过程调用的抽象,支持负载均衡、容灾和集群功能

Registry: 服务目录框架用于服务的注册和服务事件发布和订阅。

Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。

2.   简单实例

demo文件下载:http://download.csdn.net/detail/ruishenh/7164585
 

首先maven项目增加dubbo的jar依赖,因为要用到zookeeper注册中心,也要依赖但是要去掉自带的log4j不然会默认的版本依赖jms-1.1.jar jmxtools-1.2.1.jar jmxri-1.2.1.jar等3个包,下载挺麻烦,当然如果个人已经在自己的仓库中有了就无所谓了。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.ruishenh</groupId>
    <artifactId>gomeTest</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>gomeTest Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>3.1.0.RELEASE</spring.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.alibaba.rocketmq</groupId>
            <artifactId>rocketmq-all</artifactId>
            <version>3.0.4-open</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>com.alibaba.rocketmq</groupId>
            <artifactId>rocketmq-client</artifactId>
            <version>3.0.4-open</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.rocketmq</groupId>
            <artifactId>rocketmq-common</artifactId>
            <version>3.0.4-open</version>
        </dependency>
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.2.3</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.2.3</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.2.3</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.swinglabs</groupId>
            <artifactId>swingx</artifactId>
            <version>1.6.1</version>
        </dependency>
        <!--Redis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.4.2</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <!-- spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- 定时器 -->
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>1.7.2</version>
        </dependency>
        <!-- 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>
        <!-- spring mvc 返回 json使用 -->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.8.4</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.8.4</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.26</version>
                <configuration>
                    <connectors>
                        <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                            <port>8090</port>
                            <maxIdleTime>60000</maxIdleTime>
                        </connector>
                    </connectors>
                </configuration>
            </plugin>
        </plugins>
        <finalName>gomeTest</finalName>
    </build>
</project>

Spring的依赖自己添加就好了

 

因为要增加zookeeper的注册管理,所以如果有可用的zookeeper就用可用的zookeeper,没有可以按照如下的安装去本地安装一个。

zk 02之 Windows安装和使用zookeeper

项目结构图


/gomeTest/src/main/resources/spring/dubbo-provider.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.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="10.118.62.89:2181" />
    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <!-- <dubbo:registry address="multicast://10.57.41.19:1234" /> -->
    <dubbo:protocol name="dubbo" port="20880" />
    <dubbo:service interface="com.ruishenh.dubbo.example.DemoService" ref="demoService" />
    <!-- 和本地bean一样实现服务 -->
    <bean id="demoService" class="com.ruishenh.dubbo.example.DemoServiceImpl" />
</beans>

/gomeTest/src/main/resources/spring/dubbo-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.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="10.118.62.89:2181" />  
    <!-- 使用multicast广播注册中心暴露的服务地址 -->
    <!--<dubbo:registry address="multicast://10.57.41.19:1234" /> -->
     <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="demoService" interface="com.ruishenh.dubbo.example.DemoService" />
</beans>

/gomeTest/src/main/java/com/ruishenh/dubbo/example/DemoService.java

package com.ruishenh.dubbo.example;
public interface DemoService {
    
    public void sayHello();
    
    public String returnHello();
    
    public MsgInfo returnMsgInfo(MsgInfo info);
    
}

/gomeTest/src/main/java/com/ruishenh/dubbo/example/DemoServiceImpl.java

 

package com.ruishenh.dubbo.example;

public class DemoServiceImpl implements DemoService{
    
    public void sayHello() {
        System.out.println("hello world!");
    }

    public String returnHello() {
        return "hello world!";
    }

    public MsgInfo returnMsgInfo(MsgInfo info) {
        info.getMsgs().add("处理完毕");
        return info;
    }
}

/gomeTest/src/main/java/com/ruishenh/dubbo/example/LuncherProvider.java

 

package com.ruishenh.dubbo.example;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class LuncherProvider  {
    public static void main(String[] args) throws InterruptedException{
        LuncherProvider luncher=new LuncherProvider();
        luncher.start();
        Thread.sleep(1000*60*10);
    }
    
    void start(){
        String configLocation="spring/dubbo-provider.xml";
        ApplicationContext context =new  ClassPathXmlApplicationContext(configLocation);
        String [] names=context.getBeanDefinitionNames();
        System.out.print("Beans:");
        for (String string : names)
            System.out.print(string+",");
        System.out.println();
    }
}

/gomeTest/src/main/java/com/ruishenh/dubbo/example/LuncherConsumer.java

 

package com.ruishenh.dubbo.example;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class LuncherConsumer  {
    public static void main(String[] args) throws InterruptedException{
        LuncherConsumer luncher=new LuncherConsumer();
        luncher.start();
    }
    
    
    void start(){
        String configLocation="spring/dubbo-consumer.xml";
        ApplicationContext context =new  ClassPathXmlApplicationContext(configLocation);
        DemoService ds=(DemoService) context.getBean("demoService");
        String [] names=context.getBeanDefinitionNames();
        System.out.print("Beans:");
        for (String string : names) {
            System.out.print(string);
            System.out.print(",");
        }
        System.out.println();
        
        MsgInfo info =new MsgInfo();
        info.setId(1);
        info.setName("ruisheh");
        List<String> msgs=new ArrayList<String>();
        msgs.add("I");
        msgs.add("am");
        msgs.add("test");
        info.setMsgs(msgs);
        
        
        System.out.println(ds.returnMsgInfo(info).getMsgs());
    }
}

/gomeTest/src/main/java/com/ruishenh/dubbo/example/MsgInfo.java

 

package com.ruishenh.dubbo.example;

import java.io.Serializable;
import java.util.List;

public class MsgInfo implements Serializable {

    private static final long serialVersionUID = -2814022769568306965L;

    int id;
    String name;
    List<String> msgs;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<String> getMsgs() {
        return msgs;
    }

    public void setMsgs(List<String> msgs) {
        this.msgs = msgs;
    }

}

//启动provider:运行LuncherProvider.java

- Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5829428e: startup date [Fri Mar 31 16:21:30 CST 2017]; root of context hierarchy
- Loading XML bean definitions from class path resource [spring/dubbo-provider.xml]
- using logger: com.alibaba.dubbo.common.logger.support.Log4jLoggerFactory
- Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7cfefe3f: defining beans [hello-world-app,com.alibaba.dubbo.config.RegistryConfig,dubbo,com.ruishenh.dubbo.example.DemoService,demoService]; root of factory hierarchy
-  [DUBBO] No dubbo.properties found on the class path., dubbo version: 2.0.13, current host: 127.0.0.1
-  [DUBBO] Export dubbo service com.ruishenh.dubbo.example.DemoService to url dubbo://10.118.62.89:20880/com.ruishenh.dubbo.example.DemoService?anyhost=true&application=hello-world-app&dubbo=2.0.13&interface=com.ruishenh.dubbo.example.DemoService&methods=returnMsgInfo,returnHello,sayHello&prompt=dubbo&revision=, dubbo version: 2.0.13, current host: 127.0.0.1
-  [DUBBO] Register dubbo service com.ruishenh.dubbo.example.DemoService url dubbo://10.118.62.89:20880/com.ruishenh.dubbo.example.DemoService?anyhost=true&application=hello-world-app&dubbo=2.0.13&interface=com.ruishenh.dubbo.example.DemoService&methods=returnMsgInfo,returnHello,sayHello&prompt=dubbo&revision= to registry registry://10.118.62.89:2181/com.alibaba.dubbo.registry.RegistryService?application=hello-world-app&registry=zookeeper, dubbo version: 2.0.13, current host: 127.0.0.1
-  [DUBBO] Start NettyServer bind /0.0.0.0:20880, export /10.118.62.89:20880, dubbo version: 2.0.13, current host: 127.0.0.1
- Client environment:zookeeper.version=3.3.6-1366786, built on 07/29/2012 06:22 GMT
- Client environment:host.name=SF0001107252A.sf.com
- Client environment:java.version=1.6.0_29
- Client environment:java.vendor=Sun Microsystems Inc.
- Client environment:java.home=C:\Java\jdk1.6.0_29\jre
- Client environment:java.class.path=D:\shiva\omcs\gomeTest\target\classes;D:\local\repo\com\alibaba\rocketmq\rocketmq-client\3.0.4-open\rocketmq-client-3.0.4-open.jar;D:\local\repo\com\alibaba\rocketmq\rocketmq-common\3.0.4-open\rocketmq-common-3.0.4-open.jar;D:\local\repo\com\alibaba\rocketmq\rocketmq-remoting\3.0.4-open\rocketmq-remoting-3.0.4-open.jar;D:\local\repo\com\alibaba\fastjson\1.1.33\fastjson-1.1.33.jar;D:\local\repo\io\netty\netty-all\4.0.9.Final\netty-all-4.0.9.Final.jar;D:\local\repo\commons-cli\commons-cli\1.2\commons-cli-1.2.jar;D:\local\repo\commons-io\commons-io\2.4\commons-io-2.4.jar;D:\local\repo\commons-httpclient\commons-httpclient\3.1\commons-httpclient-3.1.jar;D:\local\repo\commons-logging\commons-logging\1.0.4\commons-logging-1.0.4.jar;D:\local\repo\commons-codec\commons-codec\1.2\commons-codec-1.2.jar;D:\local\repo\org\apache\httpcomponents\httpclient\4.2.3\httpclient-4.2.3.jar;D:\local\repo\org\apache\httpcomponents\httpcore\4.2.3\httpcore-4.2.3.jar;D:\local\repo\org\apache\httpcomponents\httpmime\4.2.3\httpmime-4.2.3.jar;D:\local\repo\ch\qos\logback\logback-classic\1.1.1\logback-classic-1.1.1.jar;D:\local\repo\org\slf4j\slf4j-api\1.7.6\slf4j-api-1.7.6.jar;D:\local\repo\ch\qos\logback\logback-core\1.1.1\logback-core-1.1.1.jar;D:\local\repo\org\swinglabs\swingx\1.6.1\swingx-1.6.1.jar;D:\local\repo\com\jhlabs\filters\2.0.235\filters-2.0.235.jar;D:\local\repo\org\swinglabs\swing-worker\1.1\swing-worker-1.1.jar;D:\local\repo\redis\clients\jedis\2.4.2\jedis-2.4.2.jar;D:\local\repo\org\apache\commons\commons-pool2\2.0\commons-pool2-2.0.jar;D:\local\repo\org\springframework\spring-webmvc\3.1.0.RELEASE\spring-webmvc-3.1.0.RELEASE.jar;D:\local\repo\org\springframework\spring-asm\3.1.0.RELEASE\spring-asm-3.1.0.RELEASE.jar;D:\local\repo\org\springframework\spring-context-support\3.1.0.RELEASE\spring-context-support-3.1.0.RELEASE.jar;D:\local\repo\org\springframework\spring-web\3.1.0.RELEASE\spring-web-3.1.0.RELEASE.jar;D:\local\repo\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;D:\local\repo\org\springframework\spring-test\3.1.0.RELEASE\spring-test-3.1.0.RELEASE.jar;D:\local\repo\org\springframework\spring-expression\3.1.0.RELEASE\spring-expression-3.1.0.RELEASE.jar;D:\local\repo\org\springframework\spring-tx\3.1.0.RELEASE\spring-tx-3.1.0.RELEASE.jar;D:\local\repo\org\springframework\spring-aop\3.1.0.RELEASE\spring-aop-3.1.0.RELEASE.jar;D:\local\repo\org\springframework\spring-beans\3.1.0.RELEASE\spring-beans-3.1.0.RELEASE.jar;D:\local\repo\org\springframework\spring-core\3.1.0.RELEASE\spring-core-3.1.0.RELEASE.jar;D:\local\repo\org\springframework\spring-context\3.1.0.RELEASE\spring-context-3.1.0.RELEASE.jar;D:\local\repo\org\springframework\spring-jdbc\3.1.0.RELEASE\spring-jdbc-3.1.0.RELEASE.jar;D:\local\repo\org\quartz-scheduler\quartz\1.7.2\quartz-1.7.2.jar;D:\local\repo\com\alibaba\dubbo\2.0.13\dubbo-2.0.13.jar;D:\local\repo\org\springframework\spring\2.5.6.SEC03\spring-2.5.6.SEC03.jar;D:\local\repo\org\javassist\javassist\3.15.0-GA\javassist-3.15.0-GA.jar;D:\local\repo\org\jboss\netty\netty\3.2.5.Final\netty-3.2.5.Final.jar;D:\local\repo\org\apache\zookeeper\zookeeper\3.3.6\zookeeper-3.3.6.jar;D:\local\repo\jline\jline\0.9.94\jline-0.9.94.jar;D:\local\repo\log4j\log4j\1.2.16\log4j-1.2.16.jar;D:\local\repo\org\codehaus\jackson\jackson-core-asl\1.8.4\jackson-core-asl-1.8.4.jar;D:\local\repo\org\codehaus\jackson\jackson-mapper-asl\1.8.4\jackson-mapper-asl-1.8.4.jar;D:\local\repo\javax\servlet\servlet-api\2.5\servlet-api-2.5.jar;D:\local\repo\javax\servlet\jsp-api\2.0\jsp-api-2.0.jar
- Client environment:java.library.path=C:\Java\jdk1.6.0_29\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Java/jre1.8.0_31/bin/server;C:/Java/jre1.8.0_31/bin;C:/Java/jre1.8.0_31/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\Java\jdk1.8.0_31\bin;C:\Java\jdk1.8.0_31\jre\bin;C:\Windows\System32;d:\Program Files (x86)\Lua\5.1\clibs;GRADLE_HOME/bin;C:\Python27;D:\soft\maven\apache-maven-3.3.9\bin;$STORM_HOME/bin;C:\Program Files\TortoiseGit\bin;C:\Program Files\Mercurial;D:\soft\gradle-2.12-all\bin;D:\soft\spark\spark-2.1.0-bin-hadoop2.7\bin;%system%\system32;C:\Program Files (x86)\Google\Chrome\Application;C:\ProgramData\Oracle\Java\javapath;C:\Java\jdk1.8.0_31\bin;C:\Java\jdk1.8.0_31\jre\bin;C:\Windows\System32;d:\Program Files (x86)\Lua\5.1\clibs;GRADLE_HOME/bin;C:\Python27;D:\soft\maven\apache-maven-3.3.9\bin;$STORM_HOME\bin;D:\soft\gradle-2.12-all\bin;D:\soft\developer tools\eclipse-jee-mars-2-win32-x86_64\eclipse;;.
- Client environment:java.io.tmpdir=C:\Users\01107252\AppData\Local\Temp\
- Client environment:java.compiler=<NA>
- Client environment:os.name=Windows 7
- Client environment:os.arch=amd64
- Client environment:os.version=6.1
- Client environment:user.name=01107252
- Client environment:user.home=d:\user\01107252
- Client environment:user.dir=D:\shiva\omcs\gomeTest
- Initiating client connection, connectString=10.118.62.89:2181 sessionTimeout=60000 watcher=com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistry$1@6b12da40
-  [DUBBO] Register: dubbo://10.118.62.89:20880/com.ruishenh.dubbo.example.DemoService?anyhost=true&application=hello-world-app&dubbo=2.0.13&interface=com.ruishenh.dubbo.example.DemoService&methods=returnMsgInfo,returnHello,sayHello&prompt=dubbo&revision=, dubbo version: 2.0.13, current host: 127.0.0.1
- Opening socket connection to server /10.118.62.89:2181
- Socket connection established to SF0001107252A.sf.com/10.118.62.89:2181, initiating session
- Session establishment complete on server SF0001107252A.sf.com/10.118.62.89:2181, sessionid = 0x15b23746f340000, negotiated timeout = 40000
-  [DUBBO] Recover register services [dubbo://10.118.62.89:20880/com.ruishenh.dubbo.example.DemoService?anyhost=true&application=hello-world-app&dubbo=2.0.13&interface=com.ruishenh.dubbo.example.DemoService&methods=returnMsgInfo,returnHello,sayHello&prompt=dubbo&revision=], dubbo version: 2.0.13, current host: 127.0.0.1
-  [DUBBO] Register: dubbo://10.118.62.89:20880/com.ruishenh.dubbo.example.DemoService?anyhost=true&application=hello-world-app&dubbo=2.0.13&interface=com.ruishenh.dubbo.example.DemoService&methods=returnMsgInfo,returnHello,sayHello&prompt=dubbo&revision=, dubbo version: 2.0.13, current host: 127.0.0.1
Beans:hello-world-app,com.alibaba.dubbo.config.RegistryConfig,dubbo,com.ruishenh.dubbo.example.DemoService,demoService,
-  [DUBBO] All clients has discontected from /10.118.62.89:20880. You can graceful shutdown now., dubbo version: 2.0.13, current host: 127.0.0.1
-  [DUBBO] disconected from /10.118.62.89:57216,url:dubbo://10.118.62.89:20880/com.ruishenh.dubbo.example.DemoService?anyhost=true&application=hello-world-app&channel.readonly.sent=true&codec=dubbo&codec.downstream=dubbo&dubbo=2.0.13&interface=com.ruishenh.dubbo.example.DemoService&methods=returnMsgInfo,returnHello,sayHello&prompt=dubbo&revision=, dubbo version: 2.0.13, current host: 127.0.0.1

 

//启动Consumer:运行LuncherConsumer.java

- Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7d2a1e44: startup date [Fri Mar 31 16:21:40 CST 2017]; root of context hierarchy
- Loading XML bean definitions from class path resource [spring/dubbo-consumer.xml]
- using logger: com.alibaba.dubbo.common.logger.support.Log4jLoggerFactory
- Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@667cbde6: defining beans [consumer-of-helloworld-app,com.alibaba.dubbo.config.RegistryConfig,demoService]; root of factory hierarchy
-  [DUBBO] No dubbo.properties found on the class path., dubbo version: 2.0.13, current host: 127.0.0.1
- Client environment:zookeeper.version=3.3.6-1366786, built on 07/29/2012 06:22 GMT
- Client environment:host.name=SF0001107252A.sf.com
- Client environment:java.version=1.6.0_29
- Client environment:java.vendor=Sun Microsystems Inc.
- Client environment:java.home=C:\Java\jdk1.6.0_29\jre
- Client environment:java.class.path=D:\shiva\omcs\gomeTest\target\classes;D:\local\repo\com\alibaba\rocketmq\rocketmq-client\3.0.4-open\rocketmq-client-3.0.4-open.jar;D:\local\repo\com\alibaba\rocketmq\rocketmq-common\3.0.4-open\rocketmq-common-3.0.4-open.jar;D:\local\repo\com\alibaba\rocketmq\rocketmq-remoting\3.0.4-open\rocketmq-remoting-3.0.4-open.jar;D:\local\repo\com\alibaba\fastjson\1.1.33\fastjson-1.1.33.jar;D:\local\repo\io\netty\netty-all\4.0.9.Final\netty-all-4.0.9.Final.jar;D:\local\repo\commons-cli\commons-cli\1.2\commons-cli-1.2.jar;D:\local\repo\commons-io\commons-io\2.4\commons-io-2.4.jar;D:\local\repo\commons-httpclient\commons-httpclient\3.1\commons-httpclient-3.1.jar;D:\local\repo\commons-logging\commons-logging\1.0.4\commons-logging-1.0.4.jar;D:\local\repo\commons-codec\commons-codec\1.2\commons-codec-1.2.jar;D:\local\repo\org\apache\httpcomponents\httpclient\4.2.3\httpclient-4.2.3.jar;D:\local\repo\org\apache\httpcomponents\httpcore\4.2.3\httpcore-4.2.3.jar;D:\local\repo\org\apache\httpcomponents\httpmime\4.2.3\httpmime-4.2.3.jar;D:\local\repo\ch\qos\logback\logback-classic\1.1.1\logback-classic-1.1.1.jar;D:\local\repo\org\slf4j\slf4j-api\1.7.6\slf4j-api-1.7.6.jar;D:\local\repo\ch\qos\logback\logback-core\1.1.1\logback-core-1.1.1.jar;D:\local\repo\org\swinglabs\swingx\1.6.1\swingx-1.6.1.jar;D:\local\repo\com\jhlabs\filters\2.0.235\filters-2.0.235.jar;D:\local\repo\org\swinglabs\swing-worker\1.1\swing-worker-1.1.jar;D:\local\repo\redis\clients\jedis\2.4.2\jedis-2.4.2.jar;D:\local\repo\org\apache\commons\commons-pool2\2.0\commons-pool2-2.0.jar;D:\local\repo\org\springframework\spring-webmvc\3.1.0.RELEASE\spring-webmvc-3.1.0.RELEASE.jar;D:\local\repo\org\springframework\spring-asm\3.1.0.RELEASE\spring-asm-3.1.0.RELEASE.jar;D:\local\repo\org\springframework\spring-context-support\3.1.0.RELEASE\spring-context-support-3.1.0.RELEASE.jar;D:\local\repo\org\springframework\spring-web\3.1.0.RELEASE\spring-web-3.1.0.RELEASE.jar;D:\local\repo\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;D:\local\repo\org\springframework\spring-test\3.1.0.RELEASE\spring-test-3.1.0.RELEASE.jar;D:\local\repo\org\springframework\spring-expression\3.1.0.RELEASE\spring-expression-3.1.0.RELEASE.jar;D:\local\repo\org\springframework\spring-tx\3.1.0.RELEASE\spring-tx-3.1.0.RELEASE.jar;D:\local\repo\org\springframework\spring-aop\3.1.0.RELEASE\spring-aop-3.1.0.RELEASE.jar;D:\local\repo\org\springframework\spring-beans\3.1.0.RELEASE\spring-beans-3.1.0.RELEASE.jar;D:\local\repo\org\springframework\spring-core\3.1.0.RELEASE\spring-core-3.1.0.RELEASE.jar;D:\local\repo\org\springframework\spring-context\3.1.0.RELEASE\spring-context-3.1.0.RELEASE.jar;D:\local\repo\org\springframework\spring-jdbc\3.1.0.RELEASE\spring-jdbc-3.1.0.RELEASE.jar;D:\local\repo\org\quartz-scheduler\quartz\1.7.2\quartz-1.7.2.jar;D:\local\repo\com\alibaba\dubbo\2.0.13\dubbo-2.0.13.jar;D:\local\repo\org\springframework\spring\2.5.6.SEC03\spring-2.5.6.SEC03.jar;D:\local\repo\org\javassist\javassist\3.15.0-GA\javassist-3.15.0-GA.jar;D:\local\repo\org\jboss\netty\netty\3.2.5.Final\netty-3.2.5.Final.jar;D:\local\repo\org\apache\zookeeper\zookeeper\3.3.6\zookeeper-3.3.6.jar;D:\local\repo\jline\jline\0.9.94\jline-0.9.94.jar;D:\local\repo\log4j\log4j\1.2.16\log4j-1.2.16.jar;D:\local\repo\org\codehaus\jackson\jackson-core-asl\1.8.4\jackson-core-asl-1.8.4.jar;D:\local\repo\org\codehaus\jackson\jackson-mapper-asl\1.8.4\jackson-mapper-asl-1.8.4.jar;D:\local\repo\javax\servlet\servlet-api\2.5\servlet-api-2.5.jar;D:\local\repo\javax\servlet\jsp-api\2.0\jsp-api-2.0.jar
- Client environment:java.library.path=C:\Java\jdk1.6.0_29\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Java/jre1.8.0_31/bin/server;C:/Java/jre1.8.0_31/bin;C:/Java/jre1.8.0_31/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\Java\jdk1.8.0_31\bin;C:\Java\jdk1.8.0_31\jre\bin;C:\Windows\System32;d:\Program Files (x86)\Lua\5.1\clibs;GRADLE_HOME/bin;C:\Python27;D:\soft\maven\apache-maven-3.3.9\bin;$STORM_HOME/bin;C:\Program Files\TortoiseGit\bin;C:\Program Files\Mercurial;D:\soft\gradle-2.12-all\bin;D:\soft\spark\spark-2.1.0-bin-hadoop2.7\bin;%system%\system32;C:\Program Files (x86)\Google\Chrome\Application;C:\ProgramData\Oracle\Java\javapath;C:\Java\jdk1.8.0_31\bin;C:\Java\jdk1.8.0_31\jre\bin;C:\Windows\System32;d:\Program Files (x86)\Lua\5.1\clibs;GRADLE_HOME/bin;C:\Python27;D:\soft\maven\apache-maven-3.3.9\bin;$STORM_HOME\bin;D:\soft\gradle-2.12-all\bin;D:\soft\developer tools\eclipse-jee-mars-2-win32-x86_64\eclipse;;.
- Client environment:java.io.tmpdir=C:\Users\01107252\AppData\Local\Temp\
- Client environment:java.compiler=<NA>
- Client environment:os.name=Windows 7
- Client environment:os.arch=amd64
- Client environment:os.version=6.1
- Client environment:user.name=01107252
- Client environment:user.home=d:\user\01107252
- Client environment:user.dir=D:\shiva\omcs\gomeTest
- Initiating client connection, connectString=10.118.62.89:2181 sessionTimeout=60000 watcher=com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistry$1@1d96f4b5
- Opening socket connection to server /10.118.62.89:2181
- Socket connection established to SF0001107252A.sf.com/10.118.62.89:2181, initiating session
-  [DUBBO] Subscribe: subscribe://10.118.62.89/com.ruishenh.dubbo.example.DemoService?application=consumer-of-helloworld-app&dubbo=2.0.13&id=demoService&interface=com.ruishenh.dubbo.example.DemoService&methods=returnMsgInfo,returnHello,sayHello, dubbo version: 2.0.13, current host: 10.118.62.89
-  [DUBBO] Register: subscribe://10.118.62.89/com.ruishenh.dubbo.example.DemoService?application=consumer-of-helloworld-app&dubbo=2.0.13&id=demoService&interface=com.ruishenh.dubbo.example.DemoService&methods=returnMsgInfo,returnHello,sayHello, dubbo version: 2.0.13, current host: 10.118.62.89
- Session establishment complete on server SF0001107252A.sf.com/10.118.62.89:2181, sessionid = 0x15b23746f340001, negotiated timeout = 40000
-  [DUBBO] Recover register services [subscribe://10.118.62.89/com.ruishenh.dubbo.example.DemoService?application=consumer-of-helloworld-app&dubbo=2.0.13&id=demoService&interface=com.ruishenh.dubbo.example.DemoService&methods=returnMsgInfo,returnHello,sayHello], dubbo version: 2.0.13, current host: 10.118.62.89
-  [DUBBO] Register: subscribe://10.118.62.89/com.ruishenh.dubbo.example.DemoService?application=consumer-of-helloworld-app&dubbo=2.0.13&id=demoService&interface=com.ruishenh.dubbo.example.DemoService&methods=returnMsgInfo,returnHello,sayHello, dubbo version: 2.0.13, current host: 10.118.62.89
-  [DUBBO] Recover subscribe services {subscribe://10.118.62.89/com.ruishenh.dubbo.example.DemoService?application=consumer-of-helloworld-app&dubbo=2.0.13&id=demoService&interface=com.ruishenh.dubbo.example.DemoService&methods=returnMsgInfo,returnHello,sayHello=[com.alibaba.dubbo.registry.support.RegistryDirectory@57922f46]}, dubbo version: 2.0.13, current host: 10.118.62.89
-  [DUBBO] Subscribe: subscribe://10.118.62.89/com.ruishenh.dubbo.example.DemoService?application=consumer-of-helloworld-app&dubbo=2.0.13&id=demoService&interface=com.ruishenh.dubbo.example.DemoService&methods=returnMsgInfo,returnHello,sayHello, dubbo version: 2.0.13, current host: 10.118.62.89
-  [DUBBO] Register: subscribe://10.118.62.89/com.ruishenh.dubbo.example.DemoService?application=consumer-of-helloworld-app&dubbo=2.0.13&id=demoService&interface=com.ruishenh.dubbo.example.DemoService&methods=returnMsgInfo,returnHello,sayHello, dubbo version: 2.0.13, current host: 10.118.62.89
-  [DUBBO] Start NettyClient SF0001107252A/10.118.62.89 connect to the server /10.118.62.89:20880, dubbo version: 2.0.13, current host: 10.118.62.89
-  [DUBBO] Refer dubbo service com.ruishenh.dubbo.example.DemoService from url zookeeper://10.118.62.89:2181/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=consumer-of-helloworld-app&dubbo=2.0.13&id=demoService&interface=com.ruishenh.dubbo.example.DemoService&methods=returnMsgInfo,returnHello,sayHello&prompt=dubbo&revision=, dubbo version: 2.0.13, current host: 10.118.62.89
Beans:consumer-of-helloworld-app,com.alibaba.dubbo.config.RegistryConfig,demoService,
[I, am, test, 处理完毕]
-  [DUBBO] Run shutdown hook now., dubbo version: 2.0.13, current host: 10.118.62.89
-  [DUBBO] Close all registries [zookeeper://10.118.62.89:2181/com.alibaba.dubbo.registry.RegistryService?application=consumer-of-helloworld-app&refer=application%3Dconsumer-of-helloworld-app%26dubbo%3D2.0.13%26id%3DdemoService%26interface%3Dcom.ruishenh.dubbo.example.DemoService%26methods%3DreturnMsgInfo%2CreturnHello%2CsayHello], dubbo version: 2.0.13, current host: 10.118.62.89
-  [DUBBO] Destroy registry: zookeeper://10.118.62.89:2181/com.alibaba.dubbo.registry.RegistryService?application=consumer-of-helloworld-app&refer=application%3Dconsumer-of-helloworld-app%26dubbo%3D2.0.13%26id%3DdemoService%26interface%3Dcom.ruishenh.dubbo.example.DemoService%26methods%3DreturnMsgInfo%2CreturnHello%2CsayHello, dubbo version: 2.0.13, current host: 10.118.62.89
-  [DUBBO] Unregister: subscribe://10.118.62.89/com.ruishenh.dubbo.example.DemoService?application=consumer-of-helloworld-app&dubbo=2.0.13&id=demoService&interface=com.ruishenh.dubbo.example.DemoService&methods=returnMsgInfo,returnHello,sayHello, dubbo version: 2.0.13, current host: 10.118.62.89
- Session: 0x15b23746f340001 closed
-  [DUBBO] Destroy reference: dubbo://10.118.62.89:20880/com.ruishenh.dubbo.example.DemoService?anyhost=true&application=consumer-of-helloworld-app&check=false&dubbo=2.0.13&id=demoService&interface=com.ruishenh.dubbo.example.DemoService&methods=returnMsgInfo,returnHello,sayHello&prompt=dubbo&revision=, dubbo version: 2.0.13, current host: 10.118.62.89
- EventThread shut down
-  [DUBBO] Close netty channel [id: 0x1494cb8b, /10.118.62.89:57216 => /10.118.62.89:20880], dubbo version: 2.0.13, current host: 10.118.62.89
-  [DUBBO] Close dubbo connect: 10.118.62.89:0-->10.118.62.89:20880, dubbo version: 2.0.13, current host: 10.118.62.89
-  [DUBBO] Close dubbo connect: 10.118.62.89:0-->10.118.62.89:20880, dubbo version: 2.0.13, current host: 10.118.62.89
-  [DUBBO] disconected from /10.118.62.89:20880,url:dubbo://10.118.62.89:20880/com.ruishenh.dubbo.example.DemoService?anyhost=true&application=consumer-of-helloworld-app&check=false&codec=dubbo&dubbo=2.0.13&id=demoService&interface=com.ruishenh.dubbo.example.DemoService&methods=returnMsgInfo,returnHello,sayHello&prompt=dubbo&revision=, dubbo version: 2.0.13, current host: 10.118.62.89

在上面可以看到调用结果的输出。

 

二、将web的http请求转到dubbo的服务上

先将app-config.xml文件中的dubbo-provider.xml打开

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mongo="http://www.springframework.org/schema/data/mongo"
    xsi:schemaLocation="http://www.springframework.org/schema/aop  
        http://www.springframework.org/schema/aop/spring-aop.xsd  
        http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans.xsd  
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context.xsd  
        http://www.springframework.org/schema/mvc  
        http://www.springframework.org/schema/mvc/spring-mvc.xsd  
        http://www.springframework.org/schema/tx  
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--将dubbo的配置文件导入 -->
    <import resource="dubbo-provider.xml" />
    <!-- <import resource="dubbo-consumer.xml" /> -->
</beans>

再看下web.xml文件的配置,web.xml位置:

web.xml内容:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>gomeTest</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/app-*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter>
        <filter-name>EncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <servlet-name>springmvc</servlet-name>
    </filter-mapping>
    <filter-mapping>
        <filter-name>EncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <error-page>
        <error-code>404</error-code>
        <location>/WEB-INF/views/error/404.html</location>
    </error-page>
    <welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

在pom.xml中增加jetty的插件jetty-maven-plugin插件

    <build>
        <plugins>
        
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.26</version>
                <configuration>
                    <connectors>
                        <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                            <port>8090</port>
                            <maxIdleTime>60000</maxIdleTime>
                        </connector>
                    </connectors>
                </configuration>
            </plugin>
        </plugins>
        <finalName>gomeTest</finalName>
    </build>

调试模式运行:

 

 启动成功后,访问http://localhost:8080/test/dubbo

 

三、搭建Dubbo服务管理与监控平台

1、安装Dubbo服务管理中心,需要选择一个Web容器,我们使用Tomcat服务器。首先下载Dubbo管理中心安装文件dubbo-admin-2.5.3.war,或者直接从源码构建得到该WAR文件;http://download.csdn.net/download/tangyali516/9219461

2、下载tomcat7

3、下载jdk1.7,http://download.csdn.net/download/zknxx/9662614(这里有两个包)

4、修改tomcat默认使用的jdk版本
1、windows平台
在catalina.bat文件和setclasspath.bat文件开头的空白处加上如下两句(指定JDK):
set JAVA_HOME=D:\soft\jdk\jdk1.7.0_64.01\jdk1.7.0_64
set JRE_HOME=D:\soft\jdk\jdk1.7.0_64.01\jdk1.7.0_64\jre

其中后面为指定的jdk安装路径。

2、linux平台
在catalina.sh文件和setclasspath.sh文件开头的空白处加上如下两句(指定JDK)
export JAVA_HOME=/usr/local/java/jdk1.6.0_18
export JRE_HOME=/usr/local/java/jdk1.6.0_18/jre

启动tomcat,dubbo-admin-2.5.3.war被解压,webapps/ROOT/WEB-INF/dubbo.properties,指定我们的注册中心地址以及登录密码,内容如下所示:

dubbo.registry.address=zookeeper://127.0.0.1:2181?backup=127.0.0.1:2182,127.0.0.1:2183
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest

然后访问地址http://10.10.4.130:8080/即可,根据配置文件指定的root用户密码,就可以登录Dubbo管理控制台。
我们将上面开发的服务提供方服务,部署到2个独立的节点上(192.168.14.1和10.10.4.125),然后可以通过Dubbo管理中心查看对应服务的状况,如图所示:

如图所示:

上图中可以看出,该服务有两个独立的节点可以提供,因为配置的集群模式为failover,如果某个节点的服务发生故障无法使用,则会自动透明地重试另一个节点上的服务,这就不至于出现拒绝服务的情况。如果想要查看提供方某个节点上的服务详情,可以点击对应的IP:Port链接,示例如图所示:

上图可以看到服务地址:

1 dubbo://10.10.4.125:20880/org.shirdrn.dubbo.api.ChatRoomOnlineUserCounterService?actives=100&anyhost=true&application=chatroom-cluster-provider&cluster=failover&dubbo=0.0.1-SNAPSHOT&executes=200&interface=org.shirdrn.dubbo.api.ChatRoomOnlineUserCounterService&loadbalance=random&methods=getMaxOnlineUserCount,queryRoomUserCount&pid=30942&queryRoomUserCount.actives=50&queryRoomUserCount.loadbalance=leastactive&queryRoomUserCount.retries=2&queryRoomUserCount.timeout=500&retries=2&revision=0.0.1-SNAPSHOT&side=provider&timeout=1000×tamp=1427793652814&version=1.0.0

如果我们直接暴露该地址也是可以的,不过这种直连的方式对服务消费方不是透明的,如果以后IP地址更换,也会影响调用方,所以最好是通过注册中心来隐蔽服务地址。同一个服务所部署在的多个节点上,也就对应对应着多个服务地址。另外,也可以对已经发布的服务进行控制,如修改访问控制、负载均衡相关配置内容等,可以通过上图中“消费者”查看服务消费方调用服务的情况,如图所示:
dubbo-admin-consumers
也在管理控制台可以对消费方进行管理控制。

  • Dubbo监控中心

Dubbo监控中心是以Dubbo服务的形式发布到注册中心,和普通的服务时一样的。例如,我这里下载了Dubbo自带的简易监控中心文件dubbo-monitor-simple-2.5.3-assembly.tar.gz,下载地址:http://download.csdn.net/download/liweifengwf/7864009可以解压缩以后,修改配置文件~/dubbo-monitor-simple-2.5.3/conf/dubbo.properties的内容,如下所示:

[sfapp@cmos1 bin]$ vi ../conf/dubbo.properties 

##
# Copyright 1999-2011 Alibaba Group.
#  
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#  
#      http://www.apache.org/licenses/LICENSE-2.0
#  
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##
dubbo.container=log4j,spring,registry,jetty
dubbo.application.name=simple-monitor
dubbo.application.owner=
#dubbo.registry.address=multicast://224.5.6.7:1234
dubbo.registry.address=zookeeper://10.118.62.89:2181
#dubbo.registry.address=redis://127.0.0.1:6379
#dubbo.registry.address=dubbo://127.0.0.1:9090
dubbo.protocol.port=7070
dubbo.jetty.port=8080
dubbo.jetty.directory=${user.home}/monitor
dubbo.charts.directory=${dubbo.jetty.directory}/charts
dubbo.statistics.directory=${user.home}/monitor/statistics
dubbo.log4j.file=logs/dubbo-monitor-simple.log
dubbo.log4j.level=WARN

然后启动简易监控中心,执行如下命令:

[sfapp@cmos1 duan]$ cd dubbo-monitor-simple-2.5.3/bin
[sfapp@cmos1 bin]$ ./start.sh
Starting the simple-monitor .......OK!
PID: 120076
STDOUT: logs/stdout.log

这里使用了Jetty Web容器,访问地址http://10.202.11.117:8080/就可以查看监控中心,Applications选项卡页面包含了服务提供方和消费方的基本信息,如图所示:

上图主要列出了所有提供方发布的服务、消费方调用、服务依赖关系等内容。
接着,查看Services选项卡页面,包含了服务提供方提供的服务列表,如图所示:

 


点击上图中Providers链接就能看到服务提供方的基本信息,包括服务地址等,如图所示:

点击上图中Consumers链接就能看到服务消费方的基本信息,包括服务地址等,如图所示:

由于上面是Dubbo自带的一个简易监控中心,可能所展现的内容并不能满足我们的需要,所以可以根据需要开发自己的监控中心。Dubbo也提供了监控中心的扩展接口,如果想要实现自己的监控中心,可以实现接口com.alibaba.dubbo.monitor.MonitorFactory和com.alibaba.dubbo.monitor.Monitor,其中MonitorFactory接口定义如下所示:

/**
* MonitorFactory. (SPI, Singleton, ThreadSafe)
*
* @author william.liangf
*/
@SPI("dubbo")
public interface MonitorFactory {
  
    /**
     * Create monitor.
     * @param url
     * @return monitor
     */
    @Adaptive("protocol")
    Monitor getMonitor(URL url);
 
}

Monitor接口定义如下所示:

/**
* Monitor. (SPI, Prototype, ThreadSafe)
*
* @see com.alibaba.dubbo.monitor.MonitorFactory#getMonitor(com.alibaba.dubbo.common.URL)
* @author william.liangf
*/
public interface Monitor extends Node, MonitorService {

}

具体定义内容可以查看MonitorService接口,不再累述。

总结

Dubbo还提供了其他很多高级特性,如路由规则、参数回调、服务分组、服务降级等等,而且很多特性在给出内置实现的基础上,还给出了扩展的接口,我们可以给出自定义的实现,非常方便而且强大。更多可以参考Dubbo官网用户手册和开发手册。

附录:Dubbo使用Maven构建依赖配置

<properties>
  <spring.version>3.2.8.RELEASE</spring.version>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<properties>
<dependencies>
  <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.5.3</version>
      <exclusions>
          <exclusion>
              <groupId>org.springframework</groupId>
              <artifactId>spring</artifactId>
          </exclusion>
          <exclusion>

              <groupId>org.apache.zookeeper</groupId>
              <artifactId>zookeeper</artifactId>
          </exclusion>
          <exclusion>
             <groupId>org.jboss.netty</groupId>
              <artifactId>netty</artifactId>
          </exclusion>
      </exclusions>
  </dependency>

  <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>

  </dependency>
  <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${spring.version}</version>
  </dependency>
  <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
  </dependency>

  <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${spring.version}</version>

  </dependency>

  <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
  </dependency>



  <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.6.2</version>
  </dependency>

  <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.16</version>
  </dependency>

  <dependency>
      <groupId>org.javassist</groupId>
      <artifactId>javassist</artifactId>
      <version>3.15.0-GA</version>
  </dependency>
  <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>hessian-lite</artifactId>
      <version>3.2.1-fixed-2</version>
  </dependency>
  <dependency>
     <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.1.8</version>
  </dependency>

  <dependency>
      <groupId>org.jvnet.sorcerer</groupId>
      <artifactId>sorcerer-javac</artifactId>
      <version>0.8</version>
  </dependency>
  <dependency>
      <groupId>org.apache.zookeeper</groupId>
      <artifactId>zookeeper</artifactId>
      <version>3.4.5</version>
  </dependency>
  <dependency>
      <groupId>com.github.sgroschupf</groupId>
      <artifactId>zkclient</artifactId>
      <version>0.1</version>
  </dependency>
  <dependency>
      <groupId>org.jboss.netty</groupId>
      <artifactId>netty</artifactId>
      <version>3.2.7.Final</version>
  </dependency>
</dependencies>

 

posted on 2014-03-03 14:15  duanxz  阅读(2175)  评论(1编辑  收藏  举报