想用dubbo+zookeeper实现远程调用的第一步是安装zookeeper: zookeeper的安装步骤

安装完成之后启动zookeeper,然后安装dubbo-admin (用于更好的监控服务与管理):dubbo-admin的安装步骤

都OK了之后来开始搭建我们的项目,首先创建一个项目,我取名为dubbo

然后在pom文件里导入我们需要的依赖~

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>3.1.4.RELEASE</spring.version>
        <slf4j.version>1.6.6</slf4j.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-asm</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-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-expression</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- spring end -->
        <!-- log -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <!-- dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
        </dependency>
        <!-- zkclient  -->
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
        <!--  zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.5</version>
        </dependency>
    </dependencies>

 

记得添加zookeeper和dubbo的依赖!

依赖都添加好了以后在dubbo项目下创建两个子模块(Module)

provider:提供者

consumer:消费者

我们首先在provider(消费者)模块中添加一个接口一个实现类一个启动类

DubboAp(接口)i:

package com.sixmac.dubbo;

public interface DubboApi {

    public String say(String str);
}

DubboImpl(实现类):

package com.sixmac.dubbo;


public class DubboImpl implements DubboApi{
    @Override
    public String say(String str) {
        System.out.println("Hello: "+str);
        return "Hello: "+str;

    }
}

AppP(提供者启动类):

package com.sixmac.dubbo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class AppP {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "*.xml" });
        context.start();
        DubboApi bean = context.getBean(DubboApi.class);
        bean.say("MaNongXF");
        System.out.println("服务启动成功!");
        System.in.read();


    }

}

然后就是配置文件applicationProviderxml

<?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
        ">
    <!-- 提供方应用信息,用于计算依赖关系,这个和client没必要一致 -->
    <dubbo:application name="provider-service"/>
    <!-- 使用zookeeper广播注册中心暴露服务地址 -->
    <dubbo:registry protocol="zookeeper" address="192.168.10.93:2181"/><!--==  <dubbo:registry address="zookeeper://192.168.0.101:2181" /> -->
    <!--本地伪集群配置 -->
    <!-- <dubbo:registry  protocol="zookeeper"  address="192.168.10.93:2181,192.168.10.94:2182,192.168.10.95:2183" /> -->
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!-- 声明需要暴露的服务接口(也可以使用dubbo的service注解的interFaceClass暴露接口) -->
    <dubbo:service interface="com.sixmac.dubbo.DubboApi" ref="demoService"/>
    <!-- 和本地bean一样实现服务 -->
    <bean id="demoService" class="com.sixmac.dubbo.DubboImpl"/>
</beans>

 

然后启动,打印“服务启动成功” 

 

然后在dubbo-admin 查看

这就表示服务提供者注册成功,接下来可以在消费者中调用!

服务消费者只需要一个启动类来远程调用提供者的say方法~

AppC(消费者启动类):

package com.sixmac.app;

import com.sixmac.dubbo.DubboApi;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class AppC {
    public static void main(String[] args) throws  IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationConsumer.xml" });
        context.start();
        DubboApi api = context.getBean(DubboApi.class);
        String str=api.say("MaNongXF");
        System.err.println("res: "+str);
        System.out.println("远程调用成功!");
       System.in.read();
    }


}

然后是配置文件applicationConsumer

<?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-service" />
    <!-- 使用zookeeper广播注册中心暴露发现服务地址 -->
    <dubbo:registry  protocol="zookeeper"  address="192.168.10.93:2181" />
    <!-- 和上面的一样 -->
    <!-- <dubbo:registry  address="zookeeper://192.168.10.93:2181" />    -->
    <!--本地伪集群配置 -->
    <!-- <dubbo:registry  protocol="zookeeper"  address="192.168.10.93:2181,192.168.10.94:2182,192.168.10.95:2183" /> -->
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="demoService" interface="com.sixmac.dubbo.DubboApi" />
</beans>

然后启动AppC

 

可以看到已经成功调用了提供者的方法,然后再看看dubbo-admin

 

到这里就OK啦~

 

源码在此:点击下载源码

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐