Dubbo-Spring+Dubbo+ZooKeeper单机服务

1.ZooKeeper的安装:

ZooKeeper的安装很简单,下载架包解压缩,解压缩后的目录下有个conf目录也就是zookeeper的配置文件在该目录下,该目录下有一个zoo_sample.cfg 文件,复制一个修改为zoo.cfg,像我们这里是单服务的所以这里的配置只需要修改dataDir的目录就可以了其他的可以不用修改,dataDir的目录可以自己随便定义!!!

 

2.接口定义模块(dubbo-api)

2.1 在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>groupId</groupId>
    <artifactId>dubbo-api</artifactId>
    <version>1.0-SNAPSHOT</version>


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

    <build>
        <finalName>dubbo-demo</finalName>
        <plugins>
            <!-- 非多个资源配置 start-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                    <failOnError>false</failOnError>
                </configuration>
            </plugin>
            <!-- 非多个资源配置 end-->
        </plugins>
    </build>

    
</project>

2.2 定义服务接口:

package com.dubbo.api;

public interface ServiceDemo {
    public String say(String str);
}

 

3.服务提供者模块(dubbo-provider)

3.1 该模块依赖dubbo-api,并实现dubbo-api中定义的接口:

package com.dubbo.provider;

import com.dubbo.api.ServiceDemo;

public class ServiceImp implements ServiceDemo {
    @Override
    public String say(String str) {
        System.err.println("server: "+str);
        return "hello: "+str;
    }
}

3.2 向注册中心注册服务(将接口暴露出去),这里的注册中心是zookeeper:

下面我们来看看,服务的配置也就是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">


    <!-- 提供方应用信息,用于计算依赖关系,这个和client没必要一致 -->
    <dubbo:application name="hello-world-app-my" />
    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <!-- <dubbo:registry address="multicast://192.168.0.101:2181" /> -->
    <!-- 使用zookeeper广播注册中心暴露服务地址 -->
    <dubbo:registry  protocol="zookeeper"  address="127.0.0.1:2181"/>
    <!--和上面的配置是一样的效果  -->
    <!-- <dubbo:registry address="zookeeper://127.0.0.1:2181" />  -->
    <!-- 本机 伪集群 测试 -->
    <!--  <dubbo:registry  protocol="zookeeper"  address="192.9.145.19:2181,192.9.145.19:2182,192.9.145.19:2183"  /> -->
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.dubbo.api.ServiceDemo"
                   ref="demoService" />
    <!-- 和本地bean一样实现服务 -->
    <bean id="demoService" class="com.dubbo.provider.ServiceImp"/>

</beans>

3.3 启动服务提供者(提前启动zookeeper)

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args)throws Exception
    {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationProvider.xml" });
        context.start();
        System.out.println("按任意键退出");
        System.in.read();
    }
}

 

4.服务消费者模块(dubbo-consumer)

该模块同样依赖dubbo-api

4.1 服务消费者的配置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.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        ">
    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="consumer-of-helloworld-app-my" />
    <!-- 使用zookeeper广播注册中心暴露发现服务地址 -->
    <dubbo:registry  protocol="zookeeper"  address="127.0.0.1:2181" />
    <!-- 和上面的一样 -->
    <!-- <dubbo:registry  address="zookeeper://192.168.0.101:2181" />    -->
    <!--本地伪集群配置 -->
    <!-- <dubbo:registry  protocol="zookeeper"  address="192.9.145.19:2181,192.9.145.19:2182,192.9.145.19:2183" /> -->
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="demoServicemy" interface="com.dubbo.api.ServiceDemo" />
</beans>

4.2 新建客户端调用服务的方法

package com.dubbo.consumer;

import com.dubbo.api.ServiceDemo;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        int i=0;
        while(i++<5){
            run();
            Thread.sleep(3000);
        }
    }
    public static void run(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationConsumer.xml" });
        context.start();
        ServiceDemo demoServer = (ServiceDemo) context.getBean("demoServicemy");
        String str=demoServer.say("java ---->>>");
        System.err.println("res: "+str);
    }

}

运行该方法,查看控制台日志:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值