DUBBO的简单搭建

首先添加zk和dubbo的依赖到 pom文件中,生产者消费者都要添加

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.7</version>
        </dependency>

        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
        </dependency>

        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.3</version>
        </dependency>

**

生产者:

**
目录结构如下:
在这里插入图片描述

//该接口为暴露的服务接口
public interface DubboService01 {
    String sayHello(String name);
}
//服务的具体实现
public class DubboService01Impl implements DubboService01 {
    @Override
    public String sayHello(String name) {
        return name + "调用dubbo成功!";
    }
}

生产者配置文件如下:

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

    <bean id="DubboService01" class="com.example.demo.service.impl.DubboService01Impl"/>

    <dubbo:registry address="zookeeper://127.0.0.1:2181?backup=127.0.0.1:2182,127.0.0.1:2183"/>

    <dubbo:protocol name="dubbo" port="20880"/>

    <dubbo:application name="hello-provider"/>

    <dubbo:service retries="0" interface="com.example.demo.service.DubboService01" ref="DubboService01"/>
</beans>
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

//生产者注册消息
public class Test {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-dubboprovider.xml");
        context.start();
        System.in.read(); //保持服务一直开启
    }
}

**

消费者

**

目录结构如下:
在这里插入图片描述

  1. pom文件需要添加依赖同生产者一致
  2. DubboService01(暴露的服务路径要和生产者的路径一致,体现在xml配置中)
//生产者所暴露的服务接口体现
public interface DubboService01 {
    String sayHello(String name);
}

消费者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-consumer"/>
    <dubbo:registry address="zookeeper://127.0.0.1:2181?backup=127.0.0.1:2182,127.0.0.1:2183"/>
    <!-- 此处的路径要和生产者路径完全一致 id和类名完全一致-->
    <dubbo:reference id="DubboService01" check="false" interface="com.example.demo.service.DubboService01"/>

</beans>
import com.example.demo.service.DubboService01;
import org.springframework.context.support.ClassPathXmlApplicationContext;

//消费者订阅消息
public class TestDubbo {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-dubboconsumer.xml");

        context.start();

        DubboService01 dubboService01 = (DubboService01) context.getBean("DubboService01");

        String sayHello = dubboService01.sayHello("周瑜");

        System.out.println(sayHello);
    }
}

测试结果 先启动生产者服务 在启动消费者即可 出现如下则调用成功(此处没有讲解如何配置zk 比较简单 百度一下吧 )在这里插入图片描述
仅作记录 希望对您也有些许的帮助

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值