dubbo小试

来源:互联网 发布:5s优化设置 编辑:程序博客网 时间:2024/06/15 12:05

公司项目准备重构,考虑使用分布式,所以研究了下阿里巴巴的dubbo。直接上代码

Provider端:

pom.xml:

<?xml version="1.0" encoding="UTF-8"?><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/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>com.sheng.weapp</groupId>    <artifactId>dubboserver</artifactId>    <version>1.0-SNAPSHOT</version>    <distributionManagement>        <repository>            <id>releases</id>            <name>Internal Releases</name>            <url>http://127.0.0.1:8081/nexus/content/repositories/releases/</url>        </repository>        <snapshotRepository>            <id>snapshot</id>            <name>Internal Snapshot</name>            <url>http://127.0.0.1:8081/nexus/content/repositories/snapshots/</url>        </snapshotRepository>    </distributionManagement>    <dependencies>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.11</version>            <scope>test</scope>        </dependency>        <dependency>            <groupId>commons-logging</groupId>            <artifactId>commons-logging</artifactId>            <version>1.1.1</version>        </dependency>        <dependency>            <groupId>com.alibaba</groupId>            <artifactId>dubbo</artifactId>            <version>2.5.3</version>            <exclusions>                <exclusion>                    <groupId>org.springframework</groupId>                    <artifactId>spring</artifactId>                </exclusion>            </exclusions>        </dependency>        <dependency>            <groupId>org.javassist</groupId>            <artifactId>javassist</artifactId>            <version>3.15.0-GA</version>        </dependency>        <dependency>            <groupId>log4j</groupId>            <artifactId>log4j</artifactId>            <version>1.2.15</version>            <exclusions>                <exclusion>                    <groupId>com.sun.jdmk</groupId>                    <artifactId>jmxtools</artifactId>                </exclusion>                <exclusion>                    <groupId>com.sun.jmx</groupId>                    <artifactId>jmxri</artifactId>                </exclusion>                <exclusion>                    <artifactId>jms</artifactId>                    <groupId>javax.jms</groupId>                </exclusion>                <exclusion>                    <artifactId>mail</artifactId>                    <groupId>javax.mail</groupId>                </exclusion>            </exclusions>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>4.1.6.RELEASE</version>        </dependency>        <dependency>            <groupId>org.slf4j</groupId>            <artifactId>slf4j-api</artifactId>            <version>1.7.5</version>        </dependency>        <dependency>            <groupId>org.slf4j</groupId>            <artifactId>slf4j-log4j12</artifactId>            <version>1.6.1</version>        </dependency>        <dependency>            <groupId>org.apache.zookeeper</groupId>            <artifactId>zookeeper</artifactId>            <version>3.3.6</version>        </dependency>        <dependency>            <groupId>com.github.adyliu</groupId>            <artifactId>zkclient</artifactId>            <version>2.0</version>        </dependency>        <dependency>            <groupId>org.jboss.netty</groupId>            <artifactId>netty</artifactId>            <version>3.2.0.Final</version>        </dependency>        <dependency>            <groupId>com.101tec</groupId>            <artifactId>zkclient</artifactId>            <version>0.4</version>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <artifactId>maven-assembly-plugin</artifactId>                <configuration>                    <archive>                        <manifest>                            <mainClass>com.sheng.dubboserver.DubboProviderMain</mainClass>                        </manifest>                    </archive>                    <descriptorRefs>                        <descriptorRef>jar-with-dependencies</descriptorRef>                    </descriptorRefs>                </configuration>            </plugin>        </plugins>    </build></project>

applicationProvider.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" />    <!-- 注册地址 multicast局域网内使用  消费者需声明unicast=false,否则只会有一个消费者能收到消息-->    <!--<dubbo:registry address="multicast://224.5.6.7:1234"/>  -->    <dubbo:registry address="192.168.31.239:2181" protocol="zookeeper"/>    <!-- <dubbo:registry address="127.0.0.1:2181" protocol="zookeeper"/>  -->    <!--<dubbo:protocol name="dubbo" port="20880" /> 当时本地环境win10 可能20880端口被什么占用了,启动消费端的时候始终链接不上provider端-->    <dubbo:protocol name="dubbo" port="29015" />    <bean id="demoService" class="com.sheng.dubboserver.impl.HelloWorldImpl" />    <dubbo:service interface="com.sheng.dubboserver.HelloWorld" ref="demoService"/></beans>
HelloWorld.java

package com.sheng.dubboserver;/** * Created by Administrator on 2017/6/28 0028. */public interface HelloWorld {    public String hello(String name);}
HelloWorldImpl.java

package com.sheng.dubboserver.impl;import com.sheng.dubboserver.HelloWorld;/** * Created by Administrator on 2017/6/28 0028. */public class HelloWorldImpl implements HelloWorld {    public String hello(String name) {        System.out.println("执行了-----------------");        name = name + "小明测试";        return name;    }}

主执行类,DubboProviderMain.java

package com.sheng.dubboserver;import org.springframework.context.support.ClassPathXmlApplicationContext;import java.io.IOException;/** * Created by Administrator on 2017/6/28 0028. */public class DubboProviderMain {    public static void main(String[] args) throws IOException {        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(                new String[] { "applicationProvider.xml" });        context.start();        System.out.println("Press any key to exit.");        System.in.read();    }}

下面是消费端

pom.xml

<?xml version="1.0" encoding="UTF-8"?><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/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>com.sheng.webapp</groupId>    <artifactId>dubboclient</artifactId>    <version>1.0-SNAPSHOT</version>    <dependencies>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.11</version>            <scope>test</scope>        </dependency>        <dependency>            <groupId>commons-logging</groupId>            <artifactId>commons-logging</artifactId>            <version>1.1.1</version>        </dependency>        <dependency>            <groupId>com.alibaba</groupId>            <artifactId>dubbo</artifactId>            <version>2.5.3</version>            <exclusions>                <exclusion>                    <groupId>org.springframework</groupId>                    <artifactId>spring</artifactId>                </exclusion>            </exclusions>        </dependency>        <dependency>            <groupId>org.javassist</groupId>            <artifactId>javassist</artifactId>            <version>3.15.0-GA</version>        </dependency>        <dependency>            <groupId>log4j</groupId>            <artifactId>log4j</artifactId>            <version>1.2.15</version>            <exclusions>                <exclusion>                    <groupId>com.sun.jdmk</groupId>                    <artifactId>jmxtools</artifactId>                </exclusion>                <exclusion>                    <groupId>com.sun.jmx</groupId>                    <artifactId>jmxri</artifactId>                </exclusion>                <exclusion>                    <artifactId>jms</artifactId>                    <groupId>javax.jms</groupId>                </exclusion>                <exclusion>                    <artifactId>mail</artifactId>                    <groupId>javax.mail</groupId>                </exclusion>            </exclusions>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>4.1.6.RELEASE</version>        </dependency>        <dependency>            <groupId>org.slf4j</groupId>            <artifactId>slf4j-api</artifactId>            <version>1.7.5</version>        </dependency>        <dependency>            <groupId>org.slf4j</groupId>            <artifactId>slf4j-log4j12</artifactId>            <version>1.6.1</version>        </dependency>        <dependency>            <groupId>com.github.adyliu</groupId>            <artifactId>zkclient</artifactId>            <version>2.0</version>        </dependency>        <dependency>            <groupId>org.apache.zookeeper</groupId>            <artifactId>zookeeper</artifactId>            <version>3.3.6</version>        </dependency>        <dependency>            <groupId>com.sheng.weapp</groupId>            <artifactId>dubboserver</artifactId>            <version>1.0-SNAPSHOT</version>        </dependency>    </dependencies></project>

applicationConsumer.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.xsdhttp://code.alibabatech.com/schema/dubbo    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">    <dubbo:application name="consumer-of-helloworld-app" />    <dubbo:registry address="192.168.31.239:2181"  protocol="zookeeper"  />    <!--loadbalance 负载均衡策略,可选值:random,roundrobin,leastactive,分别表示:随机,轮循,最少活跃调用-->    <dubbo:reference id="demoService" interface="com.sheng.dubboserver.HelloWorld" loadbalance="leastactive"/></beans>
Client.java


package com.sheng.dubboclient;import com.sheng.dubboserver.HelloWorld;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * Created by Administrator on 2017/6/28 0028. */public class Client implements Runnable {    public void run() {        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(                new String[] { "applicationConsumer.xml" });        context.start();        context.getBean("demoService");        HelloWorld helloWorld = (HelloWorld) context.getBean("demoService");        String hello = helloWorld.hello("小明");        System.out.println(hello);        System.out.println("执行完毕");    }    public static void main(String[] args) {        new Thread(new Client()).start();    }}


编译并deployprovider端,以供Consumer端依赖

首先安装并启动zookeeper,然后启动provider端,最后启动client端,即可成功执行程序。其中provider端可放在多台机器上部署,从而达到负载均衡的效果

实例下载地址:http://download.csdn.net/detail/shengfakun1234/9884032