dubbo入门案例搭建

spring与dubbo、Zookeeper案例

一、安装zookeeper

首先到官网下载zookeeper,版本自选点击打开链接https://zookeeper.apache.org/

1.下载完之后开始安装。这里在windows安装介绍(ps电脑性能不好,就在此演示,用法一样):

先搭建一个单机模式的的ZooKeeper环境。

2.进入到CONF目录下,将里面的.cfg文件重命名为zoo.cfg.

# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=D:\\zookeeper-3.4.6\\data
dataLogDir=D:\\zookeeper-3.4.6\\log

#dataDir=/tmp/zookeeper

从上面代码可以看到添加了两行。在本机里。zookeeper放在D盘里,然后就添加了dataDir及dataLogDir两个变量。与此同时在zookeeper文件目录下新建data及log两个文件夹,如果不创建,后面运行脚本是地会报错。

完成后,进入bin目录,运行zkServer.cmd脚本,让后就可以在单机上将zookeeper跑起来了。

剩下来的事情就是去配置DUBBO的XML文件了,通过IP地址的设置,提供本地的服务。

二、实现案例代码

1、消费者服务代码

public class IndexController {


    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dubbo-consumer.xml"});
        context.start();

        TestRegistryService testRegistryService = (TestRegistryService) context.getBean("testRegistryService"); // 获取远程服务代理
        String hello = testRegistryService.hello("world"); // 执行远程方法

        System.out.println(hello); // 显示调用结果
        System.in.read();
    }
}
2、消费者服务注册中心

<?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管理页面比较清晰是哪个应用暴露出来的 -->
    <dubbo:application name="dubbo_consumer"></dubbo:application>
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false"/>

    <dubbo:reference interface="com.stocsis.gavin.registry.service.TestRegistryService" id="testRegistryService"/>

</beans>
3.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">
    <parent>
        <groupId>com.stocsis.gavin</groupId>
        <artifactId>microservice</artifactId>
        <version>1.0</version>
    </parent>

    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>com.stocsis.gavin</groupId>
            <artifactId>provider</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>consumer</artifactId>
</project>

4、提供者服务代码

(1)、接口(这应该单独为一个服务)

public interface TestRegistryService {
    public String hello(String name);
}
(2)实现

public class TestRegistryServiceImpl implements TestRegistryService {
    public String hello(String name) {
        return "hello "+name;
    }
}
(3)服务启动类

public class DemoProvider {

   public static void main(String[] args)throws Exception {
//        new Thread(new Runnable() {
//            public void run() {
//                try {
//                    Thread.sleep(20000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                ProtocolConfig.destroyAll();
//            }
//        }).start();
        ClassPathXmlApplicationContext content = new ClassPathXmlApplicationContext(new String[]{"dubbo-provider.xml"});
        content.start();
        System.in.read();
//        com.alibaba.dubbo.container.Main.main(args);
    }

}
(5)服务注册中心

<?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管理页面比较清晰是哪个应用暴露出来的 -->
    <dubbo:application name="dubbo_provider"></dubbo:application>
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <!-- 要暴露的服务接口 -->
    <dubbo:service interface="com.stocsis.gavin.registry.service.TestRegistryService" ref="testRegistryService"/>

    <bean id="testRegistryService" class="com.stocsis.gavin.registry.serviceImpl.TestRegistryServiceImpl"></bean>

</beans>
(6)日志

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
   <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n" />
      </layout>
   </appender>
   <root>
      <level value="INFO" />
      <appender-ref ref="CONSOLE" />
   </root>
</log4j:configuration>
(7)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">
    <parent>
        <groupId>com.stocsis.gavin</groupId>
        <artifactId>microservice</artifactId>
        <version>1.0</version>
    </parent>

    <packaging>war</packaging>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>provider</artifactId>

</project>
3、项目总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.stocsis.gavin</groupId>
    <artifactId>microservice</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.0.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.0.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.0.8.RELEASE</version>
        </dependency>
        <!-- dubbo start-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
            <exclusions>
                <exclusion>
                    <artifactId>spring</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.8</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>


        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>

    </dependencies>

</project>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

平凡之路无尽路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值